21.為什么很多內置類如UITableViewControl的delegate屬性都是assign而不是retain?
防止循環(huán)引用.
如:對象A引用了對象B,對象B引用了對象C,對象C引用了對象B,這個時候B的引用計數是2,而C的引用計數是1,當A不再使用B的時候,就釋放了B的所有權,這個時候C還引用對象B,所以B不會釋放,引用計數為1,因為B也引用著對象C,B不釋放,那么C也就不會被釋放,所以他們的引用計數都為1,并且永遠不會被釋放,形成了循環(huán)引用.
22.使用UITableView的時候必須要實現的幾種方法?
2個數據源方法.分別是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
23.寫一個遍歷構造器.
+(id)leftModelWith{
leftModel * model = [self alloc]init];
return model;
}
24.UIImage初始化一張圖片有幾種方法?簡述其特點?
3種,
imageNamed:系統(tǒng)會先檢查系統(tǒng)緩存中是否有該名字的image,如果有的話,則直接返回,如果沒有,則先加載圖像到緩存,然后再返回.
initWithContentsOfFile:系統(tǒng)不會檢查緩存,而直接從文件系統(tǒng)中記載并返回.
imageWithCGImage:scale:orientation 當scale= 1的時候圖像為原始大小,orientation指定繪制圖像的方向.
25.person的retainCount值,并解釋為什么?
Person * per = [Person alloc]init];
self.person = per;
1或者2.看person是什么類型修飾的.
alloc+1,assign+0,retain+1.
26.下面這段代碼有何問題?
@implementation Person
- (void)setAge:(int)newAge {
self.age = newAge;
}
@end
死循環(huán)
27.? ? 這段代碼有什么問題,如何修改
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++) {
@antoreleasepool {
NSString *string = @”Abc”;
string = [string lowercaseString];
string = [string stringByAppendingString:@"xyz"];
NSLog(@“%@”, string);
}
}
28.截取字符串"20 | http://www.baidu.com"中,"|"字符前面和后面的數據,分別輸出它們。
["20 | http://www.baidu.com" componentSeparatedByString:@"|"];
29.用obj-c 寫一個冒泡排序.
NSMutableArray *ary = [@[@"1", @"2", @"3", @"4", @"6", @"5"] mutableCopy];
for (int i = 0; i < ary.count - 1; i++) {
for (int j = 0; j < ary.count - i - 1; j++) {
if ([ary[j] integerValue] < [ary[j + 1] integerValue]) {
[ary exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
NSLog(@"%@", ary);
30.簡述對UIView.UIWindow和CALayer的理解.
UIWindow是應用的窗口,繼承于UIView.
UIView繼承于UIResponder,是創(chuàng)建窗口中的一個視圖,可以響應交互事件.一個程序只有一個主window,可以有多個window.
CALayer圖層,一個view可有多個圖層,不可以響應事件.