記錄--
下面的例子以
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"jonery",@"stany", nil];
1乐纸、獲取數(shù)組中總共有多少個對象哈垢。
- (NSUInteger)count;
NSLog(@"%d",[array count]);2
2钠四、獲取數(shù)組中下標對應(yīng)的元素對象.(下標是從0開始)
- (id)objectAtIndex:(NSUInteger)index;
3冰沙、在當前數(shù)據(jù)中追加一個新的對象堵幽,并且返回一個新的數(shù)據(jù)對象(新的數(shù)組對象和被追加的對象叼屠,是兩個不同的數(shù)組對象)疯溺。
- (NSArray *)arrayByAddingObject:(id)anObject;
4论颅、在當前的數(shù)組中追加一個新的數(shù)據(jù)哎垦,并且返回一個新的數(shù)組對象。
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
5恃疯、使用當前的數(shù)組生成一個字符串漏设,新生成的字符串使用提供的separator 字符進行分割。
- (NSString *)componentsJoinedByString:(NSString *)separator;
[array compontsJoinedByString:@","];
運行結(jié)果:wendy,andy,tom,jonery,stany
6今妄、檢測數(shù)據(jù)中是否包含指定的對象元素
- (BOOL)containsObject:(id)anObject;
[array containsObject:@"tom"]; YES
7郑口、使用當前的數(shù)組生成字符串《芰郏可以重寫description 改變生成的字符串潘酗。相當于java 中的toString 方法。
- (NSString *)description;
運行結(jié)果
(
wendy,
andy,
tom,
jonery,
stany
)
8雁仲、根據(jù)設(shè)置的locale 進行連接數(shù)組
- (NSString *)descriptionWithLocale:(id)locale;
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
9仔夺、兩個數(shù)組的第一個元素是否相同,如果相同攒砖,則返回 數(shù)組中缸兔,第一個元素的字符串,反之吹艇,返回null 對象
- (id)firstObjectCommonWithArray:(NSArray *)otherArray;
10惰蜜、 從數(shù)組中獲取 NSRange 對象的數(shù)據(jù)存放到objects 中,NSRange 的數(shù)據(jù)標示從location,開始后面length 個數(shù)據(jù)
- (void)getObjects:(id__unsafe_unretained [])objects range:(NSRange)range;
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"jonery",@"stany",@"張山名稱",@"asdta", nil];
NSRange range = NSMakeRange(1, 5);
id *objects;
objects = malloc(sizeof(id) * range.length);
[array getObjects:objects range:range];
for(int i = 0; i < range.length; i++){
NSLog(@"%@",objects[i]);
}
free(objects);
運行的結(jié)果
andy
tom
jonery
stany
11受神、 判斷制定的anObject 對象是否存在數(shù)組中如果存在返回抛猖,對象所在的下標
- (NSUInteger)indexOfObject:(id)anObject;
如果不存在,返回的NSUInteger 與 NSNotFund 相同
NSUIndex index = [array indexOfObject:@"stan"];
if(index == NSNotFound)
{
對象不在數(shù)組中
}
11-1鼻听、 判斷制定的元素财著,是否在數(shù)組中,數(shù)組查詢的位置撑碴,是從range.location 的位置開始撑教,到range.length 的長度結(jié)束。
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;
如果數(shù)據(jù)存在醉拓,返回指定的下標伟姐,如果不存在,則返回NSNotFund 亿卤。
實質(zhì)是使用isEqual 進行比較
12愤兵、
同上面兩個方法一項,測試指定的對象是否在數(shù)組中不同的是排吴,這里使用指針進行比較
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject;
如果數(shù)據(jù)存在秆乳,返回指定的下標,如果不存在傍念,則返回NSNotFund 矫夷。
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range;
13葛闷、比較兩個數(shù)組是否相同 ,數(shù)組長度相同双藕,并且相同位置上的元素也相同淑趾。
- (BOOL)isEqualToArray:(NSArray *)otherArray;
14、返回最有一個元素忧陪,如果一個數(shù)組的長度為0 返回的對象為nil
- (id)lastObject;
15扣泊、使用數(shù)組返回一個 NSEnumerator 對象,這個對象類似與一個指針嘶摊,可以用來遍歷 整個數(shù)組 指針從前向后遍歷
- (NSEnumerator *)objectEnumerator;
示例如下
NSEnumerator *enu = [array objectEnumerator];
id *obj;
while (obj = enu.nextObject) {
NSLog(@"obj===%@==",obj);
}
16延蟹、 返回一個NSEnumerator 對象,這個對象類似一個指針叶堆,可以用來遍歷真?zhèn)€數(shù)據(jù)阱飘,所不同的是,這個指針虱颗,是從后向前遍歷沥匈。
- (NSEnumerator *)reverseObjectEnumerator;
17、生成一個NSData 的對象忘渔,主要是用來進行數(shù)組的排序高帖。 在下面的方法中使用這個對象
- (NSData *)sortedArrayHint;
18、 進行數(shù)組的排序
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id,id, void *))comparator context:(void *)context;
這個方法類似蘋果實現(xiàn)了一個簡單的 排序方法畦粮。但是實現(xiàn)的規(guī)則需要自己進行處理散址。
類似的方法如下。 首先提供一個 普通的排序算法宣赔,函數(shù)和c 的方法類似
NSInteger sortType(id st,id str,void *cha)
{
NSString *s1 = (NSString *)st;
NSString *s2 = (NSString *)str;
if(s1.length > s2.length)
{
return NSOrderedAscending;
}else if(s1.length < s2.length)
{
return NSOrderedDescending;
}
return NSOrderedSame;
}
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil];
NSArray *a = [array sortedArrayUsingFunction:sortType context:nil];
NSLog(@"a=%@",a);
NSArray 為需要排序的數(shù)組预麸,返回一個排序完成的數(shù)組,再執(zhí)行osrtedArrayUseingFunction 方法時會拉背,會自動調(diào)用上面的sortType 方法师崎,并且,可以按照你
的需要調(diào)整上面的規(guī)則
19椅棺、和上面的方法類似,也是蘋果用來進行排序的齐蔽。所不同的是两疚,需要傳入一個NSData 的數(shù)據(jù)。
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id,id, void *))comparator context:(void *)context hint:(NSData *)hint;
NSData *dat = [array sortedArrayHint];
NSArray *a = [array sortedArrayUsingFunction:sortType context:nil hint:dat];
NSLog(@"a=%@",a);
20含滴、- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
這是用來排序的函數(shù)诱渤,comparator 這個參數(shù),需要傳入一個返回結(jié)果是NSComparisonResult 的函數(shù)谈况,
主要的函數(shù)勺美,類似的函數(shù)如下:
- (NSComparisonResult)compare:(NSString *)string;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
- (NSComparisonResult)localizedCompare:(NSString *)string;
- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;
都可以進行調(diào)用
以 localizedCompare: 函數(shù)為例進行調(diào)用
NSArray *arr = [[NSArray alloc] initWithObjects:@"test", @"abc", @"xyz", nil];
NSLog(@"Befor sort:%@", arr);
SEL sel = @selector(localizedCompare:);
arr = [arr sortedArrayUsingSelector:sel];
NSLog(@"After sort:%@", arr);
得到的結(jié)果是:
abc,
test,
xyz
21递胧、用來獲取數(shù)組中range.location 開始,數(shù)據(jù)各數(shù) 為range.length 的數(shù)據(jù)赡茸,并放置到一個新的數(shù)組中
以數(shù)組 為例
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil];
- (NSArray *)subarrayWithRange:(NSRange)range;
如:
NSArray *test = [array subarrayWithRange:NSMakeRange(2, 2)];
tom,
test
注意range 的數(shù)值不要越界缎脾。
22、寫入數(shù)組中的數(shù)據(jù)占卧,到指定path 的目錄中:
參數(shù):atomically 是否把文件保存到輔助文件中
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
23遗菠、如同上面的方法一樣,所不同的是寫入數(shù)組中的內(nèi)容到 網(wǎng)上指定的路徑华蜒。
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
24辙纬、這個方法的實現(xiàn)類似于,數(shù)組中的元素叭喜,都是類對象贺拣,aselector 是這些類中的無參方法。
- (void)makeObjectsPerformSelector:(SEL)aSelector;
調(diào)用例子如下:
首先新建一個ObjectTest 的類捂蕴,在其中實現(xiàn)一個 無參數(shù)方法 - (void)tttttt
在這個方法的實現(xiàn)中可以打印一些日志
- (void)tttttt
{
NSLog(@"==========asasdfasdfasdfas===========");
}
NSArray *array = [NSArray arrayWithObjects:[[[ObjectTest alloc] init] autorelease],[[[ObjectTest alloc] init] autorelease], nil];
調(diào)用格式如下譬涡,
[array makeObjectsPerformSelector:@selector(tttttt)];
這時就可以看到打印的日志信息了。
25启绰、這個方法的調(diào)用和上面一個方法類似昂儒,所不同的是這個對象調(diào)用的方法是一個可以帶參數(shù)的方法。參數(shù)的類型是id ,也就是可以是任意的類型委可。
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
26渊跋、用來根據(jù)indexes 獲取一個數(shù)組, NSIndexSet 是一個用來管理 index 的對象着倾。
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes;
例子如下:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil];
//NSIndexSet *se = [NSIndexSet indexSetWithIndex:0];
或者是
NSIndexSet *se = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)];
NSArray *test = [array objectsAtIndexes:se];
NSLog(@"%@",test);
27拾酝、返回指定下標的一個對象。這個方法類似 objectAtIndex:
- (id)objectAtIndexedSubscript:(NSUInteger)idx
28卡者、使用block 塊遍歷整個數(shù)組蒿囤。這個block 需要三個參數(shù),id obj 表示數(shù)組中的元素崇决。
NSUInteger idx 標示元素的下標材诽,
bool *stop 是一個bool類型的參數(shù)。 官方描述如下:
A reference to a Boolean value. The block can set the value to YES to stop further processing of the array.
The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx,BOOL *stop))block
調(diào)用例子如:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil];
[array enumerateObjectsUsingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
29恒傻、同上面的方法一項脸侥,區(qū)別在于,這里多添加了一個參數(shù)盈厘,用來標示 是從前向后遍歷睁枕,還是從后往前遍歷。
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx,BOOL *stop))block
調(diào)用例子如下:
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test", nil];
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
30、同上面的方法一項外遇,不過NSIndexSet 參數(shù)標示注簿,根據(jù)下標取出的數(shù)組,這里真正在block 中遍歷的數(shù)組跳仿,是根據(jù)NSindexSet 取到的子數(shù)組
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx,BOOL *stop))block
調(diào)用如下:
[array enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)] options:NSEnumerationReverse usingBlock:^(id str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
31诡渴、 根據(jù)條件用來獲取一個NSUIndex 對象,主要是根據(jù)條件進行數(shù)據(jù)遍歷使用
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate
調(diào)用如下:
NSInteger index = [array indexOfObjectPassingTest:^ BOOL (id tr,NSUInteger index, BOOL *te){
NSString *s = (NSString *)tr;
if([@"wendy" isEqualToString:s])
{
return YES;
}
return NO;
}];
NSLog(@"index==%d=.",index);
32塔嬉、同上面的方法相同玩徊,卻別在于,這里添加了一個參數(shù)谨究,用來表示遍歷是從前向后遍歷還是從后遍歷恩袱。
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate
33、這個添加了參數(shù)NSIntexSet 參數(shù)胶哲,用來獲取子數(shù)組畔塔,然后使用這個子數(shù)組進行遍歷,處理數(shù)據(jù)
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate
31鸯屿、 根據(jù)block 的處理獲取一個NSIndexSet 對象澈吨。
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate
調(diào)用如下:
NSIndexSet *index = [array indexesOfObjectsPassingTest: ^ BOOL (id tr, NSUInteger index,BOOL *te){
NSString *s = (NSString *)tr;
if([s isEqualToString:@"andy"]){
return YES;
}
return NO;
}];
NSLog(@"%@",index);
33 、 這個方法添加了參數(shù)寄摆,用來表示谅辣,是從前向后,遍歷還是從后向前遍歷
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate NS_AVAILABLE(10_6,4_0);
調(diào)用示例如下:
NSIndexSet *index = [array indexesOfObjectsWithOptions:NSEnumerationReverse passingTest: ^ BOOL (id tr, NSUInteger index,BOOL *te){
NSString *s = (NSString *)tr;
if([s isEqualToString:@"andy"]){
return YES;
}
return NO;
}];
NSLog(@"%@",index);
34婶恼、 添加參數(shù)NSIndexSet 用來獲取子數(shù)組桑阶,使用子數(shù)組進行遍歷
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate
35、對數(shù)組進行排序操作參數(shù)cmptr 是一個block 函數(shù)塊勾邦,返回的數(shù)據(jù)類型是一個NSComparisonResult 對象
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
調(diào)用例子如下:
NSArray *te = [array sortedArrayUsingComparator:^ NSComparisonResult (NSString *s,NSString *s2){
if(s.length < s2.length){
return NSOrderedAscending;
}
if(s.length > s2.length){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
NSLog(@"te=%@.",te);
36蚣录、進行排序操作,NSSortOptions 排序的參數(shù) 用來表示是同時排序眷篇,還是穩(wěn)定執(zhí)行萎河。
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6,4_0);
NSArray *test = [array sortedArrayWithOptions:NSSortStable usingComparator:^ NSComparisonResult (NSString *s,NSString *s2){
if(s.length < s2.length){
return NSOrderedAscending;
}
if(s.length > s2.length){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
NSLog(@"%@",test);
NSArray 數(shù)組的創(chuàng)建
1、使用類方法創(chuàng)建 一個空的數(shù)組
+ (id)array;
2蕉饼、使用類方法創(chuàng)建 只有一個對象的數(shù)組
+ (id)arrayWithObject:(id)anObject;
3虐杯、從 c 數(shù)組創(chuàng)建一個 NSarray數(shù)以cnt 不能超出數(shù)組的范圍。不然會有數(shù)據(jù)越界的異常
+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
id objects[10] = {@"abbb",@"bczdfasdf",@"casdfasdf",@"asdfasdf"};
NSArray *array = [NSArray arrayWithObjects:objects count:2];
NSLog(@"%@",array);
4昧港、使用后面的元素厦幅,創(chuàng)建一個數(shù)組
+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
5、array 創(chuàng)建一個新的數(shù)組
+ (id)arrayWithArray:(NSArray *)array;
6慨飘、使用 c 數(shù)組 創(chuàng)建一個數(shù)組。
- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt;
7、使用objects 創(chuàng)建數(shù)組
- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
8瓤的、使用一個array 創(chuàng)建一個數(shù)組
- (id)initWithArray:(NSArray *)array;
9休弃、使用array 創(chuàng)建一個數(shù)組,后面的標識是 是否拷貝原來的元素
flag 如果是YES, 數(shù)組中每個元素圈膏,將引用copywithzone塔猾。
- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag;
10、讀取文件創(chuàng)建一個數(shù)組稽坤,
+ (id)arrayWithContentsOfFile:(NSString *)path;
11丈甸、使用URL 穿件一個數(shù)組,這個URL可以是本地的文件路徑尿褪,也可是是網(wǎng)絡(luò)上的內(nèi)容
+ (id)arrayWithContentsOfURL:(NSURL *)url;
12睦擂、讀取文件創(chuàng)建一個數(shù)組,
- (id)initWithContentsOfFile:(NSString *)path;
13杖玲、使用URL 穿件一個數(shù)組顿仇,這個URL可以是本地的文件路徑,也可是是網(wǎng)絡(luò)上的內(nèi)容
- (id)initWithContentsOfURL:(NSURL *)url;
1摆马、 向數(shù)組中添加一個對象
- (void)addObject:(id)anObject;
2臼闻、向數(shù)組中指定的index 位置,插入一個新的對象
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
3囤采、移除數(shù)組的最后一個元素
- (void)removeLastObject;
4述呐、移除指定為指定位置的元素
- (void)removeObjectAtIndex:(NSUInteger)index;
5、使用anObject 替換 下標為 index 位置上的元素
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
6蕉毯、使用一個數(shù)組給當前的數(shù)組添加元素
- (void)addObjectsFromArray:(NSArray *)otherArray;
7乓搬、交換指定 index1 和 index2 兩個位置上的元素
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
8、 移除數(shù)組中的所有元素
- (void)removeAllObjects;
9恕刘、使用anObject 對象替換 range 位置上的元素缤谎,
相當于刪除 range位置的元素,然后在把 anobject 插入到這個位置
- (void)removeObject:(id)anObject inRange:(NSRange)range;
10褐着、如果指定的元素坷澡,如果元素不存在,則不移除
- (void)removeObject:(id)anObject;
11含蓉、 同9 相同
- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range;
12频敛、方法內(nèi)容 和9 相同
- (void)removeObjectIdenticalTo:(id)anObject;
13、不建議使用
- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);
14馅扣、移除給定數(shù)組中的元素
- (void)removeObjectsInArray:(NSArray *)otherArray;
15斟赚、移除指定range上的所有元素
- (void)removeObjectsInRange:(NSRange)range;
16、使用otherArray 數(shù)組中 otherRange 位置上的元素差油,替換當前數(shù)組中 range 位置上的元素
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
17 拗军、 使用otherArray 數(shù)組上的位置任洞,替換 range 上的元素
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;
18、對當前的數(shù)組排序发侵,使用排序算法
- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;
19交掏、對當前的數(shù)組排序,使用排序算法
- (void)sortUsingSelector:(SEL)comparator;
20刃鳄、在indexes 的位置上盅弛,插入一個數(shù)組
- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
21、移除制定indexes 位置上的元素
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
22叔锐、使用一個對象數(shù)組挪鹏,替換 indexes 位置上的 元素
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;
23
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
24、排序
- (void)sortUsingComparator:(NSComparator)cmptr
25愉烙、 使用后面的元素進行排序
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
25讨盒、 創(chuàng)建NSMutableArray 數(shù)組
+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;