NSArray和NSMutableArray对象的使用
/* 初始化方法:
??? 1.init返回一個(gè)空數(shù)組
???? 2.initWithArray從已有數(shù)組初始化
???? 3.initWithContentsOfFile//從plist文件加載
???? 4.initWithContentsOfUrl//從網(wǎng)絡(luò)地址上獲取
???? 5.initWithObject用一個(gè)對(duì)象初始化
???? 6.initWithObjects從多對(duì)象初始化
? ?
??? self.theDataArray=[[NSMutableArray alloc]initWithCapacity:5];//指定有五個(gè)元素初始化
???????????????????????????? ?
???? */??? ?
?? ?
??? /*打印第一個(gè)元素:
???? NSLog(@"the object? is:%@",[theDataArray firstObjectCommonWithArray:theDataArray]);
?? */
??? /*打印最后一個(gè)元素:
??? NSLog(@"the object? is:%@",[theDataArray lastObject]);
??? */
????? /*枚舉所有元素,方法一:
?????? for (NSString * theStr in theDataArray) {
???????? NSLog(@"%@:",theStr);
??????? }
??? */
??? /*枚舉所有元素,方法二:
??????? for (int i=0,i<[theDataArray count],i++) {
?????? //NSLog(@"%@:",[theDataArray objectAtIndex:i]);
????????? }
????? */
?? /* 枚舉所有元素,方法三,用枚舉器:
??? NSEnumerator *enumerator=[theDataArray objectEnumerator];
???? id obj;
???? while (obj =[enumerator nextObject]) {
??????? NSLog(@"%@",obj);
?? }
??? */
?? ?
???? /*? //添加元素
??? [theDataArray addObject:@"這是新添加的元素"];//從最后開(kāi)始添加
?????? */
?? ?
??? /* //從指定索引插入元素
???? [theDataArray insertObject:@"this is inerted object" atIndex:0];//是插入到指定 索引的前面
???? */
?? ?
?? ?
??? /* //修改.更新元素
???? [theDataArray replaceObjectAtIndex:0 withObject:@"new obj"];//指定索引修改
???? */
?? ?
??? /* //判斷數(shù)組是否包含某個(gè)對(duì)象
??? ?
??? if ([theDataArray containsObject:@"selectiont"]) {
??????? NSLog(@"the object selection is contained in array");
??? }
??? else{
??????? NSLog(@"not contain");
??? }
??? */
??? /* //獲取元素索引
??? NSLog(@"the idx is:%i",[theDataArray indexOfObject:@"selection"]);
??? */
?? ?
??? /*//數(shù)組元素排序
??? ?
???? 方法一:
???? ?
? ?
???
NSArray *theTempArr=[[NSArray alloc] initWithArray:[theDataArray
sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
?? */
??? //[theDataArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];? // or @selector(compare:)
?? ?
???/*//數(shù)組元素排序
???? 方法二:
??? ?
???? NSLog(@"before sorted array:%@",theDataArray);
??? NSCountedSet *cset=[[NSCountedSet alloc] initWithArray:theDataArray];
?? ?
???? NSArray *theTempArr=[[NSArray alloc] initWithArray:[[cset allObjects]sortedArrayUsingSelector:@selector(compare:)]];
?????
??? NSLog(@"after sorted array:%@",theTempArr);
?????? */
?? /* //對(duì)數(shù)組對(duì)象進(jìn)行排序?? NSSortDescriptor
??? NSSortDescriptor *sortDescriptor=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];//@“name”是對(duì)象屬性
??? [theDataArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];//返回排序好的數(shù)組
?? //還可以用自定義方法:[theDataArray sortUsingSelector:@selector(custom Method:)];
-(NSInteger)customMethod:(someObject *)otherObj{
?? NSCompareResult compareResult=[self.name compare:otherObj.name];
? if(compareResult == NSOrderedSame)? return 0;
if(compareResult == NSOrderedAscending)? return 1;
? else return -1;
}
*/
/*? //對(duì)象數(shù)組過(guò)濾(NSPredicate)
NSArray? *keyArray=[[NSArray alloce]initWtihObjects:@"A",@"B",nil];
? for (NSString *key in keyArray)
???? {
? //定義規(guī)則:
???
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"SELF.name
beginswith[c]? %@",key]; //SELF.name是對(duì)象屬性,beginswith[c]? %@",key
表示選出name以key開(kāi)頭的對(duì)象
? //或者 :
NSPredicate *apredicate=[NSPredicate
predicateWithFormat:@"SELF.name contains [c]? %@",key]; //contains[c]?
%@",key 表示選出name中包含有key的對(duì)象
? //或者 :
NSInteger age=20;
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"SELF.age >? %i",age]; // 表示選出age>20的對(duì)象
? //或者 :
NSString * matchStr=@"hel*";
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"SELF.name? like %@",matchStr]; // 表示選出name中包含與字符串matchStr相似的對(duì)象
??? NSArray *newArr=[theDataArray filteredArrayUsingPredicate:apredicate];
???????? [theDataDict setValue:newArr forKey:key];
?????? }
*/
/*? //刪除數(shù)組元素
NSMutableArray *tempArray=[[NSMutableArray alloc]initWithObjects:@"one",@"tow",@"threr",nil];
?[tempArray removeObjectAtIndex:0];//從指定索引移除
?[tempArray removeAllObjects];//移除所有元素
? [tempArray removeLastObject];//移除最后那個(gè)元素
? [tempArray removeObjectsInArray:newArray];//移除指定數(shù)組中元素
?? [tempArray? removeObjectsAtIndexes: NSIndexSet *__strong)];//從所選擇的索引中移除
*/
轉(zhuǎn)載于:https://www.cnblogs.com/tony_cap/archive/2012/04/15/2450536.html
總結(jié)
以上是生活随笔為你收集整理的NSArray和NSMutableArray对象的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: dos命令行输入adb shell命令为
- 下一篇: EAS BOS 发布