三种方法创建字符串
int main(int argc, const char * argv[]) {
@autoreleasepool {
unichar data[6]={97,98,99,100.101,102};
//1、使用unicode数值数组初始化字符串
NSString* str=[[NSString alloc]initWithCharacters:data length:6];
NSLog(@"%@",str);
char* cstr="hello,IOS!";
//将C风格的字符串转换成NSstring对象
NSString* str2=[NSString stringWithUTF8String:cstr];
NSLog(@"%@",str2);
//将字符串写入到指定文件
[str2 writeToFile:@"myFile.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
//读取文件内容,用文件内容初始化字符串
NSString* str3=[NSString stringWithContentsOfFile:@"main.m" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str3);
}
}
输出
2020-05-21 22:05:28.348144+0800 EX003[1800:116732] abcdf
2020-05-21 22:05:28.348458+0800 EX003[1800:116732] hello,IOS!
2020-05-21 22:05:28.349045+0800 EX003[1800:116732] (null)
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString* str=@"hello";
NSString* book=@"疯狂ios讲义";
//追加字符串
str=[str stringByAppendingString:@",IOS!"];
NSLog(@"%@",str);
//获取字符串对应的c风格字符串
const char* cstr=[str UTF8String];
NSLog(@"%s",cstr);//看输出方式
//在字符串后面追加带变量的字符串
str=[str stringByAppendingFormat:@"%@是一本非常不错的图书",book];
NSLog(@"%@",str);
NSLog(@"str字符串的个数位:%lu",[str length]);
//获取str的前10个字符组成的字符串
NSString *s1=[str substringToIndex:10];
NSLog(@"前十个字符串%@",s1);
//获取从第五个字符串开始的字符串
NSString* s2=[str substringFromIndex:5];
NSLog(@"第五个字符串开始的%@",s2);
//从第五个到15个字符组成的字符串
NSString* s3=[str substringWithRange:NSMakeRange(5,15)];
NSLog(@"%@",s3);
//获取ios在字符串中出现的位置
NSRange pos=[str rangeOfString:@"IOS"];
NSLog(@"ios在str中出现的开始位置:%ld,长度为%ld",pos.location,pos.length);
//将str的所有字符转换为大写
str=[str uppercaseString];
NSLog(@"%@",str);
}
}
输出:
2020-05-21 23:30:46.289884+0800 EX003[2096:147235] hello,IOS!
2020-05-21 23:30:46.290200+0800 EX003[2096:147235] hello,IOS!
2020-05-21 23:30:46.290278+0800 EX003[2096:147235] hello,IOS!疯狂ios讲义是一本非常不错的图书
2020-05-21 23:30:46.290317+0800 EX003[2096:147235] str字符串的个数位:27
2020-05-21 23:30:46.290352+0800 EX003[2096:147235] 前十个字符串hello,IOS!
2020-05-21 23:30:46.290422+0800 EX003[2096:147235] 第五个字符串开始的,IOS!疯狂ios讲义是一本非常不错的图书
2020-05-21 23:30:46.290458+0800 EX003[2096:147235] ,IOS!疯狂ios讲义是一本
2020-05-21 23:30:46.290501+0800 EX003[2096:147235] ios在str中出现的开始位置:6,长度为3
2020-05-21 23:30:46.290539+0800 EX003[2096:147235] HELLO,IOS!疯狂IOS讲义是一本非常不错的图书
NSMutableString代表一个字符序列可变的字符串,他是NSString的子类
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString* book=@"疯狂IOS讲义";
//创建一个NSMutableString对象
NSMutableString* str=[NSMutableString stringWithFormat:@"Hello"];
//追加固定字符串
[str appendString:@",IOS!"];
NSLog(@"%@",str);
//追加带变量的字符串
[str appendFormat:@"%@是一本非常不错的图书",book];
NSLog(@"%@",str);
//指定位置插入字符串
[str insertString:@"fengkuang" atIndex:6];
//删除从位置6开始,长度为12的所有字符
[str deleteCharactersInRange:NSMakeRange(6, 12)];
NSLog(@"%@",str);
//将从位置6开始,长度为9的所有字符替换成object-c
[str replaceCharactersInRange:NSMakeRange(6, 9) withString:@"object-c"];
NSLog(@"%@",str);
}
}
输出:
2020-05-21 23:49:37.772417+0800 EX003[2145:153767] Hello,IOS!
2020-05-21 23:49:37.772743+0800 EX003[2145:153767] Hello,IOS!疯狂IOS讲义是一本非常不错的图书
2020-05-21 23:49:37.772782+0800 EX003[2145:153767] Hello,!疯狂IOS讲义是一本非常不错的图书
2020-05-21 23:49:37.772807+0800 EX003[2145:153767] Hello,object-c一本非常不错的图书
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate* date1=[NSDate date];
NSLog(@"%@",date1);
//获取从当前时间开始,一天之后的日期
NSDate* date2=[[NSDate alloc]initWithTimeIntervalSinceNow:3600*24];
NSLog(@"%@",date2);
//获取从当前时间开始,三天前的日期
NSDate* date3=[[NSDate alloc]initWithTimeIntervalSinceNow:-3*3600*24];
NSLog(@"%@",date3);
//获取从70年1月1日之后20年
NSDate* date4=[NSDate dateWithTimeIntervalSince1970:3600*24*366*20];
NSLog(@"%@",date4);
//获取系统当前的NSLocale
NSLocale* cn=[NSLocale currentLocale];//中文字符串
//获取nsdate在当前nslocale下对应的字符串
NSLog(@"%@",[date1 descriptionWithLocale:cn] );
//获取两个日其中较早前的一个
NSDate* earlier=[date1 earlierDate:date2];
//获取两个日其中较晚的一个
NSDate* later=[date1 laterDate:date2];
NSLog(@"%@",earlier);
NSLog(@"%@",later);
//compare方法返回枚举值包含升序,等值,降序
switch ([date1 compare:date3]) {
case NSOrderedAscending:
NSLog(@"date1位于date3之前");
break;
case NSOrderedSame:
NSLog(@"date1与date3相等");
break;
case NSOrderedDescending:
NSLog(@"date1位于date3之后");
break;
default:
break;
}
//获取两个时间之差
NSLog(@"date1与date3之间时间差%g秒",[date1 timeIntervalSinceDate:date3]);
//获取指定时间与现在时间差
NSLog(@"date2与现在时间差%g秒",[date2 timeIntervalSinceNow]);
}
}
输出:
2020-05-22 00:35:14.807419+0800 EX003[2330:172646] Fri May 22 00:35:14 2020
2020-05-22 00:35:14.807879+0800 EX003[2330:172646] Sat May 23 00:35:14 2020
2020-05-22 00:35:14.807933+0800 EX003[2330:172646] Tue May 19 00:35:14 2020
2020-05-22 00:35:14.807959+0800 EX003[2330:172646] Tue Jan 16 08:00:00 1990
2020-05-22 00:35:14.812408+0800 EX003[2330:172646] 2020年5月22日 星期五 中国标准时间 上午12:35:14
2020-05-22 00:35:14.812469+0800 EX003[2330:172646] Fri May 22 00:35:14 2020
2020-05-22 00:35:14.812495+0800 EX003[2330:172646] Sat May 23 00:35:14 2020
2020-05-22 00:35:14.812533+0800 EX003[2330:172646] date1位于date3之后
2020-05-22 00:35:14.812565+0800 EX003[2330:172646] date1与date3之间时间差259200秒
2020-05-22 00:35:14.812651+0800 EX003[2330:172646] date2与现在时间差86400秒
int main(int argc, const char * argv[]) {
@autoreleasepool {
//获取代表公里的nscalendar对象
NSCalendar* gregorian=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//获取当前日期
NSDate* dt=[NSDate date];
//定义一个时间字段的flag,指定将会获取指定年月日时分秒的信息
unsigned unitFlags=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour|NSCalendarUnitMinute|
NSCalendarUnitSecond | NSCalendarUnitWeekday;
//获取不同时间字段的信息
NSDateComponents* comp=[gregorian components:unitFlags fromDate:dt];
//获取各时间字段的数组
NSLog(@"现在是%ld年",comp.year);
NSLog(@"现在是%ld月",comp.month);
NSLog(@"现在是%ld日",comp.day);
NSLog(@"现在是%ld时",comp.hour);
NSLog(@"现在是%ld分",comp.minute);
NSLog(@"现在是%ld秒",comp.weekday);
//Z再次创建一个nsdatecompentents对象
NSDateComponents* comp2=[[NSDateComponents alloc]init];
comp2.year=2020;
comp2.month=5;
comp2.day=3;
comp2.hour=18;
comp2.minute=34;
//通过nsdatecompentents所包含的各时间字段的数值来恢复nsdate对象
NSDate* date=[gregorian dateFromComponents:comp2];
NSLog(@"获取的日期为:%@",date);
}
}
输出:
2020-05-22 01:08:33.746108+0800 EX003[2399:182344] 现在是2020年
2020-05-22 01:08:33.746280+0800 EX003[2399:182344] 现在是5月
2020-05-22 01:08:33.746311+0800 EX003[2399:182344] 现在是22日
2020-05-22 01:08:33.746335+0800 EX003[2399:182344] 现在是1时
2020-05-22 01:08:33.746356+0800 EX003[2399:182344] 现在是8分
2020-05-22 01:08:33.746377+0800 EX003[2399:182344] 现在是6秒
2020-05-22 01:08:33.749371+0800 EX003[2399:182344] 获取的日期为:2020-05-03 10:34:00 +0000
创建定时器的方法
timeInterval:指定每隔多少秒执行一次任务
invocation或target与selector:指定重复执行的任务。如果指定target和selector参数,则指定用某个对象的特定方法作为重复执行的任务。
invocation参数,概参数需要传入一个nsinvocation对象,概对象也封装了target和selector
userinfo:用于传入额外的附加信息
repeats:指定一个bool值,该参数控制是否需要重复执行任务
_timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(inof:) userInfo:nil repeats:YES];
-(void)info:(NSTimer*)timer{
NSLog(@"正在执行第%ld次任务",_count++);
if (_count>10) {
NSLog(@"取消执行定时器");
[_timer invalidate];
}
}
copy和方法返回对象不可修改的副本,即是对象是可修改的对象
mutableCopy方法返回对象可修改的副本(可变副本),即是对象是不可修改的对象
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *book=[NSMutableString stringWithString:@"这是一个可变字符串"];
//创建一个可变字符串的可变副本
NSMutableString* bookCopy=[book mutableCopy];
//修改这个可变副本
[bookCopy replaceCharactersInRange:NSMakeRange(2,3) withString:@"对肯变字符进行替换"];
//打印原字符和可变副本
NSLog(@"%@",book);
NSLog(@"%@",bookCopy);
//创建一个不可变字符
NSString* bookName=@"这是一本你看不懂的书";
NSMutableString* bookNameCopy=[bookName mutableCopy];
[bookNameCopy appendString:@"追加的文本"];
NSLog(@"原字符串%@",bookName);
NSLog(@"字符串副本%@",bookNameCopy);
//创建一个可变字符串
NSMutableString* bookCopy2=[book copy];
//copy方法创建的副本是不可修改的
[bookCopy2 appendString:@"不可以修改"];//运行时本行报错,但是编译不报错
}
}
输出
2020-05-22 09:20:07.307616+0800 EX003[744:19055] 这是一个可变字符串
2020-05-22 09:20:07.307916+0800 EX003[744:19055] 这是对肯变字符进行替换变字符串
2020-05-22 09:20:07.307983+0800 EX003[744:19055] 原字符串这是一本你看不懂的书
2020-05-22 09:20:07.308075+0800 EX003[744:19055] 字符串副本这是一本你看不懂的书追加的文本
NSArray的常用方法
一旦得到NSArray对象,接下来可以调用他的方法来操作NSArray集合。
集合的方法大致包含
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray* myarray=[NSArray arrayWithObjects:@"疯狂安卓讲义",@"疯狂IOS讲义",@"疯狂Swift义",@"疯狂XML讲义",@"疯狂JAVA讲义",nil];
NSLog(@"第一个元素为%@",[myarray objectAtIndex:0]);
NSLog(@"第二个元素为%@",[myarray objectAtIndex:1]);
NSLog(@"最后一个元素为%@",[myarray lastObject]);
NSArray* myarray1=[myarray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 3)]];
NSLog(@"从中截取的三个数的集合为:%@",myarray1);
//获取指定元素在集合中的位置
NSLog(@"疯狂IOS讲义在整个集合中的位置为%ld",[myarray indexOfObject:@"疯狂IOS讲义"]);//索引位置
//获取指定元素在指定范围中的位置
NSLog(@"疯狂XML讲义在指定范围内的位置%ld",[myarray indexOfObject:@"疯狂XML讲义" inRange:NSMakeRange(2, 3)]);
//向数组后追加一个元素,追加后原数组并没有变化,而是生成一个新的数组
NSArray* myarray2=[myarray arrayByAddingObject:@"疯狂html5讲义"];
//向数组后追加一个数组的所有元素,同样原数组没有变化,而是生成一个新的数组
NSArray* myarray3=[myarray arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"疯狂PHP讲义",@"疯狂JAVASCRIPT讲义",@"疯狂OBJECT-C讲义",nil]];
for (int i=0; i<myarray2.count; i++) {
NSLog(@"%@",myarray2[i]);
}
NSLog(@"----------");
for (int i=0; i<myarray3.count; i++) {
NSLog(@"%@",[myarray3 objectAtIndex:i]);
}
//获取array中指定元素
NSArray* myarray4=[myarray3 subarrayWithRange:NSMakeRange(3, 4)];
[myarray4 writeToFile:@"myfile.txt" atomically:YES];
}
}
输出
2020-05-22 18:01:15.239778+0800 EX003[3496:171862] 第一个元素为疯狂安卓讲义
2020-05-22 18:01:15.240111+0800 EX003[3496:171862] 第二个元素为疯狂IOS讲义
2020-05-22 18:01:15.240147+0800 EX003[3496:171862] 最后一个元素为疯狂JAVA讲义
2020-05-22 18:01:15.240223+0800 EX003[3496:171862] 从中截取的三个数的集合为:(
“\U75af\U72c2Swift\U4e49”,
“\U75af\U72c2XML\U8bb2\U4e49”,
“\U75af\U72c2JAVA\U8bb2\U4e49”
)
2020-05-22 18:01:15.240254+0800 EX003[3496:171862] 疯狂IOS讲义在整个集合中的位置为1
2020-05-22 18:01:15.240279+0800 EX003[3496:171862] 疯狂XML讲义在指定范围内的位置3
2020-05-22 18:01:15.240316+0800 EX003[3496:171862] 疯狂安卓讲义
2020-05-22 18:01:15.240339+0800 EX003[3496:171862] 疯狂IOS讲义
2020-05-22 18:01:15.240359+0800 EX003[3496:171862] 疯狂Swift义
2020-05-22 18:01:15.240377+0800 EX003[3496:171862] 疯狂XML讲义
2020-05-22 18:01:15.240395+0800 EX003[3496:171862] 疯狂JAVA讲义
2020-05-22 18:01:15.240413+0800 EX003[3496:171862] 疯狂html5讲义
2020-05-22 18:01:15.240429+0800 EX003[3496:171862] ----------
2020-05-22 18:01:15.240447+0800 EX003[3496:171862] 疯狂安卓讲义
2020-05-22 18:01:15.240464+0800 EX003[3496:171862] 疯狂IOS讲义
2020-05-22 18:01:15.243297+0800 EX003[3496:171862] 疯狂Swift义
2020-05-22 18:01:15.243328+0800 EX003[3496:171862] 疯狂XML讲义
2020-05-22 18:01:15.243349+0800 EX003[3496:171862] 疯狂JAVA讲义
2020-05-22 18:01:15.243368+0800 EX003[3496:171862] 疯狂PHP讲义
2020-05-22 18:01:15.243387+0800 EX003[3496:171862] 疯狂JAVASCRIPT讲义
2020-05-22 18:01:15.243404+0800 EX003[3496:171862] 疯狂OBJECT-C讲义
@interface User:NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* pass;
-(id)initwithName:(NSString*)name andpass:(NSString*)pass;
-(void)say:(NSString*) content;
@end
@implementation User
-(id)initwithName:(NSString*)name andpass:(NSString*)pass{
self.name=name;
self.pass=pass;
return self;
}
-(void)say:(NSString*) content{
NSLog(@"%@ say %@",self.name,content);
}
-(NSString*) description{
return [NSString stringWithFormat:@"User的name=%@,pass=%@",self.name,self.pass];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray* array=@[
[[User alloc]initwithName:@"张三" andpass:@"zhangsan"],
[[User alloc]initwithName:@"李四" andpass:@"lis"],
[[User alloc]initwithName:@"王五" andpass:@"wangwu"],
[[User alloc]initwithName:@"赵六" andpass:@"zhaoliu"]];
//使用集合元素整体调用方法,会同时调用4次方法
[array makeObjectsPerformSelector:@selector(say:) withObject:@"下午好,这是调用对象的方法"];
NSString* content=@"疯狂安卓讲义";
//迭代集合内指定范围内的元素,并使用该元素来执行代码块
//代码块的第一个参数是正在遍历的集合元素,第二个参数代表正在遍历的集合元素的索引,第三个参数用于控制是否停止遍历,将该参数设置为no即可停止遍历
[array enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)] options:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
NSLog(@"正在处理第%ld个元素:%@",idx,obj);
[obj say:content];
}];
}
}
输出:
2020-05-22 20:29:55.001380+0800 EX003[3852:209366] 张三 say 下午好,这是调用对象的方法
2020-05-22 20:29:55.001800+0800 EX003[3852:209366] 李四 say 下午好,这是调用对象的方法
2020-05-22 20:29:55.001856+0800 EX003[3852:209366] 王五 say 下午好,这是调用对象的方法
2020-05-22 20:29:55.001897+0800 EX003[3852:209366] 赵六 say 下午好,这是调用对象的方法
2020-05-22 20:29:55.001983+0800 EX003[3852:209366] 正在处理第3个元素:User的name=赵六,pass=zhaoliu
2020-05-22 20:29:55.002014+0800 EX003[3852:209366] 赵六 say 疯狂安卓讲义
2020-05-22 20:29:55.002048+0800 EX003[3852:209366] 正在处理第2个元素:User的name=王五,pass=wangwu
2020-05-22 20:29:55.002070+0800 EX003[3852:209366] 王五 say 疯狂安卓讲义
#import
//第一比较函数,根据两个对象的intValue进行比较
NSComparisonResult intSort(id num1,id num2,void *context){
int v1=[num1 intValue];
int v2=[num2 intValue];
if (v1<v2) {
return NSOrderedAscending;
}else if(v1>v2){
return NSOrderedDescending;
}else{
return NSOrderedSame;
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化一个元素为nsstring的nsArray的对象
NSArray* array1=@[@"Object-c",@"C",@"C++",@"Ruby",@"Perl",@"Swift"];
//使用集合元素的compare方法进行排序,这个compare是数组自带的排序函数
array1=[array1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",array1);
//初始化一个元素为NSNumber的NSArray对象
NSArray* array2=@[
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:-8],
[NSNumber numberWithInt:34],
[NSNumber numberWithInt:23],
[NSNumber numberWithInt:53],
[NSNumber numberWithInt:-23]
];
//使用自定义的intSort函数进行排序
array2=[array2 sortedArrayUsingFunction:intSort context:nil];
NSLog(@"%@",array2);
//使用代码块对集合进行排序
NSArray* array3=[array2 sortedArrayUsingComparator:^(id obj1, id obj2) {
//该代码块就是根据集合元素的intValue进行比较
if ([obj1 intValue]>[obj2 intValue]) {
return NSOrderedDescending;
}else if([obj1 intValue]<[obj2 intValue]) {
return NSOrderedAscending;
}else{
return NSOrderedSame;
}
}];
NSLog(@"%@",array3);
}
}
输出:
2020-05-22 21:32:47.146512+0800 EX003[3969:227655] (
C,
“C++”,
“Object-c”,
Perl,
Ruby,
Swift
)
2020-05-22 21:32:47.146888+0800 EX003[3969:227655] (
“-23”,
“-8”,
12,
20,
23,
34,
53
)
2020-05-22 21:32:47.146948+0800 EX003[3969:227655] (
“-23”,
“-8”,
12,
20,
23,
34,
53
)
nsarray对象的两个方法返回枚举器
- objectEnumerator:返回nsarray集合的顺序枚举器
- reverseObjectEnumerator:返回nsArray集合的逆序枚举器
以上两个方法都返回一个nsenumerator枚举器,而枚举器又包含以下两个方法
- allObjects:获取被枚举集合中的所有元素
- nextObject:获取被枚举集合中的下一个元素。
一般来说通过nextObject方法即可对集合元素进行枚举,可以采取循环的方式不断获取nextObject方法的返回值,直到该方法的返回值为nil结束循环。
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化一个元素为NSNumber的NSArray对象
NSArray* array2=@[
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:-8],
[NSNumber numberWithInt:34],
[NSNumber numberWithInt:23],
[NSNumber numberWithInt:53],
[NSNumber numberWithInt:-23]
];
//获取NSArray的顺序枚举器
NSEnumerator* en=[array2 objectEnumerator];
id object;
while (object=[en nextObject]) {
NSLog(@"%@",object);
}
NSLog(@"------");
//获取NSArray的顺序枚举器
NSEnumerator* en2=[array2 reverseObjectEnumerator];
while (object=[en2 nextObject]) {
NSLog(@"%@",object);
}
}
}
输出:
2020-05-22 21:49:48.624486+0800 EX003[4011:232953] 20
2020-05-22 21:49:48.624784+0800 EX003[4011:232953] 12
2020-05-22 21:49:48.624815+0800 EX003[4011:232953] -8
2020-05-22 21:49:48.624836+0800 EX003[4011:232953] 34
2020-05-22 21:49:48.624854+0800 EX003[4011:232953] 23
2020-05-22 21:49:48.624872+0800 EX003[4011:232953] 53
2020-05-22 21:49:48.624891+0800 EX003[4011:232953] -23
2020-05-22 21:49:48.624913+0800 EX003[4011:232953] ------
2020-05-22 21:49:48.624939+0800 EX003[4011:232953] -23
2020-05-22 21:49:48.624959+0800 EX003[4011:232953] 53
2020-05-22 21:49:48.624977+0800 EX003[4011:232953] 23
2020-05-22 21:49:48.625025+0800 EX003[4011:232953] 34
2020-05-22 21:49:48.625056+0800 EX003[4011:232953] -8
2020-05-22 21:49:48.625077+0800 EX003[4011:232953] 12
2020-05-22 21:49:48.625104+0800 EX003[4011:232953] 20
Program ended with exit code: 0
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化一个元素为NSNumber的NSArray对象
NSArray* array2=@[
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:-8],
[NSNumber numberWithInt:34],
[NSNumber numberWithInt:23],
[NSNumber numberWithInt:53],
[NSNumber numberWithInt:-23]
];
for (id object in array2) {
NSLog(@"%@",object);
}
}
}
输出:
2020-05-22 21:52:40.120016+0800 EX003[4033:234404] 20
2020-05-22 21:52:40.120325+0800 EX003[4033:234404] 12
2020-05-22 21:52:40.120372+0800 EX003[4033:234404] -8
2020-05-22 21:52:40.120395+0800 EX003[4033:234404] 34
2020-05-22 21:52:40.120414+0800 EX003[4033:234404] 23
2020-05-22 21:52:40.120431+0800 EX003[4033:234404] 53
2020-05-22 21:52:40.120450+0800 EX003[4033:234404] -23
可变数组,数量可变,元素可追加和删该
add开头添加集合元素
remove开头删除集合元素
replace开头替换集合元素
sort开头对集合排序
nsmutableArray同样也提供了三排序方法,但不同的是,该方法是对array数组本身进行了排序,而不是产生一个新的对象
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化一个元素为NSNumber的NSArray对象
NSMutableArray* array=[NSMutableArray arrayWithObjects:@"疯狂安卓讲义",@"疯狂IOS讲义",@"疯狂JAVA讲义",nil];
//向集合添加一个元素
[array addObject:@"疯狂精神分裂讲义"];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//向集合插入一个由多个元素组成的数组
[array addObjectsFromArray:[NSArray arrayWithObjects:@"疯狂html讲义",@"疯狂的石头讲义", nil]];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//指定位置插入元素
[array insertObject:@"疯狂的开火车讲义" atIndex:3];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//指定位置插入多个元素
[array insertObjects:[NSArray arrayWithObjects:@"疯狂Swift义",@"疯狂XML讲义", nil] atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 2)]];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//删除集合最后一个元素
[array removeLastObject];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//删除集合第5个元素
[array removeObjectAtIndex:5];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//删除集合指定段的元素
[array removeObjectsInRange:NSMakeRange(2, 3)];
for (id object in array) {
NSLog(@"%@",object);
}
NSLog(@"---------");
//替换指定位置元素
[array replaceObjectAtIndex:2 withObject:@"疯狂的搞不清楚的讲义"];
for (id object in array) {
NSLog(@"%@",object);
}
}
}
输出:
2020-05-23 00:53:48.805974+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.806330+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.806363+0800 EX003[4358:283472] 疯狂JAVA讲义
2020-05-23 00:53:48.806385+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.806404+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.806454+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.806475+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.806493+0800 EX003[4358:283472] 疯狂JAVA讲义
2020-05-23 00:53:48.806511+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.806532+0800 EX003[4358:283472] 疯狂html讲义
2020-05-23 00:53:48.806549+0800 EX003[4358:283472] 疯狂的石头讲义
2020-05-23 00:53:48.806596+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.806633+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.806657+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.806677+0800 EX003[4358:283472] 疯狂JAVA讲义
2020-05-23 00:53:48.806718+0800 EX003[4358:283472] 疯狂的开火车讲义
2020-05-23 00:53:48.806743+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.806761+0800 EX003[4358:283472] 疯狂html讲义
2020-05-23 00:53:48.809615+0800 EX003[4358:283472] 疯狂的石头讲义
2020-05-23 00:53:48.809656+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.809747+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.809773+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.809794+0800 EX003[4358:283472] 疯狂JAVA讲义
2020-05-23 00:53:48.809812+0800 EX003[4358:283472] 疯狂Swift义
2020-05-23 00:53:48.809830+0800 EX003[4358:283472] 疯狂XML讲义
2020-05-23 00:53:48.809848+0800 EX003[4358:283472] 疯狂的开火车讲义
2020-05-23 00:53:48.809866+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.809883+0800 EX003[4358:283472] 疯狂html讲义
2020-05-23 00:53:48.809900+0800 EX003[4358:283472] 疯狂的石头讲义
2020-05-23 00:53:48.809917+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.809936+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.809953+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.810016+0800 EX003[4358:283472] 疯狂JAVA讲义
2020-05-23 00:53:48.810037+0800 EX003[4358:283472] 疯狂Swift义
2020-05-23 00:53:48.810055+0800 EX003[4358:283472] 疯狂XML讲义
2020-05-23 00:53:48.810102+0800 EX003[4358:283472] 疯狂的开火车讲义
2020-05-23 00:53:48.810140+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.810167+0800 EX003[4358:283472] 疯狂html讲义
2020-05-23 00:53:48.810187+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.810240+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.810301+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.810330+0800 EX003[4358:283472] 疯狂JAVA讲义
2020-05-23 00:53:48.810352+0800 EX003[4358:283472] 疯狂Swift义
2020-05-23 00:53:48.810370+0800 EX003[4358:283472] 疯狂XML讲义
2020-05-23 00:53:48.810388+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.810406+0800 EX003[4358:283472] 疯狂html讲义
2020-05-23 00:53:48.810423+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.810440+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.810498+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.810542+0800 EX003[4358:283472] 疯狂精神分裂讲义
2020-05-23 00:53:48.810605+0800 EX003[4358:283472] 疯狂html讲义
2020-05-23 00:53:48.810640+0800 EX003[4358:283472] ---------
2020-05-23 00:53:48.810665+0800 EX003[4358:283472] 疯狂安卓讲义
2020-05-23 00:53:48.810684+0800 EX003[4358:283472] 疯狂IOS讲义
2020-05-23 00:53:48.810703+0800 EX003[4358:283472] 疯狂的搞不清楚的讲义
2020-05-23 00:53:48.810720+0800 EX003[4358:283472] 疯狂html讲义
nsArray允许直接对集合中的所有元素进行整体的KVC编码,有两套方法
#import
@interface User:NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* pass;
-(id)initwithName:(NSString*)name andpass:(NSString*)pass;
-(void)say:(NSString*) content;
@end
@implementation User
-(id)initwithName:(NSString*)name andpass:(NSString*)pass{
self.name=name;
self.pass=pass;
return self;
}
-(void)say:(NSString*) content{
NSLog(@"%@ say %@",self.name,content);
}
-(NSString*) description{
return [NSString stringWithFormat:@"User的name=%@,pass=%@",self.name,self.pass];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray* array=@[
[[User alloc]initwithName:@"张三" andpass:@"zhangsan"],
[[User alloc]initwithName:@"李四" andpass:@"lis"],
[[User alloc]initwithName:@"王五" andpass:@"wangwu"],
[[User alloc]initwithName:@"赵六" andpass:@"zhaoliu"]];
//获取所有集合元素的name属性组成的新集合
id newArray=[array valueForKey:@"name"];
for (id object in newArray) {
NSLog(@"%@",object);
}
NSLog(@"-----------");
//对集合所有元素整体进行kvc编程,将所有集合元素的name属性改成“新名字”
[array setValue:@"新名字" forKey:@"name"];
for (id object in array) {
NSLog(@"%@",object);
}
}
}
输出:
2020-05-23 01:07:48.038048+0800 EX003[4430:289963] 张三
2020-05-23 01:07:48.038353+0800 EX003[4430:289963] 李四
2020-05-23 01:07:48.038404+0800 EX003[4430:289963] 王五
2020-05-23 01:07:48.038437+0800 EX003[4430:289963] 赵六
2020-05-23 01:07:48.038459+0800 EX003[4430:289963] -----------
2020-05-23 01:07:48.038704+0800 EX003[4430:289963] User的name=新名字,pass=zhangsan
2020-05-23 01:07:48.038748+0800 EX003[4430:289963] User的name=新名字,pass=lis
2020-05-23 01:07:48.038774+0800 EX003[4430:289963] User的name=新名字,pass=wangwu
2020-05-23 01:07:48.038797+0800 EX003[4430:289963] User的name=新名字,pass=zhaoliu
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSSet* set=[NSSet setWithObjects:@"疯狂安卓讲义",@"疯狂IOS讲义",@"疯狂JAVA讲义",nil];
//集合的个数
NSLog(@"%ld",[set count]);
NSLog(@"---------");
for (id object in set) {
NSLog(@"%@",object);
}
NSSet* set2=[NSSet setWithObjects:@"冷静安卓讲义",@"冷静IOS讲义",@"冷静JAVA讲义",nil];
set =[set setByAddingObject:@"疯狂XML讲义"];//从第一个索引位置
NSLog(@"---------");
for (id object in set) {
NSLog(@"%@",object);
}
NSSet* set3= [set setByAddingObjectsFromSet:set2];
NSLog(@"---------");
for (id object in set3) {
NSLog(@"%@",object);
}
BOOL intersects=[set intersectsSet:set2];
NSLog(@"两集合是否有交集%d",intersects);
BOOL issub=[set isSubsetOfSet:set2];
NSLog(@"两集合是否有子集%d",issub);
BOOL iscontain=[set containsObject:@"疯狂JAVA讲义"];
NSLog(@"两集合是否包含%d",iscontain);
//不确定地取出一个元素
NSLog(@"随机取出一个元素:%@",[set2 anyObject]);
}
}
输出:
2020-05-23 10:15:29.891791+0800 EX003[1191:36641] 3
2020-05-23 10:15:29.892060+0800 EX003[1191:36641] ---------
2020-05-23 10:15:29.892120+0800 EX003[1191:36641] 疯狂IOS讲义
2020-05-23 10:15:29.892145+0800 EX003[1191:36641] 疯狂安卓讲义
2020-05-23 10:15:29.892166+0800 EX003[1191:36641] 疯狂JAVA讲义
2020-05-23 10:15:29.892190+0800 EX003[1191:36641] ---------
2020-05-23 10:15:29.892218+0800 EX003[1191:36641] 疯狂XML讲义
2020-05-23 10:15:29.892238+0800 EX003[1191:36641] 疯狂IOS讲义
2020-05-23 10:15:29.892256+0800 EX003[1191:36641] 疯狂安卓讲义
2020-05-23 10:15:29.892274+0800 EX003[1191:36641] 疯狂JAVA讲义
2020-05-23 10:15:29.892293+0800 EX003[1191:36641] ---------
2020-05-23 10:15:29.892312+0800 EX003[1191:36641] 疯狂安卓讲义
2020-05-23 10:15:29.892331+0800 EX003[1191:36641] 冷静安卓讲义
2020-05-23 10:15:29.892388+0800 EX003[1191:36641] 疯狂IOS讲义
2020-05-23 10:15:29.892421+0800 EX003[1191:36641] 冷静IOS讲义
2020-05-23 10:15:29.892443+0800 EX003[1191:36641] 冷静JAVA讲义
2020-05-23 10:15:29.892466+0800 EX003[1191:36641] 疯狂XML讲义
2020-05-23 10:15:29.892498+0800 EX003[1191:36641] 疯狂JAVA讲义
2020-05-23 10:15:29.894999+0800 EX003[1191:36641] 两集合是否有交集0
2020-05-23 10:15:29.895044+0800 EX003[1191:36641] 两集合是否有子集0
2020-05-23 10:15:29.895071+0800 EX003[1191:36641] 两集合是否包含1
2020-05-23 10:15:29.895102+0800 EX003[1191:36641] 随机取出一个元素:冷静IOS讲义
集合的计数的一个集合,集合添加或减少一个元素的时候这个count就会+1或者-1
方法:countForObject:
这两个集合既具有nsSet集合的特征(不允许重复元素)也具有nsarray的特征(保持元素的添加顺序,且每个元素都有索引,可以根据索引来操作元素)
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSOrderedSet* set=[NSOrderedSet orderedSetWithObjects:
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:-8],
[NSNumber numberWithInt:34],
[NSNumber numberWithInt:23],
[NSNumber numberWithInt:53],
[NSNumber numberWithInt:-23],nil
];
NSLog(@"set集合的第一个元素:%@",[set firstObject]);
NSLog(@"set集合的最后一个元素:%@",[set lastObject]);
NSLog(@"set集合索引为2的元素:%@",[set objectAtIndex:2]);
NSLog(@"23在索引中的位置为:%ld",[set indexOfObject:[NSNumber numberWithInt:23]]);
//使用代码块,把老集合中所有>20的元素都找出来提取到新的集合中
NSIndexSet* indexSet=[set indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
return (BOOL)([obj intValue]>20);
}];
NSLog(@"---------");
NSLog(@"%@",indexSet);
}
}
输出:
2020-05-23 11:20:37.609326+0800 EX003[2172:61649] set集合的第一个元素:20
2020-05-23 11:20:37.609631+0800 EX003[2172:61649] set集合的最后一个元素:-23
2020-05-23 11:20:37.609668+0800 EX003[2172:61649] set集合索引为2的元素:-8
2020-05-23 11:20:37.609695+0800 EX003[2172:61649] 23在索引中的位置为:4
2020-05-23 11:20:37.609739+0800 EX003[2172:61649] ---------
2020-05-23 11:20:37.609874+0800 EX003[2172:61649]
创建字典的简化语法
@{key1:value1,key2:value2,…}
其他方法:
#import
@interface NSDictionary(print)
-(void)print;
@end
@implementation NSDictionary(print)
-(void)print{
NSMutableString* result=[NSMutableString stringWithString:@"{"];
for (id key in self) {
[result appendString:[key description]];
[result appendString:@"="];
//使用下标法根据key来获取对应的value
[result appendString:[self[key] description]];
[result appendString:@","];
}
//获取字符串长度
NSUInteger len=[result length];
//去掉字符串最后的两个字符
[result deleteCharactersInRange:NSMakeRange(len-2, 2)];
[result appendString:@"}"];
NSLog(@"%@",result);
}
@end
@interface User:NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* pass;
-(id)initwithName:(NSString*)name andpass:(NSString*)pass;
-(void)say:(NSString*) content;
@end
@implementation User
-(id)initwithName:(NSString*)name andpass:(NSString*)pass{
self.name=name;
self.pass=pass;
return self;
}
-(void)say:(NSString*) content{
NSLog(@"%@ say %@",self.name,content);
}
-(NSString*) description{
return [NSString stringWithFormat:@"User的name=%@,pass=%@",self.name,self.pass];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary* dict=[NSDictionary dictionaryWithObjectsAndKeys:[[User alloc]initwithName:@"张三" andpass:@"zhangsan"],@"one",
[[User alloc]initwithName:@"李四" andpass:@"lisi"],@"two",
[[User alloc]initwithName:@"王五" andpass:@"wangwu"],@"three",
[[User alloc]initwithName:@"赵六" andpass:@"zhaoliu"],@"four",
[[User alloc]initwithName:@"孙七" andpass:@"sunqi"],@"five",nil];
[dict print];
NSLog(@"dict里包含%ld个键值对",[dict count]);
NSLog(@"dict里包所的key为:%@",[dict allKeys]);
NSLog(@"name赵六对应的key为:%@",[dict allKeysForObject:[[User alloc]initwithName:@"王五" andpass:@"wangwu"]]);//不知道为什么不能实现
//获取遍历集合的枚举器
NSEnumerator* en=[dict objectEnumerator];
//用枚举器来遍历所有的value
id object;
while (object=[en nextObject]) {
NSLog(@"字典集合中所有的value为:%@",object);
}
//使用块来迭代所有的键值对
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key的值为%@",key);
[obj say:@"疯狂讲义"];
}];
}
}
输出
2020-05-23 14:52:44.443971+0800 EX003[2685:115782] {one=User的name=张三,pass=zhangsan,five=User的name=孙七,pass=sunqi,three=User的name=王五,pass=wangwu,two=User的name=李四,pass=lisi,four=User的name=赵六,pass=zhaoli}
2020-05-23 14:52:44.444277+0800 EX003[2685:115782] dict里包含5个键值对
2020-05-23 14:52:44.444369+0800 EX003[2685:115782] dict里包所的key为:(
one,
five,
three,
two,
four
)
2020-05-23 14:52:44.444423+0800 EX003[2685:115782] name赵六对应的key为:(
)
2020-05-23 14:52:44.444636+0800 EX003[2685:115782] 字典集合中所有的value为:User的name=张三,pass=zhangsan
2020-05-23 14:52:44.444674+0800 EX003[2685:115782] 字典集合中所有的value为:User的name=孙七,pass=sunqi
2020-05-23 14:52:44.444702+0800 EX003[2685:115782] 字典集合中所有的value为:User的name=王五,pass=wangwu
2020-05-23 14:52:44.444726+0800 EX003[2685:115782] 字典集合中所有的value为:User的name=李四,pass=lisi
2020-05-23 14:52:44.444749+0800 EX003[2685:115782] 字典集合中所有的value为:User的name=赵六,pass=zhaoliu
Program ended with exit code: 0
三个方法
#import
@interface NSDictionary(print)
-(void)print;
@end
@implementation NSDictionary(print)
-(void)print{
NSMutableString* result=[NSMutableString stringWithString:@"{"];
for (id key in self) {
[result appendString:[key description]];
[result appendString:@"="];
//使用下标法根据key来获取对应的value
[result appendString:[self[key] description]];
[result appendString:@","];
}
//获取字符串长度
NSUInteger len=[result length];
//去掉字符串最后的两个字符
[result deleteCharactersInRange:NSMakeRange(len-2, 2)];
[result appendString:@"}"];
NSLog(@"%@",result);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary* dict=@{@"one":@"Object-c",@"two":@"ruby",@"three":@"Python",@"four":@"Perl"};
//打印集合的所有元素
[dict print];
//获取所有直接调用value的compare方法对所有的key进行排序
//返回排序号的所有key组成的nsArray
NSArray* keyArray=[dict keysSortedByValueUsingSelector:@selector(compare:)];
NSLog(@"%@",keyArray);
//以nsdictionary的value长度作为标准进行排序,越长越大
NSArray* keyArr2=[dict keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 length]>[obj2 length]) {
return NSOrderedAscending;
}else if([obj1 length]<[obj2 length]){
return NSOrderedDescending;
}else{
return NSOrderedSame;
}
}];
NSLog(@"%@",keyArr2);
for (id object in keyArr2) {
NSLog(@"key:%@ value:%@",object,dict[object]);
}
}
}
输出
2020-05-23 23:39:14.804647+0800 EX003[1319:37139] {one=Object-c,three=Python,two=ruby,four=Per}
2020-05-23 23:39:14.805010+0800 EX003[1319:37139] (
one,
four,
three,
two
)
2020-05-23 23:39:14.805065+0800 EX003[1319:37139] (
one,
three,
two,
four
)
2020-05-23 23:39:14.805096+0800 EX003[1319:37139] key:one value:Object-c
2020-05-23 23:39:14.805118+0800 EX003[1319:37139] key:three value:Python
2020-05-23 23:39:14.805138+0800 EX003[1319:37139] key:two value:ruby
2020-05-23 23:39:14.805156+0800 EX003[1319:37139] key:four value:Perl
@interface NSDictionary(print)
-(void)print;
@end
@implementation NSDictionary(print)
-(void)print{
NSMutableString* result=[NSMutableString stringWithString:@"{"];
for (id key in self) {
[result appendString:[key description]];
[result appendString:@"="];
//使用下标法根据key来获取对应的value
[result appendString:[self[key] description]];
[result appendString:@","];
}
//获取字符串长度
NSUInteger len=[result length];
//去掉字符串最后的两个字符
[result deleteCharactersInRange:NSMakeRange(len-2, 2)];
[result appendString:@"}"];
NSLog(@"%@",result);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//使用简化语法创建nsdictionary对象
NSDictionary* dict=@{
@"Object-c":[NSNumber numberWithInt:89],
@"JAVA":[NSNumber numberWithInt:29],
@"SWIFT":[NSNumber numberWithInt:129],
@"JavaScript":[NSNumber numberWithInt:39]
};
[dict print];
//对nsdictionary的所有key进行过滤
NSSet* keySet=[dict keysOfEntriesPassingTest:^BOOL(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//当value值大于80时返回YES
return (BOOL)([obj intValue]>80);
}];
NSLog(@"%@",keySet);
}
}
输出:
2020-05-23 23:55:48.161469+0800 EX003[1472:43530] {Object-c=89,SWIFT=129,JAVA=29,JavaScript=3}
2020-05-23 23:55:48.161811+0800 EX003[1472:43530] {(
“Object-c”,
SWIFT
)}
用于定义一个逻辑条件,通过该条件可执行搜索或内存中的过滤操作。
以谓词字符串来创建谓词
group.name like “work”
ALL children.age>12
ANY children.age>12
#import
@interface User:NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* pass;
-(id)initwithName:(NSString*)name andpass:(NSString*)pass;
-(void)say:(NSString*) content;
@end
@implementation User
-(id)initwithName:(NSString*)name andpass:(NSString*)pass{
self.name=name;
self.pass=pass;
return self;
}
-(void)say:(NSString*) content{
NSLog(@"%@ say %@",self.name,content);
}
-(NSString*) description{
return [NSString stringWithFormat:@"User的name=%@,pass=%@",self.name,self.pass];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//name以s开头的字符串,以此谓词字符串作为谓词对象
NSPredicate* pred=[NSPredicate predicateWithFormat:@"name like 's*'"];
User* user1=[[User alloc]initwithName:@"sun" andpass:@"123"];
//对user1进行谓词判断
BOOL result1=[pred evaluateWithObject:user1];
NSLog(@"user1的那么是否以s开头:%d",result1);
User* user2=[[User alloc]initwithName:@"zhang" andpass:@"123"];
//对user1进行谓词判断
BOOL result2=[pred evaluateWithObject:user2];
NSLog(@"user1的那么是否以s开头:%d",result2);
}
}
输出:
2020-05-24 00:20:08.599536+0800 EX003[1866:52961] user1的那么是否以s开头:1
2020-05-24 00:20:08.599847+0800 EX003[1866:52961] user1的那么是否以s开头:0
#import
@interface User:NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* pass;
-(id)initwithName:(NSString*)name andpass:(NSString*)pass;
-(void)say:(NSString*) content;
@end
@implementation User
-(id)initwithName:(NSString*)name andpass:(NSString*)pass{
self.name=name;
self.pass=pass;
return self;
}
-(void)say:(NSString*) content{
NSLog(@"%@ say %@",self.name,content);
}
-(NSString*) description{
return [NSString stringWithFormat:@"User的name=%@,pass=%@",self.name,self.pass];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray* array=[NSMutableArray arrayWithObjects:[NSNumber numberWithInt:40],
[NSNumber numberWithInt:55],[NSNumber numberWithInt:340],[NSNumber numberWithInt:20],nil];
//创建谓词,要求该对象自身大于50的集合元素
NSPredicate* pred1=[NSPredicate predicateWithFormat:@"self>50"];
//使用谓词执行过滤,过滤后只剩下大于50的集合元素
[array filterUsingPredicate:pred1];
NSLog(@"值大于50的元素%@",array);
NSSet* set=[NSSet setWithObjects:[[User alloc] initwithName:@"孙悟空" andpass:@"234"],
[[User alloc] initwithName:@"孙悟空" andpass:@"234"],
[[User alloc] initwithName:@"金角大王" andpass:@"1234"],
[[User alloc] initwithName:@"猪八戒" andpass:@"2334"],
[[User alloc] initwithName:@"太上老君" andpass:@"254"],
[[User alloc] initwithName:@"银角大王" andpass:@"235"],
nil];
//创建谓词,要求该对象的name值包含大王
NSPredicate* pred2=[NSPredicate predicateWithFormat:@"name CONTAINS '大王'"];
//执行过滤,过滤后集合中只剩下两个元素
NSSet* newSet=[set filteredSetUsingPredicate:pred2];
NSLog(@"%@",newSet);
}
}
输出:
2020-05-24 00:42:08.651228+0800 EX003[2115:61811] 值大于50的元素(
55,
340
)
2020-05-24 00:42:08.651697+0800 EX003[2115:61811] {(
User的name=银角大王,pass=235,
User的name=金角大王,pass=1234
)}
在谓词表达式中使用变量,则可以考虑在谓词表达式中使用占位符参数,在谓词表达式中支持一下两个占位符
%K:该占位符用于动态传入属性名
%@:该占位符用于动态设置属性值
最后可以使用动态改变的属性值,$SUBSTR
具体方法
1、先调用nspredict的predicatewithSubstitutionVariables:方法为占位符参数设置参数值,改方法返回一个可用的nspredict对象
2、直接调用nspredict的evaluateWithObject:substitutionVariables:方法同事完成参数设置和计算结果
@interface User:NSObject
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* pass;
-(id)initwithName:(NSString*)name andpass:(NSString*)pass;
-(void)say:(NSString*) content;
@end
@implementation User
-(id)initwithName:(NSString*)name andpass:(NSString*)pass{
self.name=name;
self.pass=pass;
return self;
}
-(void)say:(NSString*) content{
NSLog(@"%@ say %@",self.name,content);
}
-(NSString*) description{
return [NSString stringWithFormat:@"User的name=%@,pass=%@",self.name,self.pass];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSSet* set=[NSSet setWithObjects:[[User alloc] initwithName:@"孙悟空" andpass:@"234"],
[[User alloc] initwithName:@"孙悟空" andpass:@"234"],
[[User alloc] initwithName:@"金角大王" andpass:@"1234"],
[[User alloc] initwithName:@"猪八戒" andpass:@"2334"],
[[User alloc] initwithName:@"太上老君" andpass:@"254"],
[[User alloc] initwithName:@"银角大王" andpass:@"235"],
nil];
NSString* propPath=@"name";
NSString* value=@"大王";
//创建谓词,谓词包含了两个占位符,后面的两个变量用于为占位符设置参数值
NSPredicate* pred=[NSPredicate predicateWithFormat:@"%K CONTAINS %@",propPath,value];
//执行过滤,过滤后集合中只剩下两个元素
NSSet* newSet=[set filteredSetUsingPredicate:pred];
NSLog(@"%@",newSet);
//创建谓词,谓词包含了两个占位符,使用%K占位符,概占位符使用pass代替,要求被比较对象的pass包含$SUBSTR
NSPredicate* pred2=[NSPredicate predicateWithFormat:@"%K CONTAINS $SUBSTR",@"pass"];
//使用nsdictionary制定substr的值为34
NSPredicate* pred1=[pred2 predicateWithSubstitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys:@"34",@"SUBSTR", nil]];
//执行过滤,过滤后集合中只剩下4个元素
NSSet* newSet1=[set filteredSetUsingPredicate:pred1];
NSLog(@"%@",newSet1);
}
}
输出:
2020-05-24 01:05:31.266075+0800 EX003[2202:68939] {(
User的name=金角大王,pass=1234,
User的name=银角大王,pass=235
)}
2020-05-24 01:05:31.266449+0800 EX003[2202:68939] {(
User的name=猪八戒,pass=2334,
User的name=孙悟空,pass=234,
User的name=金角大王,pass=1234,
User的name=孙悟空,pass=234
)}