您的位置:首页 > 移动开发 > IOS开发

iOS开发中遇到的问题整理 (一)

2014-11-25 14:36 531 查看
1、@try @catch @finally 的用途是?
答:Handling Exception (处理异常) https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html#//apple_ref/doc/uid/20000059-BBCHGJIJ

https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Exceptions/Exceptions.html

2、如何用GCD创建一个单例方法?
答:+ (SVProgressHUD*)sharedView
{
static dispatch_once_t once;
static SVProgressHUD *sharedView;
dispatch_once(&once, ^ { sharedView = [[SVProgressHUD alloc] initWithFrame:[[UIScreen
mainScreen] bounds]]; });
return sharedView;
}

3、多线程下防止单例被同时调用怎么办?
答:可以加一个线程锁 @synchronized(self) 或 NSLock

4、枚举如何定义?
答:

typedef enum

{

//以下是枚举成员

TestA = 0,

TestB,

TestC,

TestD

}Test;//枚举名称

亦可以如下定义(推荐:结构比较清晰):

typedef NS_ENUM(NSInteger, Test1)

{

//以下是枚举成员

Test1A = 0,

Test1B = 1,

Test1C = 2,

Test1D = 3

};

枚举的定义还支持位运算的方式定义,如下:

等于号后面必须等于1

typedef NS_ENUM(NSInteger, Test)

{

TestA = 1, //1 1 1

TestB = 1 << 1, //2 2 10 转换成 10进制 2

TestC = 1 << 2, //4 3 100 转换成 10进制 4

TestD = 1 << 3, //8 4 1000 转换成 10进制 8

TestE = 1 << 4 //16 5 10000 转换成 10进制 16

};

5、动态加载xib?
答:self.view = [[[NSBundle mainBundle]loadNibNamed:@"RecommednViewController" owner:self options:nil]lastObject]; 同时需要关联xib为该controller

6、if else 与 switch case 区别??
答:if-else结构中,条件判断部分可以是任何符合语法的表达式。switch-case结构中,switch括号中以及case右边的表达式必须是常量表达式。如果条件判断所用的表达式并非常量,那么不能用switch-case,只能用if-else。

7、GoogleMaps常用的类?
答:
GMSCameraPosition (确定即将呈现地图的位置)
GMSMapView (地图视图)
GMSMapViewDelegate (代理)
GMSMutablePath (点与点之间的路径)
GMSPolyline (呈现路径的线条)
GMSMarker (地图上的小icon)
CLLocationDegrees (封装坐标点)

CLLocation (坐标)
CLGeocoder (通过坐标来进行城市解码)
CLPlacemark (储存的城市信息)
CLLocationManager

8、RunLoop 的使用,如何阻塞当前线程??
答:NSRunLoop * asyRunLoop=[NSRunLoop currentRunLoop];

while (pause)
{

[asyRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

}

9、iOS autoresizingMask与autoLayout的区别?
答:autoresizingMask是子视图的左、右、上、下边距以及宽度和高度相对于父视图按比例变化
UIViewAutoresizingNone (不自动调整)

autoLayout是子视图相对于某个视图(可以是父视图也可以是同级兄弟视图)的位置,在屏
幕尺寸发生变化时,相对位置保持不变

//在页面水平居中显示按钮。
[self.viewaddConstraint: [NSLayoutConstraintconstraintWithItem:self.btn

attribute:NSLayoutAttributeCenterX

relatedBy:NSLayoutRelationEqual

toItem:self.view

attribute:NSLayoutAttributeCenterX

multiplier:1.0

constant:0]];

10、如何实现微信摇一摇功能?
答:AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

player=[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[player prepareToPlay];
player.volume = .8;
[player play];

11、图片移动的动画效果?
答://让imagdown上下移动
CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];
translation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 345)];
translation.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 420)];
translation.duration = 0.5;
translation.repeatCount = 1;
translation.autoreverses = YES;
[_imgDown.layer addAnimation:translation forKey:@"translation"];

11、如何适配IOS6.0 和IOS7.0 ,以及3.5inch 和4.0inch的屏幕?
答:
方法一:

int width = [[UIScreen mainScreen] bounds].size.width;
int height = [[UIScreen mainScreen] bounds].size.height;

if (IOS7_EARLY) {

_myTabelview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 , width , height - 64 - 49 ) style:UITableViewStyleGrouped];
}
else
_myTabelview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 , width , height - 64 -49) style:UITableViewStyleGrouped];

方法二:
UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];

if (IOS7_EARLY) {

_myTabelview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 , 320, CGRectGetHeight(bgView.frame)- 64 - 49 ) style:UITableViewStyleGrouped];
}
else
_myTabelview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 , 320, CGRectGetHeight(bgView.frame) - 64 -49) style:UITableViewStyleGrouped];

12、静态方法和实例化方法的区别??
答: 从线程安全、性能、兼容性上来看 也是选用实例化方法为宜。
早期的结构化编程,几乎所有的方法都是“静态方法”,引入实例化方法概念是面向对象概念出现以后的事情了,区分静态方法和实例化方法不能单单从性能上去理解,创建c++,java,c#这样面向对象语言的大师引入实例化方法一定不是要解决什么性能、内存的问题,而是为了让开发更加模式化、面向对象化。这样说的话,静态方法和实例化方式的区分是为了解决模式的问题。
拿别人一个例子说事:
比如说“人”这个类,每个人都有姓名、年龄、性别、身高等,这些属性就应该是非静态的,因为每个人都的这些属性都不相同;但人在生物学上属于哪个门哪个纲哪个目等,这个属性是属于整个人类,所以就应该是静态的——它不依赖与某个特定的人,不会有某个人是“脊椎动物门哺乳动物纲灵长目”而某个人却是“偶蹄目”的。
/article/2377979.html

13、如何一次性将NSLog语句移除?
答:在-Prefix.pch文件中加入以下代码:
#ifdef DEBUG
# define DLog(...) NSLog(__VA_ARGS__)
#else
# define DLog(...) /* */
#endif
#define ALog(...) NSLog(__VA_ARGS__)

14、typedef void (*comm_callback)(char*, int, void*); 是什么意思 ?
答:定义一个函数指针类型comm_callback
这个函数类型是这样:返回值void 参数:char*,int,void*
可以直接使用comm_callback定义一个函数指针
如:comm_callback pFun;
http://modernrobber.blog.163.com/blog/static/214636320071125112629565/

/article/10851061.html (函数指针右左法则)

15、双向链表是怎样的?
答:线性表的双向链表存储结构
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior,*next;
}DuLNode,*DuLinkList;

16、正则表达式??
答:NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES
%@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];

17、键盘的显示和隐藏,动画效果?
答:const float movementDuration = .3f;
[UIView beginAnimations:@"anim" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:movementDuration];
//一个动画的运动随时间快慢曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//动画方式
[UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
view.frame = CGRectOffset(view.frame, 0, distance);
[UIView commitAnimations];

18、textView高度自适应??
答:IOS6 可用contentSize.height 来自适应,但是不适用于IOS7
IOS7 的方法如下:
- (CGSize)contentSizeOfTextView:(UITextView *)textView
{
CGSize textViewSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)];

return textViewSize;
}

19、NSClassFromString 和 NSSelectorFromString 的区别??

NSClassFromString

NSSelectorFromString

正常来说,

id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];



id myObj = [[MySpecialClass alloc] init];

是一样的。但是,如果你的程序中并不存在MySpecialClass这个类,下面的写法会出错,而上面的写法只是返回一个空对象而已。

因此,在某些情况下,可以使用NSClassFromString来进行你不确定的类的初始化。

比如在iPhone中,NSTask可能就会出现这种情况,所以在你需要使用NSTask时,最好使用:

[[NSClassFromString(@"NSTask") .....]]

而不要直接使用[NSTask ...]这种写法。

NSClassFromString的好处是:

1 弱化连接,因此并不会把没有的Framework也link到程序中。

2 不需要使用import,因为类是动态加载的,只要存在就可以加载。

20、CATransaction 的用处??
答:CATransaction 事务类,可以对多个layer的属性同时进行修改.它分隐式事务,和显式事务
[CATransaction begin];
[CATransaction setAnimationDuration:3.f]; // 3 second animation

GMSCameraPosition *camera =
[[GMSCameraPosition alloc] initWithTarget:marker.position
zoom:8
bearing:50
viewingAngle:60];
[mapView animateToCameraPosition:camera];
[CATransaction commit];

/article/5136141.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: