close

overview_systemlayers.jpg
iOS的開發說老實真的是博大精深,
也只有不斷接觸才會不斷有所心得,

況且Apple也不斷對iOS SDK推陳出新,

讓開發者得不中斷研究完全沒有到頂的機會XDD(誤)


市面上已經有很多介紹iOS開發的叢書,
所以本系列柯南想隨性一點寫
如果有寫不對的地方或者需要補充的地方,
也歡迎大家在底下留言分享。
整體來說一個app的主畫面的構成多少都跟UIViewController這個class相關,
但是一些基本用來儲存或傳遞資料的結構也很重要。

例如NSArrayNSMutableArray
還有NSDictionaryNSMutableDictionary

關於NSArray與NSDictionary的分別

我覺得以下在網路上看到的說明寫得蠻容易懂的。

NSArray is more appropriate when you have a bunch of objects that are all the same type, and you're likely to loop through them in order. For example, a list of books could be put in an NSArray- book[0] is one book, book[1] is another book.


NSDictionary is more appropriate when you have objects (sometimes of different types) that you want to access by keys; often out of order. For example, an individual book could be represented as a Dictionary, where the key "sold" refers to a NSNumber for the number sold, and the key "Title" points to an NSString for the title of the book.

基本上NSArray主要一個主打單純儲存物件,
NSDictionay主打儲存Key-value形式的物件。

至於NSMutableArray與NSMutableDictionay和上述兩種又有什麼不同呢?
基本上就是static跟dynamic的分別,
NSArray與NSDictionary都是一宣告就固定長度,
而NSMutableArrat與NSMutableDictionay則可以變動長度。

最後要跟大家介紹的是NSEnumerator這個Class,
使用類似下方的程式碼,
就可以取出NSArray, NSMutableArray, NSDictionary與NSMutableDictionary中的物件了,

NSEnumerator *enumerator = [myMutableDict keyEnumerator];
id aKey
= nil;
while ( (aKey = [enumerator nextObject]) != nil) {
    id value
= [myMutableDict objectForKey:anObject];
   
NSLog(@"%@: %@", aKey, value);
}

Technorati 的標籤:iOS, development, objectiveC, NSArray, NSDictionay


arrow
arrow
    全站熱搜

    賽拉維‧柯南 發表在 痞客邦 留言(0) 人氣()