NSString是一个不可变数据类型,对象创建后它的内容不能修改,实现了NSCopy和NSCoding协议。NSString有一个可变的子类:NSMutableString
以下为NSString具体使用方法
//创建文件保存路径
_savepath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.plist" ];
//NSArray 有序的,可重复的
NSArray *arrtemp = @[@12 ,@43 ,@"test"];
//创建一个元素的数组对象
arrtemp = [NSArray arrayWithObject:@"array1"];
//创建多个元素的数组对象
arrtemp = [NSArray arrayWithObjects:@"test1",@"test2",@"test3",@"test4",@"test5",nil];
// arrtemp = [[NSArray alloc] initWithObjects:@"test1",@"test2", nil];
//把数组持久化
//从给定文件路径创建数组对象
[arrtemp writeToFile:_savepath atomically:YES];
NSArray *arrtemp2 = [[NSArray alloc] initWithContentsOfFile:_savepath];
NSLog(@"arrtemp2 = %@",arrtemp2);
//数组的归档
//归档:NSCoding协议
[NSKeyedArchiver archiveRootObject:arrtemp2 toFile:_savepath];
//解归档
NSArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithFile:_savepath];
NSLog(@"newArry =%@",newArray);
// NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arrtemp2];
//大小不可变,不能进行增删操作
NSInteger intIndex = [newArray indexOfObject:@"test2"];
if(intIndex !=NSNotFound){
NSString *str = [newArray objectAtIndex:intIndex];
NSLog(@"str =%@",str);
}
//数组的遍历
//for循环
for(int i=0;i<[newArray count];i++){
NSLog(@"index:%d,value:%@",i,newArray[i]);
}
//快速枚举
// for in
for (NSString *strtemp in newArray){
NSLog(@"for in value:%@",strtemp);
}
//使用block枚举数组中的元素
[newArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"block: index:%ld,obj=%@",idx,obj);
if(idx==3)
*stop = YES;
}];
//Reverse
[newArray enumerateObjectsWithOptions:(NSEnumerationReverse) usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"reverse block:index:%ld,obj:%@",idx,obj);
//枚举遍历数组中的元素
//NSEnumerator
NSEnumerator *enumer = [arrNumber objectEnumerator];
id obj;
while (obj =[enumer nextObject]){
NSLog(@"%@",obj);
}
}];
//数组排序
NSArray *arrNumber = @[@22,@12,@36,@78,@20];
//排序使用@selector
NSArray *sortedNumber = [arrNumber sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"an=%@",sortedNumber);
//排序使用Block
NSArray *sortedNumber2 = [arrNumber sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if([obj1 integerValue]>[obj2 integerValue]){
return NSOrderedAscending;
}
return NSOrderedDescending;
}];
NSLog(@"sortedNumber2 =%@",sortedNumber2);
//NSMutableArray 可变数组
NSMutableArray *mutArray = [NSMutableArray arrayWithArray:@[@12]];
mutArray = [NSMutableArray array];
mutArray = [NSMutableArray arrayWithObjects:@"test1",@"test2", nil];
//实例方法
mutArray = [[NSMutableArray alloc] initWithArray:@[@"test1",@"test3",@"test4",@"test5"]];
NSLog(@"mutArray = %@",mutArray);
//可变数组插入,删除,替换
[mutArray insertObject:@"test2" atIndex:1];
NSLog(@"mu = %@",mutArray);
//删除
[mutArray removeObject:@"test3"];
NSLog(@"m = %@",mutArray);
//替换
[mutArray replaceObjectAtIndex:1 withObject:@"test0"];
NSLog(@"mut = %@",mutArray);
//替换效果与replace相似,只能用于可变数组
mutArray[2] =@"test6";
NSLog(@"m2 = %@",mutArray);
[mutArray removeAllObjects];
NSLog(@"all = %@",mutArray);
#endif
NSArray *arr = @[@1,@34,@12,@22,@15,@68];
NSArray *arr1 = [arr objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 4)]];
NSLog(@"arr = %@",arr1);
}