面试题(3)

1,描述应用程序的启动顺序。

     1、程序入口main函数创建UIApplication实例和UIApplication代理实例

     2、在UIApplication代理实例中重写启动方法,设置第一ViewController

     3、在第一ViewController中添加控件,实现对应的程序界面。

为什么很多内置类如UITableViewControl的delegate属性都是assign而不是retain?请举例说明。防止循环引用,

Student * str=[];

Teacher *teacher=[[Teacher alloc] init];

Student * student=[[Student alloc] init];

teacher.delegate=student;

student.delegate= teacher;

在teacher中dealloc会release当前的Delegate,就会触发student对象release,继而也会导致student执行dealloc,在student中也会release自己的delegate,产生循环了。

2,使用UITableView时候必须要实现的几种方法?

2个。

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section; 这个方法返回每个分区的行数

-(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初始化一张图片有几种方法?简述各自的优缺点。

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

答:死循环

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

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

NSString *string = @”Abc”;

string = [string lowercaseString];

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

NSLog(@“%@”, string);

}

答:如果数字很大的话会造成内存一直增加(因为一直通过便利构造器方法创建autorelease对象),直到循环结束才减少,在循环内加一个自动释放池,更改后代码如下:

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);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.#import和#include的区别 @class? TA的最新馆藏 明星| 妈妈快看这里有天使!透明感.....
    晚照清舟阅读 595评论 0 0
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 1,393评论 0 3
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,205评论 30 471
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,759评论 18 399
  • 题目链接:在二维数组中查找 我的思路 逐步缩小矩阵的范围,当矩阵缩小到一个数字的时候,看是否该数字是目标数字。是t...
    贾雨村甄士隐阅读 650评论 5 7