出于事業(yè)的發(fā)展筒繁,3月中旬離職了噩凹,離職后,一直在找工作毡咏,這里做一下面試的總結(jié)驮宴。
一、問:任何開發(fā)語言寫一種排序呕缭,并寫出其時間復(fù)雜度
答:建議看看下面這篇文章堵泽,我當(dāng)時寫了個冒泡排序,時間復(fù)雜度為O(n^2)
http://blog.sina.com.cn/s/blog_77795cad01011txt.html
二恢总、問:atomic和nonatomic的區(qū)別迎罗,默認(rèn)是哪個?
答:atomic:默認(rèn)是有該屬性的片仿,這個屬性是為了保證程序在多線程情況下纹安,編譯器會自動生成一些互斥加鎖代碼,避免該變量的讀寫不同步問題砂豌。
nonatomic:如果該對象無需考慮多線程的情況厢岂,請加入這個屬性,這樣會讓編譯器少生成一些互斥加鎖代碼阳距,可以提高效率塔粒。
默認(rèn)是atomic的。
三筐摘、問:修改下列代碼中的錯誤之處
// .h中
@interface Teacher : NSObject
@property (nonatomic, strong) NSMutableArray *students;
@end
@interface Student : NSObject
@property (nonatomic, strong) Teacher *teacher;
@end
// viewDidLoad中
Teacher *teacher = [[Teacher alloc] init];
teacher.students = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
Student *student = [[Student alloc] init];
student.teacher = teacher;
[teacher.students addObject:student];
}
答:這里面主要考的是互相引用的問題卒茬,可以通過兩種方式進(jìn)行修改
第一種:
Teacher *teacher = [[Teacher alloc] init];
teacher.students = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
Student *student = [[Student alloc] init];
// 弱引用
__weak Teacher *weakTeacher = teacher;
student.teacher = weakTeacher;
[teacher.students addObject:student];
}
第二種:
// 修改Student對象的teacher屬性為weak引用
@property (nonatomic, weak) Teacher *teacher;
四、問:如下代碼的輸出
NSString *str1 = @"nick";
NSString *str2 = @"nick";
if (str1 == str2) {
NSLog(@"相等");
} else {
NSLog(@"不相等");
}
答:輸出"相等"咖熟,==比較的是指針指向的地址是否相同扬虚,這里相同字符串的地址是相同的
五、問:Application的幾種狀態(tài)
答:1球恤、應(yīng)用程序的狀態(tài)
狀態(tài)如下:
Not running 未運(yùn)行 程序沒啟動
Inactive 未激活 程序在前臺運(yùn)行,不過沒有接收到事件荸镊。在沒有事件處理情況下程序通常停留在這個狀態(tài)
Active 激活 程序在前臺運(yùn)行而且接收到了事件咽斧。這也是前臺的一個正常的模式
Backgroud 后臺 程序在后臺而且能執(zhí)行代碼,大多數(shù)程序進(jìn)入這個狀態(tài)后會在在這個狀態(tài)上停留一會躬存。時間到之后會進(jìn)入掛起狀態(tài)(Suspended)张惹。有的程序經(jīng)過特殊的請求后可以長期處于Backgroud狀態(tài)
Suspended 掛起 程序在后臺不能執(zhí)行代碼。系統(tǒng)會自動把程序變成這個狀態(tài)而且不會發(fā)出通知岭洲。當(dāng)掛起時宛逗,程序還是停留在內(nèi)存中的,當(dāng)系統(tǒng)內(nèi)存低時盾剩,系統(tǒng)就把掛起的程序清除掉雷激,為前臺程序提供更多的內(nèi)存替蔬。
下圖是程序狀態(tài)變化圖:
各個程序運(yùn)行狀態(tài)時代理的回調(diào):
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions 告訴代理進(jìn)程啟動但還沒進(jìn)入狀態(tài)保存
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 告訴代理啟動基本完成程序準(zhǔn)備開始運(yùn)行
- (void)applicationWillResignActive:(UIApplication *)application 當(dāng)應(yīng)用程序?qū)⒁敕腔顒訝顟B(tài)執(zhí)行,在此期間屎暇,應(yīng)用程序不接收消息或事件承桥,比如來電話了
- (void)applicationDidBecomeActive:(UIApplication *)application 當(dāng)應(yīng)用程序入活動狀態(tài)執(zhí)行,這個剛好跟上面那個方法相反
- (void)applicationDidEnterBackground:(UIApplication *)application 當(dāng)程序被推送到后臺的時候調(diào)用根悼。所以要設(shè)置后臺繼續(xù)運(yùn)行凶异,則在這個函數(shù)里面設(shè)置即可
- (void)applicationWillEnterForeground:(UIApplication *)application 當(dāng)程序從后臺將要重新回到前臺時候調(diào)用,這個剛好跟上面的那個方法相反挤巡。
- (void)applicationWillTerminate:(UIApplication *)application 當(dāng)程序?qū)⒁顺鍪潜徽{(diào)用剩彬,通常是用來保存數(shù)據(jù)和一些退出前的清理工作。這個需要要設(shè)置UIApplicationExitsOnSuspend的鍵值矿卑。
- (void)applicationDidFinishLaunching:(UIApplication*)application 當(dāng)程序載入后執(zhí)行
六喉恋、問:__block和__weak的區(qū)別
答:API Reference對__block變量修飾符有如下幾處解釋:
//A powerful feature of blocks is that they can modify variables in the same lexical scope. You signal that a block can modify a variable using the __block storage type modifier.
//At function level are __block variables. These are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap.
大概意思?xì)w結(jié)出來就是兩點(diǎn):
1.__block對象在block中是可以被修改、重新賦值的粪摘。
2.__block對象在block中不會被block強(qiáng)引用一次瀑晒,從而不會出現(xiàn)循環(huán)引用問題。
API Reference對__weak變量修飾符有如下幾處解釋:
__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil whenthere are no strong references to the object.
使用了__weak修飾符的對象徘意,作用等同于定義為weak的property苔悦。自然不會導(dǎo)致循環(huán)引用問題,因?yàn)樘O果文檔已經(jīng)說的很清楚椎咧,當(dāng)原對象沒有任何強(qiáng)引用的時候玖详,弱引用指針也會被設(shè)置為nil。
因此勤讽,__block和__weak修飾符的區(qū)別其實(shí)是挺明顯的:
- __block不管是ARC還是MRC模式下都可以使用蟋座,可以修飾對象,還可以修飾基本數(shù)據(jù)類型脚牍。
- __weak只能在ARC模式下使用向臀,也只能修飾對象(NSString),不能修飾基本數(shù)據(jù)類型(int)诸狭。
-
__block對象可以在block中被重新賦值券膀,__weak不可以。
PS:__unsafe_unretained修飾符可以被視為iOS SDK 4.3以前版本的__weak的替代品驯遇,不過不會被自動置空為nil芹彬。所以盡可能不要使用這個修飾符。