記錄--
下面的例子以
NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"jonery",@"stany", nil];
1、獲取數(shù)組中總共有多少個對象。
- (NSUInteger)count;
NSLog(@"%d",[array count]);2
2、獲取數(shù)組中下標(biāo)對應(yīng)的元素對象.(下標(biāo)是從0開始)
- (id)objectAtIndex:(NSUInteger)index;
3、在當(dāng)前數(shù)據(jù)中追加一個新的對象,并且返回一個新的數(shù)據(jù)對象(新的數(shù)組對象和被追加的對象,是兩個不同的數(shù)組對象)。
- (NSArray *)arrayByAddingObject:(id)anObject;
4、在當(dāng)前的數(shù)組中追加一個新的數(shù)據(jù),并且返回一個新的數(shù)組對象。
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;
5、使用當(dāng)前的數(shù)組生成一個字符串,新生成的字符串使用提供的separator 字符進(jìn)行分割。
- (NSString *)componentsJoinedByString:(NSString *)separator;
[array compontsJoinedByString:@","];
運(yùn)行結(jié)果:wendy,andy,tom,jonery,stany
6、檢測數(shù)據(jù)中是否包含指定的對象元素
- (BOOL)containsObject:(id)anObject;
[array containsObject:@"tom"]; YES
7、使用當(dāng)前的數(shù)組生成字符串。可以重寫description 改變生成的字符串。相當(dāng)于java 中的toString 方法。
- (NSString *)description;
運(yùn)行結(jié)果
(
wendy,
andy,
tom,
jonery,
stany
)
8、根據(jù)設(shè)置的locale 進(jìn)行連接數(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ù)標(biāo)示從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);
運(yùn)行的結(jié)果
andy
tom
jonery
stany
11、 判斷制定的anObject 對象是否存在數(shù)組中如果存在返回,對象所在的下標(biāo)
- (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ù)存在,返回指定的下標(biāo),如果不存在,則返回NSNotFund 。
實質(zhì)是使用isEqual 進(jìn)行比較
12、
同上面兩個方法一項,測試指定的對象是否在數(shù)組中不同的是,這里使用指針進(jìn)行比較
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject;
如果數(shù)據(jù)存在,返回指定的下標(biāo),如果不存在,則返回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 的對象,主要是用來進(jìn)行數(shù)組的排序。 在下面的方法中使用這個對象
- (NSData *)sortedArrayHint;
18、 進(jìn)行數(shù)組的排序
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id,id, void *))comparator context:(void *)context;
這個方法類似蘋果實現(xiàn)了一個簡單的 排序方法。但是實現(xiàn)的規(guī)則需要自己進(jìn)行處理。
類似的方法如下。 首先提供一個 普通的排序算法,函數(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、和上面的方法類似,也是蘋果用來進(jìn)行排序的。所不同的是,需要傳入一個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;
都可以進(jìn)行調(diào)用
以 localizedCompare: 函數(shù)為例進(jìn)行調(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、返回指定下標(biāo)的一個對象。這個方法類似 objectAtIndex:
- (id)objectAtIndexedSubscript:(NSUInteger)idx
28、使用block 塊遍歷整個數(shù)組。這個block 需要三個參數(shù),id obj 表示數(shù)組中的元素。
NSUInteger idx 標(biāo)示元素的下標(biāo),
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ù),用來標(biāo)示 是從前向后遍歷,還是從后往前遍歷。
- (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ù)標(biāo)示,根據(jù)下標(biāo)取出的數(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ù)條件進(jìn)行數(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ù)組進(jìn)行遍歷,處理數(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ù)組進(jìn)行遍歷
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL *stop))predicate
35、對數(shù)組進(jìn)行排序操作參數(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、進(jìn)行排序操作,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ù)組,后面的標(biāo)識是 是否拷貝原來的元素
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 替換 下標(biāo)為 index 位置上的元素
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
6、使用一個數(shù)組給當(dāng)前的數(shù)組添加元素
- (void)addObjectsFromArray:(NSArray *)otherArray;
7、交換指定 index1 和 index2 兩個位置上的元素
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
8、 移除數(shù)組中的所有元素
- (void)removeAllObjects;
9、使用anObject 對象替換 range 位置上的元素,
相當(dāng)于刪除 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 位置上的元素,替換當(dāng)前數(shù)組中 range 位置上的元素
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
17 、 使用otherArray 數(shù)組上的位置,替換 range 上的元素
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;
18、對當(dāng)前的數(shù)組排序,使用排序算法
- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;
19、對當(dāng)前的數(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、 使用后面的元素進(jìn)行排序
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
25、 創(chuàng)建NSMutableArray 數(shù)組
+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;