長沙戴維營教育iOS開發面試題週刊

[TOC]

1. 介紹一下assign, copy與retain的區別。
  • assign 簡單的指針賦值,不涉及引用計數的操作。

  • copy 產生一個新對象,引用計數爲1,老對象引用計數不變。

  • retain 對象的引用計數加1。

  • weak 自動引用計數環境下使用,與assign類似,但是當對象釋放後會自動置爲nil。

  • strong 自動引用計數環境下使用,類似於retain,強引用的對象不會被釋放。

2. center、frame與bounds的關係是什麼?

center和frame是指視圖在父視圖的座標系統中的表示,center表示UIView的中心點在父視圖座標系統中的位置,frame表示UIView在父視圖座標系統中的位置和大小。bounds表示視圖在它本身的座標系統中的位置。如果修改bounds的origin會導致該視圖的子視圖位置發生改變,但不會影響視圖本身的位置。一般來說,frame是由center和bounds計算得來。或者是說UIView的frame由它所擁有的CALayer的position、anchorPoint和bounds決定。 

3. 執行下面的代碼會發生什麼現象?
Ball *ball = [[[[Ball alloc] init] autorelease] autorelease];

給對象發送一次autorelease消息就會將它在自動釋放池中註冊一次。當自動釋放池release或者drain的時候,將給註冊到裏面的對象按註冊的次數發送release消息。因此最後ball對象會接收到兩次release消息,由於ball的引用計數爲1,當接收第一次release後就被釋放了。第二次release的時候會導致程序崩潰。

4. 什麼是KVC,如何使用?

KVC是由NSKeyValueCoding非正式協議所實現的一種機制,使得應用程序可以通過名字或Key來訪問對象的屬性,而不是直接調用訪問器或者實例變量。 示例代碼:

//  Copyright (c) 2014年 戴維營教育. All rights reserved.
//

#import 

@class DVIDog;

@interface DVIStudent : NSObject
{
@public
   NSString *_name;
@protected
   NSString *_studentID;
@private
   float _height;
}
//_age
@property (nonatomic, assignint age;
@property (nonatomic, strongDVIDog *dog;
@property (nonatomic, strongNSMutableArray *dogsArray;

- (void)printInfo;

- (void)setValueFromDict:(NSDictionary *)dict;
@end

類的實現:

//
//  DVIStudent.m
//  KVCSample
//
//  Copyright (c) 2014年 戴維營教育. All rights reserved.
//

#import "DVIStudent.h"

#import "DVIDog.h"

@implementation DVIStudent

- (id)init
{
   if (self = [super init]) {
       _dogsArray = [NSMutableArray array];
   }

   return self;
}

@synthesize age = _age;

- (void)printInfo
{
   NSLog(@"%@:%@:%d:%f", _name, _studentID, _age, _height);
}

- (void)setValueFromDict:(NSDictionary *)dict
{
   _name = [dict objectForKey:@"name"];
   _age = [[dict objectForKey:@"age"] intValue];
   _height = [[dict objectForKey:@"height"] floatValue];
   _studentID = [dict objectForKey:@"studentID"];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
   NSLog(@"%@:%@", key, value);
}

- (void)setNilValueForKey:(NSString *)key
{
   NSLog(@"%@", key);
}

- (void)setAge:(int)age
{
   NSLog(@"age: %d", age);
   _age = age;

   _height += 0.1;
}

- (int)age
{
   return _age;
}
@end

第二個類:

//  Copyright (c) 2014年 戴維營教育. All rights reserved.
//

#import 

@interface DVIDog : NSObject

@property (nonatomic, assignint weight;

@end

第二個類的實現:

//  Copyright (c) 2014年 戴維營教育. All rights reserved.
//

#import "DVIDog.h"

@implementation DVIDog

@end

使用KVC訪問屬性:

_student = [[DVIStudent alloc] init];
//    student.age = 20;
[_student setValue:@20 forKey:@"age"];

_student->_name = @"Zhangsan";
//1. _key
//2. key
[_student setValue:@"1001" forKey:@"studentID"];
[_student setValue:@1.7 forKey:@"height"];

[_student printInfo];

NSLog(@"%@", [_student valueForKey:@"age"]);

NSDictionary *dict = @{@"name":@"Lisi",@"studentID":@"2002",@"age":@33,@"height":@1.8};
[_student setValueFromDict:dict];
[_student printInfo];

dict = @{@"name":@"Wangwu",@"studentID":@"3002",@"age":@23,@"height":@0.8};
[_student setValuesForKeysWithDictionary:dict];
[_student printInfo];

DVIDog *dog = [[DVIDog alloc] init];
_student.dog = dog;

_student.dog.weight = 20;
[_student setValue:@30 forKeyPath:@"dog.weight"];
NSLog(@"%@", [_student valueForKeyPath:@"dog.weight"]);

[_student setValue:@12 forKey:@"weight"];
[_student setNilValueForKey:@"age"];

NSLog(@"%@", [_student valueForKey:@"age"]);

KVC中除了訪問單個屬性外,還能夠對集合屬性進行訪問和操作:

for (int i = 0i <</span> 100++i{
   DVIDog *dog = [[DVIDog alloc] init];
   dog.weight = i;
   [_student.dogsArray addObject:dog];
}

//
NSLog(@"%@", [_student valueForKeyPath:@"dogsArray.@count"]);

常用的集合操作:

bash @avg, @max, @min, @count, @sum等 

在KVC的基礎上,蘋果提供了KVO來獲取屬性改變的事件: 添加KVO觀察者:

[_student addObserver:self forKeyPath:@"name" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld |NSKeyValueObservingOptionPrior context:nil];

觀察者獲取通知:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
   NSLog(@"%@: %@: %@", keyPath, object, change);
   _nameLabel.text = [change objectForKey: NSKeyValueChangeNewKey];

}
5. 舉例說明幾個常用的Xcode環境變量。
  • $(BUILT_PRODUCTS_DIR) 構建成功後,目標文件存放的位置。

  • $(TARGET_NAME) 目標工程名。

  • $(SRCROOT) 工程文件存放的位置。

  • $(CURRENT_PROJECT_VERSION) 當前工程版本號。

6. 需要對一個代理對象進行強引用(retain)嗎?

不應該對一個對象進行強引用。代理對象可能對被代理對象擁有強引用,如UITableViewController對它裏面的tableView的引用。如果tableView的代理也是強引用的話,就會導致因爲循環引用而出現內存泄漏。

7. 描述一下UIButton類的繼承關係,直到NSObject。

NSObject <- UIResponder <- UIView <- UIControl <- UIButton

8. 什麼是@dynamic?

@synthesize 編譯器自動生成setter和getter。 @dynamic 需要手工實現setter和getter,並且不能在後面用=號標明對應的實例變量。 @compatibility_alias 給類起別名。

@compatibility_alias DVINewStudent DVIStudent;

@encode 獲取類型的字符串,可以用來判斷類型。

@encode(int) //i
@encode(CGRect) //{CGRect={CGPoint=ff}{CGSize=ff}}
@encode(DVIStudent) //{DVIStudent=#}
9. 給一個對象發送performSelector:withObject:afterDelay:消息後,會給對對象進行retain嗎?

給對象發送延時執行的消息會對對象進行retain操作,並且在方法執行完後進行release操作。

10. NSCoder類的用途是什麼?

NSCoder是一個抽象的基類,爲子類定義了在對象和其它Objective-C數據在內存和其它格式直接進行轉換的能力,是歸檔(將對象和數據存放到磁盤)和分發(在進程和線程直接進行數據傳遞)的基礎。在基礎框架(Foundation)中提供了NSKeyedArchiver、NSKeyedUnArchiver等子類。NSCoder可以操作對象、標量、C語言數組、結構體、字符串等,但是不支持聯合體、void指針、函數指針等與平臺實現相關的數據類型。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章