25個 iOS 面試題整理(帶答案)二

hzeng999 8年前發布 | 34K 次閱讀 iOS開發 設計模式 移動開發

來自: http://www.cocoachina.com/programmer/20160219/15331.html

第一篇面試題整理:

http://www.cocoachina.com/bbs/read.php?tid-459620.html

本篇面試題同樣:如答案有問題,歡迎指正!

1.回答person的retainCount值,并解釋為什么

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

self.person = per;

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

<p>@implementation Person</p>

- (void)setAge:(int)newAge {

self.age = newAge;

}

<p>@end</p>

正確寫法

{

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寫一個冒泡排序

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.baiduhome.net/bbs/view/1324367918671

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

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

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

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

封裝、繼承、多態

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

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

舉例說明:

@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</span></pre>

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

    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中常看見的多態體現:

    - (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath
    {
    static NSString
    CellWithIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];  
    return cell;  
    

    }</span></pre>

    (UITableViewCell *) 指向 cell 子類對象

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

    -(void)settetName:(NSString )name{
    if(_name){
    [_name release];
    }
    _name = [name retain];
    }
    -(NSString )getterName{

        return [[_name retain]autorelease];</span></pre> 
    

    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幾乎一樣 但是他需要注意不能重復引用

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

    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.如何將敏感字變成**

    search = @"某某某";

    replace = @“***”;

    range = [mstr rangeOfString:search];

    [mstr replaceCharactersInRange:rangewithString:replace];    

    NSLog(@"%@",mstr);

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

    類方法

    24.單例目的是什么,并寫出一個?

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

    static Model * model;
    +(id)singleton{
    if(!model){
    @synchronized(self){
    model = [[Model alloc]init];
    }}
    return model;
    }

    25.說說響應鏈

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

    從手指觸摸屏幕的地方的最上層控件是第一響應者,事件會沿著響應鏈一直向下傳遞直到被接受并作出處理

    </div>

 本文由用戶 hzeng999 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!