黑馬程序員——黑蘋果的日記(7)——Foundation(2)


------<<a href="http://www.itheima.com" _xhe_href="http://www.itheima.com" "="" target="_blank">點擊打開鏈接 點擊打開鏈接>Java培訓、Android培訓、iOS培訓、.Net培訓</a>、期待與您交流! -------



1 NSArray的創建方式:
+ (instancetype)array;
+ (instancetype)arrayWithObject:(id)anObject;
+ (instancetype)arrayWithObjects:(id)firstObj, ...;
+ (instancetype)arrayWithArray:(NSArray *)array;
@[item1, item2, item3, ......];
--------------- 根據文件來創建 ----------------

+ (id)arrayWithContentsOfFile:(NSString *)path; // 讀取一個xml文件. 

+ (id)arrayWithContentsOfURL:(NSURL *)url; // 讀取一個xml文件.


2 NSArray的特點:
* 只能存放OC對象, 有順序
* 不能存儲非OC對象
* 它是不可變的

* NSArray直接使用NSLog()

作爲字符串輸出時是小括號括起來的形式。

* NSArray中不能存儲nil

3 NSArray的常見方法:

- (NSUInteger)count;

 獲取集合元素個數

- (id)objectAtIndex:(NSUInteger)index;
獲得index位置的元素, 等價於array[index];
- (BOOL)containsObject:(id)anObject;
是否包含某一個元素

- (id)lastObject; 

返回最後一個元素

- (id)firstObject; 

返回最後一個元素

- (NSUInteger)indexOfObject:(id)anObject;
查找anObject元素在數組中的位置(如果找不到,返回-1)NSNotFound
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;
在range範圍內查找anObject元素在數組中的位置
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
4  NSArray遍歷元素

普通遍歷
for (int i = 0; i<array.count; i++) { }
快速遍歷
for (id obj in array) { }
Block遍歷
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
 }];

5 將一個NSArray保存到文件中
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

6 字符串拼接, 字符串(NSString)的方法:

- (NSString *)componentsJoinedByString:(NSString *)separator; 

7  字符串分割, 數組(NSArray)的方法:

- (NSArray *)componentsSeparatedByString:(NSString *)separator; 


8  NSMutableArray的特點:
 NSMutableArray是NSArray的子類

 NSMutableArray是可變的

NSArray是不可變的

 創建一個空的NSMutableArray: NSMutableArray *array = [NSMutableArray array];


9  NSMutableArray的常見方法:

- (void)addObject:(id)object;

 添加一個元素

- (void)addObjectsFromArray:(NSArray *)array;

 添加otherArray的全部元素到當前數組中

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;

 在index位置插入一個元素

- (void)removeLastObject;

 刪除最後一個元素

- (void)removeAllObjects;

 刪除所有的元素

- (void)removeObjectAtIndex:(NSUInteger)index; 

刪除index位置的元素

- (void)removeObject:(id)object; 

刪除特定的元素

- (void)removeObjectsInRange:(NSRange)range; 

刪除range範圍內的所有元素

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; 

用anObject替換index位置對應的元素, 等價於array[index] = anObject;

- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

 交換idx1和idx2位置的元素


10 NSDictionary和 NSMutableDictionary
1> NSDictionary是不可變的
2> NSDictionary是"鍵值對"的方式保存數據。

3> NSDictionary的創建方式: 

+ (instancetype)dictionary;

+ (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;

 // objectForKey,根據鍵取值。

+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;

 @{@"zs" : @"zhangsan", @"ls" : @"lisi", @"ww" : @"Wangwu"};

4> NSDictionary的常見方法:
- (NSUInteger)count; 返回字典的鍵值對數目

- (id)objectForKey:(id)aKey;
根據key取出value, 等價於: dict[aKey];

* 快速遍歷
for (NSString *key in dict) { }
* Block遍歷
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
 }];

5> 字典內容到文件:
* 將字典寫入文件中

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; 

- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

* 將文件中的數據讀取到字典中:

+ (id)dictionaryWithContentsOfFile:(NSString *)path; 

+ (id)dictionaryWithContentsOfURL:(NSURL *)url;


6> NSMutableDictionary

* NSMutableDictionary是NSDictionary的子類
* NSDictionary是不可變的
* NSMutableDictionary是可變的


7> NSMutableDictionary的常見操作

- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey; 

添加一個鍵值對(會把aKey之前對應的值給替換掉), 等價於: dict[@"name"] = @"Jack";

- (void)removeObjectForKey:(id)aKey; 

通過aKey刪除對應的value

- (void)removeAllObjects;

 刪除所有的鍵值對

通過dictionary屬性快速初始化一個NSMutableDictionary dict.dictionary = @{@"name" : @"bob", @"age" : @"18"};



8>   NSArray和NSDictionary的區別
8.1>  NSArray是有序的,NSDictionary是無序的

 NSArray是通過下標訪問元素,NSDictionary是通過key訪問元素


8.2>  NSArray的用法
 創建
@[@"Jack", @"Rose"] (返回是不可變數
組)
 訪問
id d = array[1];
 賦值

array[1] = @"jack";


* NSDictionary的用法
  創建
@{ @"name" : @"Jack", @"phone" : @"10086" } (返回是不可變字典)
   訪問
id d = dict[@"name"];
   賦值
dict[@"name"] = @"jack";



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