iOS面试题三

1. 描述应用程序的启动顺序。缓存


1)程序入口main函数建立UIApplication实例和UIApplication代理实例。ide

2) UIApplication代理实例中重写启动方法,设置根ViewController函数

3) 在根ViewController中添加控件,实现应用程序界面。spa



2.为何不少内置类如UITableViewControldelegate属性都是assign而不是retain?请举例说明。代理


避免循环引用指针

这里delegate咱们只是想获得实现了它delegate方法的对象,而后拿到这个对象的指针就能够了,咱们不指望去改变它或者作别的什么操做,因此咱们只要用assign拿到它的指针就能够了。component

而用retain的话,计数器加1。咱们有可能在别的地方指望释放掉delegate这个对象,而后经过一些判断好比说它是否已经被释放,作一些操做。可是实际上它retainCount仍是1,没有被释放掉,对象

二者相互持有.RC永远不会变成0;dealloc不会执行,二者都不会释放.排序


(一个对象不必管理本身delegate的生命周期,或者说不必拥有该对象,因此咱们只要知道它的指针就能够了,用指针找到对象去调用方法)生命周期


3.使用UITableView时候必需要实现的几种方法?


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


4.写一个便利构造器。

- (id)initWithName(NSString *)name age(int)age sex(NSString *)sex
{
    self = [super init];
    
    if (self){
        _name = name;
        _age = age;
        _sex = sex;
    }
    return self;
}

+ (id)initWithName(NSString *)name age(int)age sex(NSString *)sex
{
    Person *p = [[Person alloc]initWithName:name age:age sex:sex];
    return [p autorelease];
}


5. UIImage初始化一张图片有几种方法?简述各自的优缺点


p_w_picpathNamed:系统会先检查系统缓存中是否有该名字的Image,若是有的话,则直接返回,若是没有,则先加载图像到缓存,而后再返回。

initWithContentsOfFile:系统不会检查系统缓存,而直接从文件系统中加载并返回。

p_w_picpathWithCGImage:scale:orientation scale=1的时候图像为原始大小,orientation制定绘制图像的方向。

p_w_picpathWithData;


6.回答personretainCount值,并解释为何

Person * per = [[Person alloc] init];

self.person = per;


RC= 2;

per.retainCount = 1 ,

set方法调用retain

setter方法引用计数再+1


7. 这段代码有什么问题吗:

@implementation Person

- (void)setAge:(int)newAge {

    self.age = newAge;

}

@end


self.age在左边也是至关于setter方法,这就致使了死循环


8. 这段代码有什么问题,如何修改

for (int i = 0; i < someLargeNumber; i++) {

        NSString *string = @"Abc";

        string = [string lowercaseString];

        string = [string stringByAppendingString:@"xyz"];

        NSLog(@"%@", string);

    }


更改: 在循环里加入自动释放池@autoreleasepool{};

for (int i = 0; i < someLargeNumber; i++) {

    @autoreleasePool{NSString *string = @"Abc";

        string = [string lowercaseString];

        string = [string stringByAppendingString:@"xyz"];

        NSLog(@"%@", string);

    }

}

内存泄露.方法自带的自动释放池来不及释放.

for循环中,本身添加内存池,产生一个释放一个


9.截取字符串"20 | http://www.baidu.com"中,"|"字符前面和后面的数据,分别输出它们。

 

NSString *str = @"20|http://www.baidu.com";

NSArray *arr=[str componentsSeparatedByString:@"|"];

NSLog(@"%@%@",[arr objectAtIndex:0], [arr objectAtIndex:1]);


10. obj-c写一个冒泡排序

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"25",@"12",@"15",@"8",@"10", nil];
for (int i = 0; i < array.count - 1; i++) {
    int a = [array[i] intValue];
    for (int j = i + 1; j < array.count; j++) {
        int b = [array[j] intValue];
        if (a < b) {
            [array exchangeObjectAtIndex:i withObjectAtIndex:j];
        }
    }
}
for (int i = 0; i < array.count; i++) {
    NSLog(@"%@",array[i]);
}