ios面試題整合4

1.回答person的retainCount值,並解釋爲什麼

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

self.person = per;

2.這段代碼有什麼問題嗎:

@implementation Person

- (void)setAge:(int)newAge {

self.age = newAge;

}

@end


正確寫法

{

if(_age){

[_age release];

}

_age = [newAge retain];


}

死循環(擴展:知道如何正確寫setter和getter方法)


3.這段代碼有什麼問題,如何修改

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

NSString *string = @”Abc”;//常量區

string = [string lowercaseString];//新的堆區

string = [string stringByAppendingString:@"xyz"];//新的堆區

NSLog(@“%@”, string);

}

在for循環裏添加自動釋放池(擴展:常量區的retaincount是怎麼個情況)


4.截取字符串”20 | http://www.baidu.com”中,”|”字符前面和後面的數據,分別輸出它們。

componentsSeparatedByString

NSString * str = @“20|http://www.baidu.com”;

for(NSString*s in [str componentsSeparatedByString]){

NSLog(@“%@“,s);

}


5.用obj-c寫一個冒泡排序
1
2
3
4
5
6
7
8
for(int i = 0, i < arr.count - 1,i++){
for (int j = 0,j < arr.count - 1 - i;j++){
int a = [[arr objectAtIndex:j]intValue];
int b=[[arr objectAtIndex:j+1]intValue];
if(a < b){
[arr replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@“%d”,b]];
[arr replaceObjectAtIndex:j+1 withObject:[NSString stringWithFormat:@“%d”,a];
}}}

6.簡述你對UIView、UIWindow和CALayer的理解

http://blog.csdn.net/kuqideyupian/article/details/7731942

http://o0o0o0o.iteye.com/blog/1728599


7.寫一個完整的代理,包括聲明,實現

注意手寫的準確性


8.分析json、xml的區別?json、xml解析方式的底層是如何處理的?

http://www.open-open.com/bbs/view/1324367918671

http://hi.baidu.com/fevelen/item/a25253ab76f766756cd455b6


9.ViewController 的 didReceiveMemoryWarning 是在什麼時候被調用的?默認的操作是什麼?

http://blog.sina.com.cn/s/blog_68661bd80101nn6p.html


10.面向對象的三大特徵,並作簡單的介紹

封裝、繼承、多態

多態:父類指針指向子類對象。兩種表現形式:重寫(父子類之間)和重載(本類中)

OC的多態體現是:重寫,沒有重載這種表現形式


舉例說明:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@interface Parent : NSObject    //父類
- (void)simpleCall;
@end 
@interface Child_A : Parent   //子類  Child_A
@end 
@implementation Child_A
- (void)simpleCall
{
    NSLog(@"我是Child_A的simpleCall方法");
}
@end
@interface Child_B : Parent     //子類Child_B
@end
- (void)simpleCall
{
     NSLog(@"我是Child_的simpleCall方法");
}
@end


然後,我們就可以看到多態所展示的特性了:

1
2
3
4
Parent * pa=[[Child_A alloc] init];// 父類指針指向子類Child_A對象
Parent * pb=[[Child_B alloc] init]; //父類指針指向子類Child_B對象
[pa simpleCall];// 顯然是調用Child_A的方法
[pb simpleCall];// 顯然是調用Child_B的方法



在OC中常看見的多態體現:

1
2
3
4
5
6
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
 {  
     static NSString *CellWithIdentifier = @"Cell";  
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];  
      return cell;  
  }



(UITableViewCell *)指向cell子類對象


11.重寫一個NSString類型的,retain方式聲明name屬性的setter和getter方法

1
2
3
4
5
6
7
8
-(void)settetName:(NSString *)name{
if(_name){
[_name release];
}
 _name = [name retain];
}
-(NSString *)getterName{
          return [[_name retain]autorelease];


12.簡述NotificationCenter、KVC、KVO、Delegate?並說明它們之間的區別?

http://blog.csdn.net/zuoerjin/article/details/7858488

http://blog.sina.com.cn/s/blog_bf9843bf0101j5px.html


13.What is lazy loading?

懶漢模式,只在用到的時候纔去初始化。也可以理解成延時加載。我覺得最好也最簡單的一個列子就是tableView中圖片的加載顯示了。一個延時載,避免內存過高,一個異步加載,避免線程堵塞


14.什麼是Protocol?什麼是代理?寫一個委託的interface?委託的property聲明用什麼屬性?爲什麼?

委託的property聲明用什麼屬性是assign(防止循環引用)


15.分別描述類別(categories)和延展(extensions)是什麼?以及兩者的區別?繼承和類別在實現中有何區別?爲什麼Category只能爲對象添加方法,卻不能添加成員變量?

考慮類目比繼承的優點

類別是把類的實現方法分散到不同的文件中 也可以給類擴展新方法

延展是給類添加私有方法 只爲自己類所見 所使用

繼承可以擴展實例變量 而類別不能

類別如果可以添加成員變量 就跟繼承沒什麼兩樣了  而且在上線的項目更新中 用類別筆繼承更能維護項目的穩定性


16.Objective-C有私有方法麼?私有變量呢?如多沒有的話,有沒有什麼代替的方法?

oc沒有私有方法 但是有私有變量@property  私有方法可以用延展代替


17.#import、#include和@class有什麼區別

#import 系統文件、自定義文件引用 不用擔心重複引用的問題

#include 跟#import幾乎一樣 但是他需要注意不能重複引用

@class 只是告訴系統有這個類 但是如果在實現類中用到這個類 需要重新用#import導入該類頭文件 


18.談談你對MVC的理解?爲什麼要用MVC?在Cocoa中MVC是怎麼實現的?你還熟悉其他的OC設計模式或別的設計模式嗎?

mvc - model view controller   避免了view與model 的強耦合 使代碼更靈活 更容易維護 可複用 可擴展   oc其他設計模式有Notification 。target;action.  singleton delegate


19.如監測系統鍵盤的彈出

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( ) name:UIKeyboardWillShowNotification object:nil];

擴展:ios 彈出鍵盤擋住UITextView的解決方式


20.舉出5個以上你所熟悉的ios  sdk庫有哪些和第三方庫有哪些?

AFWorking/WebKit/SQLite/Core Data/Address Book


21.如何將產品進行多語言發佈?

http://fengmm521.blog.163.com/blog/static/25091358201291645852889/


22.如何將敏感字變成**

1
2
3
4
5
search = @"某某某";
    replace = @“***”;
    range = [mstr rangeOfString:search];
    [mstr replaceCharactersInRange:range withString:replace];    
    NSLog(@"%@",mstr);

  

23.objc中的減號與加號代表什麼?

類方法

24.單例目的是什麼,並寫出一個?

避免重複創建  節省內存空間

1
2
3
4
5
6
7
8
static Model * model;
+(id)singleton{
if(!model){
 @synchronized(self){
model = [[Model alloc]init];
}}
return model;
}


25.說說響應鏈

http://www.tuicool.com/articles/6jmUje


從手指觸摸屏幕的地方的最上層控件是第一響應者,事件會沿着響應鏈一直向下傳遞直到被接受並作出處理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章