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]);
}
相關文章
相關標籤/搜索