iOS 面试题(六)

  1. 回答person的retainCount值,并解释为什么

Person * per = [[Person alloc] init];

self.person = per;

1 或者2  
看对象用什么属性修饰,Person类对象在创建的时候引用计数加1,若用 assign类不会使引用计数加1的属性修饰 person的retainCount= 1 ; 若使用retain类会使引用计数增加的属性修饰,引用计数加1, person的retainCount = 2  
 
2  这段代码有什么问题吗:

@implementation Person

-(void)setAge:(int)newAge {


self.age = newAge;

}

@end

死循环  setter方法里调用setter方法

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

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

NSString *string = @”Abc”;

string = [string lowercaseString];

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

NSLog(@“%@”, string);

}

i在满足条件下一直自加1循环执行 ,造成内存消耗
在循环里面加入自动释放池 @autoreleasepool{}

4  截取字符串”20 | http://www.baidu.com”中,”|”字符前面和后面的数据,分别输出它们。

    NSLog(@"%@",[string componentsSeparatedByString:@“|"][0]);

    NSLog(@"%@",[string componentsSeparatedByString:@“|"][1]);


5  用obj-c写一个冒泡排序

NSMutableArray *array = [NSMutableArray arrayWithObject:@“15”,@“12”,@“5”,@“8”,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 exchangeObjetAtIndex:i withObjectAtIndex:j];

       } 

    }

}

for(int i = 0;i < array.count;i++){

    NSLog(@“%@“,array[i]);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值