iOS面試題三

1. 描述應用程序的啓動順序。


1)程序入口main函數創建UIApplication實例和UIApplication代理實例。

2) UIApplication代理實例中重寫啓動方法,設置根ViewController

3) 在根ViewController中添加控件,實現應用程序界面。



2.爲什麼很多內置類如UITableViewControldelegate屬性都是assign而不是retain?請舉例說明。


避免循環引用

這裏delegate我們只是想得到實現了它delegate方法的對象,然後拿到這個對象的指針就可以了,我們不期望去改變它或者做別的什麼操作,所以我們只要用assign拿到它的指針就可以了。

而用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]);
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章