1,描述應用程序的啟動順序。
? ? ?1、程序入口main函數創(chuàng)建UIApplication實例和UIApplication代理實例
? ? ?2咐蚯、在UIApplication代理實例中重寫啟動方法,設置第一ViewController
? ? ?3蜓斧、在第一ViewController中添加控件仓蛆,實現(xiàn)對應的程序界面。
為什么很多內置類如UITableViewControl的delegate屬性都是assign而不是retain挎春?請舉例說明看疙。防止循環(huán)引用,
Student * str=[];
Teacher *teacher=[[Teacher alloc] init];
Student * student=[[Student alloc] init];
teacher.delegate=student;
student.delegate= teacher;
在teacher中dealloc會release當前的Delegate直奋,就會觸發(fā)student對象release能庆,繼而也會導致student執(zhí)行dealloc,在student中也會release自己的delegate脚线,產生循環(huán)了搁胆。
2,使用UITableView時候必須要實現(xiàn)的幾種方法邮绿?
2個渠旁。
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section; 這個方法返回每個分區(qū)的行數
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath;這個方法返回我們調用的每一個單元格
3,寫一個便利構造器船逮。
+ (id)studentWithName:(NSString *)newName andAge:(int)newAge{
?Student *stu = [[Student alloc] initWithName:newName andAge:newAge];
?return [stu autorelease];
}
4顾腊,UIImage初始化一張圖片有幾種方法?簡述各自的優(yōu)缺點挖胃。
1杂靶、從資源讀取 , 這個方法的圖片是從緩存里面獲取的, 先在緩存里面查看是不是有這個圖片, 沒有的話將圖片添加進緩存再使用. 有的話直接使用緩存里面的. 如果這張圖片用的次數比較多的話, 建議使用這種方式. 缺點是效率低下.UIImage *image = [UIImage imageNamed:@”1.png”];
2 .從手機本地讀取, 比較第一種方式, 這個事直接加載圖片的. 所以建議在圖片使用率低的圖片時 使用這個方法. //讀取本地圖片非resource
NSString?*aPath3=[NSString?stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];[UIImage imageWithContentsOfFile:aPath3]
5,回答person的retainCount值酱鸭,并解釋為什么
Person * per = [[Person alloc] init];
self.person = per;person屬性如果為assign的話retainCount為1吗垮,如果為retain的話retainCount為2
6,這段代碼有什么問題嗎:
@implementation Person
- (void)setAge:(int)newAge {
self.age = newAge;
}
@end
答:死循環(huán)
7凹髓,這段代碼有什么問題,如何修改
for (int i = 0; i < someLargeNumber; i++) {
NSString *string = @”Abc”;
string = [string lowercaseString];
string = [string stringByAppendingString:@"xyz"];
NSLog(@“%@”, string);
}
答:如果數字很大的話會造成內存一直增加(因為一直通過便利構造器方法創(chuàng)建autorelease對象)烁登,直到循環(huán)結束才減少,在循環(huán)內加一個自動釋放池蔚舀,更改后代碼如下:
for (int i = 0; i < someLargeNumber; i++) {
NSString *string = @”Abc”;@autoreleasepool {
string = [string lowercaseString];
string = [string stringByAppendingString:@"xyz"];
NSLog(@“%@”, string);
}
}
8饵沧,截取字符串”20 | http://www.baidu.com”中蚀之,”|”字符前面和后面的數據,分別輸出它們捷泞。
NSString *string = @” 20 | http://www.baidu.com”;[string componentsSeparatedByString:@”|”];
9,用obj-c寫一個冒泡排序
NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"3",@"1",@"10",@"5",@"2",@"7",@"12",@"4",@"8"]];
for (int i = 0; i < array.count; i ++) {
for (int j = 0; j < array.count? - 1 - i; j++) {
if ([[array objectAtIndex:j] integerValue] > [[array objectAtIndex:j + 1] integerValue]) {
[array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
NSLog(@"%@", array);