字符串数组集合OC简单介绍
来源:互联网 发布:linux http server 编辑:程序博客网 时间:2024/06/08 16:13
//1、类合和对象的定义声明和使用。
/*
// insert code here...
// //流程
//1.创建类(commond + n)
//2.写实例变量,在类的接口中(接口就是.h中的@interface..@end).注意:大括号.(@public)
//3.在接口中写方法的声明(在实例变量的大括号外面).
//4.写方法的实现,(在相应的.m文件中的@implementation...@end中间).
//5.赋初值(在类的实现部分,就是类的.m中写 init方法,在init方法中给类的实例变量赋值,这里只是初始值).
//6.回到main.m中(首先引用#import)
//7.创建对象(Person *person1 = [[Person alloc] init])
//8.赋值方法:给实例变量赋值(person1->_name)
//9.调用类中定义的方法.(只能调用自己类中已经定义好的方法),
//其他的知识点
//1.输出用 NSLog
//2.字符串前面的@
//3.字符串输出时的占位符是:%@
//4.创建对象的时候前面的*号
//5.@public
//6.给方法发送消息用中括号--[]
Student *stu=[[Student alloc] init];//创建对象。
[stu printStudents];//发送消息输出第一种方法赋初值的结果。
stu->_name=@"zhangsan";//第二种方法给实例变量赋值。
stu->sex='f';//给实例变量赋值。
stu->age=18;//给实例变量赋值。
stu->score=99.9;//给实例变量赋值。
[stu printStudents];//发送消息输出第二种方法赋值的结果。
NSLog(@"%@",[stu getName]);//get方法取值。
[stu setName:@"komng"]; //set方法赋值。
NSLog(@"%@",[stu getName]);
NSLog(@"%@",[stu getSex]);
[stu setSex:@"f"];
NSLog(@"%@",[stu getSex]);
Student *str=[[Student alloc] initWithName:@"hongji" sex1:@"f"];//自定义赋值。
NSLog(@"%@",[str getName]);
NSLog(@"%@",[str getSex]);
*/
//2、试用便利构造器创造对象(为类方法+)。
/*
Teacher *p1=[Teacher teacherWithName:@"hongming" sex:@"m" age:22]; //父类便利构造器。
Students *p2=[Students studentsWithNmae:@"jiuna" sex:@"f" age:21 classnum:@"2344567" num:@"89"];//子类便利构造器。
*/
//不可变字符串。
/*
NSString *str=@"qwert";
[str length];//求字符串长度。
[str substringFromIndex:2];//获取字符串从下标为2的元素开始。
[str substringToIndex:2];//获取字符串从下标0开始到下标2结束但不包含下标为2的元素。
[str substringWithRange:NSMakeRange(0, 3)];//获取字符串从下标为0开始往后截取3个字符串结束。(NSMakeRange为确定下标的范围。)
[str stringByAppendingString:@"123"];//将字符串@“123”拼接在字符串str后面生成一个新的字符串。
[str stringByReplacingOccurrencesOfString:@"qw" withString:@"yk"];//将字符串str中的字符@“qw”替换成@“yk”。(替换掉一个字符串中的一小段字符。)
[str isEqualToString:@"qw"];//判断字符串str是否和字符@“qw”相等(返回类型为BOOL类常用于if语句的判断。)
[str hasPrefix:@"qw"];//判断字符串str开头字符是不是以@“qw”开头的。(返回类型为BOOL类常用于if语句的判断。)
[str hasSuffix:@"rt"];//判断字符串str结尾字符是不是以@“qw”结尾的。(返回类型为BOOL类常用于if语句的判断。)
[@"qwe" compare:@"asd"];//判断两个字符串的大小,=1则后面的大于前面的,=0则两个相等,=-1则后面的小于前面的。
//可变字符串。
NSMutableString *str1=[NSMutableString stringWithString:@"abcdefg"];//可变字符串的定义。
[str1 appendString:@"good like"];//将字符串@“good like”拼接在字符串str1前面。
[str1 insertString:@"2222" atIndex:5];//将字符串@“2222”插入在字符串str1下标为5的字符后面。
[str1 deleteCharactersInRange:NSMakeRange(2, 5)];//删除字符串str1中从下标为2的开始后面5个字符。
//数组不可变数组。
NSArray *array=[NSArray arrayWithObjects:@"zxcv",@"2222",@"lkjj",@"4444", nil];//定义不可变数组。
[array objectAtIndex:1];//取出数组中下标为1的数组元素。
[array arrayByAddingObject:@"999"];//将数组元素@“999”添加到数组array中生成新的数组。
[array arrayByAddingObjectsFromArray:array];//将一个数组添加到另一个数组中形成新的数组。
[array indexofobject:@"2222"];//获取元素@”2222“的下标值。
//可变数组。
NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"111",@"222",@"333",@"444", nil];//定义可变数组。
[array1 addObject:@"555"];//将元素@“555”添加到数组array1中。
[array1 removeObject:@"111"];//将数组中@“111”元素删除。
[array1 removeLastObject];//将数组中最后一个元素删除。
//[array1 removeAllObjects];//将数组中的所有元素删除。
[array1 exchangeObjectAtIndex:0 withObjectAtIndex:1];//将数组中元素下标为2和3的元素进行交换。
//值对象。
NSNumber *num=[NSNumber numberWithInt:12];//完成数值标量和对象类型的互转
[num integerValue];//将对象值转换成数值标量。
//字典不可变字典。
NSDictionary *dict=[NSDictionary dictionaryWithObject:@"zhangsan" forKey:@"name"];//定义不可变字典。
NSLog(@"%@",dict);
NSArray *array2=[NSArray arrayWithObjects:@"zhangsan",@"22",@"man", nil];
NSArray *array3=[NSArray arrayWithObjects:@"name",@"age",@"sex", nil];
NSDictionary *dict1=[NSDictionary dictionaryWithObjects:array2 forKeys:array3];//将两个数组放入字典中。
NSLog(@"%@",dict1);
NSDictionary *dict2=[NSDictionary dictionaryWithDictionary:dict];//将字典dict放入字典dict2中。(将一个字典放入一个新的字典中。)
[dict objectForKey:@"name"];//通过建key在字典中找到对应的元素。(通过name 找到zhangsan);
[dict count];//计算值的个数。
[dict2 allKeys];//取字典中所有的建。
[dict2 allValues];//取字典中所有的值。
//可变字典。
NSMutableDictionary *dict3=[NSMutableDictionary dictionaryWithObject:@"wangwu" forKey:@"name"];//定义可变字典。
NSMutableDictionary *dict4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"man",@"sex",@"23",@"age", nil];//另一种定义字典方法。
[dict3 setObject:@"zhangsan" forKey:@"name"];//添加一对建和值,但值不能为空nil,重复添加会覆盖掉原来的值。
[dict3 setValue:nil forKey:@"name"];//添加新的一对建和值,可以传空值。更为高级。
[dict3 removeObjectForKey:@"name"];//删除字典中的name对应的值。
//快速枚举。(for in).
NSArray *arr=[NSArray arrayWithObjects:@"1111",@"2222",@"333",@"4444", nil];
for (NSArray *art in arr){
NSLog(@"%@",art);
}
for (NSMutableDictionary *dic in dict4){
NSLog(@"%@",[dict4 objectForKey:dic]);
}
NSMutableDictionary *a=[NSMutableDictionary dictionaryWithObject:@"sanfen" forKey:@"name"];
NSMutableDictionary *b=[NSMutableDictionary dictionaryWithObject:@"man" forKey:@"sex"];
NSMutableDictionary *c=[NSMutableDictionary dictionaryWithObject:@"23" forKey:@"age"];
NSMutableArray *arry=[NSMutableArray arrayWithObjects:a,b,c, nil];
for (NSMutableDictionary *dic in arry)
{NSLog(@"%@",dic);}
//不可变集合。
NSArray *rry=[NSArray arrayWithObjects:@"211",@"222",@"333",@"121", nil];
NSSet *set=[NSSet setWithArray:rry];//创建集合,集合中元素不能重复且无序排列(将数组放入集合)。
NSSet *set2=[NSSet setWithObjects:@"qwe",@"eret",@"666",@"444", nil];//创建集合。
NSLog(@"%@ %@",set,set2);
[set2 anyObject];
[set2 member:@"qwe"];//判断集合中是否有元素@“qwe”,有则返回这个元素,没有则返回空null.
[set2 containsObject:@"qwe"];//判断集合中是否有@“qwe”,有返回1,没有返回0.(为BOOL类型)。
//可变集合。
NSMutableSet *set3=[NSMutableSet setWithObjects:@"222",@"rtre",@"6666",@"czv", nil];//可变集合的创建。
[set3 addObject:@"333"];//将元素添加到集合中。
[set3 removeObject:@"222"];//删除@“222”元素。
NSCountedSet *set4=[NSCountedSet setWithObjects:@"sa",@"dfg",@"ewr",@"czzc", nil];//创建元素出现次数的集合。
NSLog(@"%@",set4);
//数组冒泡。
NSMutableArray *rray1=[NSMutableArray arrayWithObjects:@"21",@"43",@"21",@"65",@"45",@"13", nil];
// for(int i=0;i<[rray1 count]-1;i++){
// for(int j=0;j<[rray1 count]-1-i;j++){
// if([[array1 objectAtIndex:j]compare: [array1 objectAtIndex:j+1] ]==1){
// [array1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
// }
// }
// }
//方法:数组排序方法。(sortUsingSelector)冒泡排序.
[rray1 sortUsingSelector: @selector(compare:)]; //方法选择器selector.
NSLog(@"%@",rray1);
*/
//3用字典和bolck进行排序。
/*
NSMutableDictionary*dic1=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20012",@"classnum", nil];
NSMutableDictionary *dic2=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",@"20006",@"classnum", nil];
NSMutableDictionary *dic3=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"wangwu",@"name",@"20078",@"classnum", nil];
NSMutableDictionary *dic4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"kongjiu",@"name",@"20000",@"classnum", nil];
NSMutableArray *rry1=[NSMutableArray arrayWithObjects:dic1,dic2,dic3,dic4, nil];
NSComparisonResult (^myblok)(NSMutableDictionary *,NSMutableDictionary *)=^(NSMutableDictionary *a1,NSMutableDictionary *a2){
NSString *str1=[a1 objectForKey:@"classnum"];
NSString *str2=[a2 objectForKey:@"classnum"];
return -[str1 compare:str2];
};
NSLog(@"%@",[rry1 sortedArrayUsingComparator:myblok]);
*/
//4、用类和数组和Block进行排序。
/*
Students1 *stu1=[Students1 studentWithName:@"zhangsan" calss:@"000112"];
Students1 *stu2=[Students1 studentWithName:@"lisi" calss:@"000102"];
Students1 *stu3=[Students1 studentWithName:@"wangwu" calss:@"000123"];
NSMutableArray *array=[NSMutableArray arrayWithObjects:stu1,stu2,stu3, nil];
NSComparisonResult (^mybook)(Students1 *,Students1 *)=^(Students1 *str1,Students1 *str2){
NSString *stu1=[str1 getName];//控制由姓名进行排序。
NSString *stu2=[str2 getName];
// NSString *stu1=[str1 getCalss];//控制学号的排序
// NSString *stu2=[str2 getCalss];
return +[stu1 compare:stu2];//加-或+可以控制升序降序,-降序,+升序可省略。
};
NSArray *p1=[array sortedArrayUsingComparator:mybook] ;//将得到的字符串放入一个新的数组。
for (int i=0; i<[array count]; i++) {
NSLog(@"姓名:%@ 学号:%@",[p1[i] getName],[p1[i] getCalss]);
}
*/
//5、字面量的使用。
/*
NSString *str=[NSString stringWithUTF8String:"Hello word"];//等价于下面写法。
NSString *str1=@"Hello word";
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];//等价于arr的写法。
NSArray *arr=@[@"1",@"2",@"3"];
[array objectAtIndex:1];//等价于array[1]的写法。
//array[1];
NSDictionary *dict=[NSDictionary dictionaryWithObjects:@[@"zhangsan",@"23"] forKeys:@[@"name",@"age"]];//等价于dict1
NSDictionary *dict1=@{@"name":@"zhangsan",@"age":@"23"};
NSDictionary *dict2=[NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"23",@"age", nil];//dict2==dict3;
NSDictionary *dict3=@{@"name":@"zhangsan",@"age":@"23"};
[dict2 valueForKey:@"name"];//等价于下面写法。
dict2[@"name"];
*/
//6、用block判断一个数组中是否有和一个字符串相同的元素。有返会这个元素下标,没有返回-1.
/*
NSUInteger (^b2)(NSArray *,NSString *)=^(NSArray *am,NSString *sm){
if ([am containsObject:sm]) {
return [am indexOfObject:sm];
}
else
{
return (NSUInteger)(-1);
}
};
NSLog(@"%ld",b2(@[@"aa",@"sd",@"er",@"ad"],@"ad"));
*/
//7、用block判断两个字符串是否相等。
/*
BOOL (^mu)(NSString *,NSString *)=^(NSString *a1,NSString *b1){
return [a1 isEqualToString:b1];
};
NSLog(@"%d",mu(@"asda",@"asd"));
*/
//8、用block对数组进行排序。
/*
NSArray *arry=@[@"123",@"21",@"33",@"69",@"108",@"256"];
NSComparisonResult (^paixiu)(NSString *,NSString *)=^(NSString *str1,NSString *str2){
return [str1 compare:str2];
};
NSArray *p=[arry sortedArrayUsingComparator:paixiu];
NSLog(@"%@",p);
*/
//9、使用代理类进行任务再次分配,有参数代理实现转换时间和字符时间。
/*
NSString *string=@"2013-05-30 15:45:32";//字符时间。
Muqing *mq=[[Muqing alloc] init];//创建一个母亲对象
Shouliren *sr=[[Shouliren alloc] init];//创建一个受理人对象。
[mq setDelegate:sr string:string];//两者进行代理授权并传入参数。
[mq goDOboyy];///给受理人发送消息执行命令。
[mq goDocook];//给受理人发送消息执行命令。
[mq goZhuanHuan];//将字符时间转换成时间输出。
[mq goZhuanHuan1];//将时间转换成字符串输出。
*/
//10.类的扩展。
/*
//方法1 获取当前时间
NSDate *date1=[NSDate date];
NSLog(@"%@",date1);
//方法2 在当前时间上加/减一定的时间(就是前后的参数,以秒为单位。)
NSDate *date2=[NSDate dateWithTimeIntervalSinceNow:8*60*60];
NSLog(@"%@",date2);
NSTimeInterval t=[ date1 timeIntervalSinceDate: date2];//时间1和2相差多少时间(秒)
NSLog(@"%f",t/3600);
*/
//11.把时间转换成字符串。
/*
NSDate *date=[NSDate date];//获取当前系统的时间。
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];//创建时间格式的对象。
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];//设定时间格式。(yyyy-MM-dd HH:mm:ss 为24小时制)例题中为12小时制。
NSString *string=[formatter stringFromDate:date];//将获取的时间转换成字符串。
NSLog(@"%@",string);
*/
//12.把字符串转化成时间。(字符串时间格式要与设置的时间格式相同)
/*
NSDateFormatter *formatter1=[[NSDateFormatter alloc] init];//设置时间格式对象。
[formatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss a" ];//设置时间格式
NSString *string1=@"2008-08-23 01:23:35 下午";//字符串时间。
NSDate *date1=[formatter1 dateFromString:string1];//将对应字符串转换成相应格式时间。
NSLog(@"%@",date1);//由于默认时区为+0000,输出结果会有误差。
*/
//13、使用category类方法转换时间。(此方法只能添加方法,不能添加实例变量,不需要源代码,自己额外添加的方法根据需要。)。
/*
NSString *str=@"2015-3-12 22:35:45";//定义一个时间字符串。
[NSString zhuanhuan:str];//发送消息,传入时间字符串。
*/
//14.属性使用。
/*
Shuxing *p1=[[Shuxing alloc] init];//定义对象。
p1._name=@"zhangsan";//给实例变量赋值这里相当于set方法。
p1._sex=@"man";
p1.age=23;
NSLog(@"%@ %@ %d",p1._name,p1._sex,p1.age);//从实例变量取值,这里相当于get方法。
*/
//15.Kvc方法使用。
/*
//第一种形式。
//是用来给实例变量赋值取值的(在没有get ,set 方法时)
//key一定是和实例变量一样,(下划线可以,没有。)
Kvc *kvc1=[[Kvc alloc] init];
[kvc1 setValue:@"zhangsan" forKey:@"name"];
[kvc1 setValue:@"man" forKey:@"sex"];
NSLog(@"%@ %@",[kvc1 valueForKey:@"name"],[kvc1 valueForKey:@"sex"]);
*/
/*
// insert code here...
// //流程
//1.创建类(commond + n)
//2.写实例变量,在类的接口中(接口就是.h中的@interface..@end).注意:大括号.(@public)
//3.在接口中写方法的声明(在实例变量的大括号外面).
//4.写方法的实现,(在相应的.m文件中的@implementation...@end中间).
//5.赋初值(在类的实现部分,就是类的.m中写 init方法,在init方法中给类的实例变量赋值,这里只是初始值).
//6.回到main.m中(首先引用#import)
//7.创建对象(Person *person1 = [[Person alloc] init])
//8.赋值方法:给实例变量赋值(person1->_name)
//9.调用类中定义的方法.(只能调用自己类中已经定义好的方法),
//其他的知识点
//1.输出用 NSLog
//2.字符串前面的@
//3.字符串输出时的占位符是:%@
//4.创建对象的时候前面的*号
//5.@public
//6.给方法发送消息用中括号--[]
Student *stu=[[Student alloc] init];//创建对象。
[stu printStudents];//发送消息输出第一种方法赋初值的结果。
stu->_name=@"zhangsan";//第二种方法给实例变量赋值。
stu->sex='f';//给实例变量赋值。
stu->age=18;//给实例变量赋值。
stu->score=99.9;//给实例变量赋值。
[stu printStudents];//发送消息输出第二种方法赋值的结果。
NSLog(@"%@",[stu getName]);//get方法取值。
[stu setName:@"komng"]; //set方法赋值。
NSLog(@"%@",[stu getName]);
NSLog(@"%@",[stu getSex]);
[stu setSex:@"f"];
NSLog(@"%@",[stu getSex]);
Student *str=[[Student alloc] initWithName:@"hongji" sex1:@"f"];//自定义赋值。
NSLog(@"%@",[str getName]);
NSLog(@"%@",[str getSex]);
*/
//2、试用便利构造器创造对象(为类方法+)。
/*
Teacher *p1=[Teacher teacherWithName:@"hongming" sex:@"m" age:22]; //父类便利构造器。
Students *p2=[Students studentsWithNmae:@"jiuna" sex:@"f" age:21 classnum:@"2344567" num:@"89"];//子类便利构造器。
*/
//不可变字符串。
/*
NSString *str=@"qwert";
[str length];//求字符串长度。
[str substringFromIndex:2];//获取字符串从下标为2的元素开始。
[str substringToIndex:2];//获取字符串从下标0开始到下标2结束但不包含下标为2的元素。
[str substringWithRange:NSMakeRange(0, 3)];//获取字符串从下标为0开始往后截取3个字符串结束。(NSMakeRange为确定下标的范围。)
[str stringByAppendingString:@"123"];//将字符串@“123”拼接在字符串str后面生成一个新的字符串。
[str stringByReplacingOccurrencesOfString:@"qw" withString:@"yk"];//将字符串str中的字符@“qw”替换成@“yk”。(替换掉一个字符串中的一小段字符。)
[str isEqualToString:@"qw"];//判断字符串str是否和字符@“qw”相等(返回类型为BOOL类常用于if语句的判断。)
[str hasPrefix:@"qw"];//判断字符串str开头字符是不是以@“qw”开头的。(返回类型为BOOL类常用于if语句的判断。)
[str hasSuffix:@"rt"];//判断字符串str结尾字符是不是以@“qw”结尾的。(返回类型为BOOL类常用于if语句的判断。)
[@"qwe" compare:@"asd"];//判断两个字符串的大小,=1则后面的大于前面的,=0则两个相等,=-1则后面的小于前面的。
//可变字符串。
NSMutableString *str1=[NSMutableString stringWithString:@"abcdefg"];//可变字符串的定义。
[str1 appendString:@"good like"];//将字符串@“good like”拼接在字符串str1前面。
[str1 insertString:@"2222" atIndex:5];//将字符串@“2222”插入在字符串str1下标为5的字符后面。
[str1 deleteCharactersInRange:NSMakeRange(2, 5)];//删除字符串str1中从下标为2的开始后面5个字符。
//数组不可变数组。
NSArray *array=[NSArray arrayWithObjects:@"zxcv",@"2222",@"lkjj",@"4444", nil];//定义不可变数组。
[array objectAtIndex:1];//取出数组中下标为1的数组元素。
[array arrayByAddingObject:@"999"];//将数组元素@“999”添加到数组array中生成新的数组。
[array arrayByAddingObjectsFromArray:array];//将一个数组添加到另一个数组中形成新的数组。
[array indexofobject:@"2222"];//获取元素@”2222“的下标值。
//可变数组。
NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"111",@"222",@"333",@"444", nil];//定义可变数组。
[array1 addObject:@"555"];//将元素@“555”添加到数组array1中。
[array1 removeObject:@"111"];//将数组中@“111”元素删除。
[array1 removeLastObject];//将数组中最后一个元素删除。
//[array1 removeAllObjects];//将数组中的所有元素删除。
[array1 exchangeObjectAtIndex:0 withObjectAtIndex:1];//将数组中元素下标为2和3的元素进行交换。
//值对象。
NSNumber *num=[NSNumber numberWithInt:12];//完成数值标量和对象类型的互转
[num integerValue];//将对象值转换成数值标量。
//字典不可变字典。
NSDictionary *dict=[NSDictionary dictionaryWithObject:@"zhangsan" forKey:@"name"];//定义不可变字典。
NSLog(@"%@",dict);
NSArray *array2=[NSArray arrayWithObjects:@"zhangsan",@"22",@"man", nil];
NSArray *array3=[NSArray arrayWithObjects:@"name",@"age",@"sex", nil];
NSDictionary *dict1=[NSDictionary dictionaryWithObjects:array2 forKeys:array3];//将两个数组放入字典中。
NSLog(@"%@",dict1);
NSDictionary *dict2=[NSDictionary dictionaryWithDictionary:dict];//将字典dict放入字典dict2中。(将一个字典放入一个新的字典中。)
[dict objectForKey:@"name"];//通过建key在字典中找到对应的元素。(通过name 找到zhangsan);
[dict count];//计算值的个数。
[dict2 allKeys];//取字典中所有的建。
[dict2 allValues];//取字典中所有的值。
//可变字典。
NSMutableDictionary *dict3=[NSMutableDictionary dictionaryWithObject:@"wangwu" forKey:@"name"];//定义可变字典。
NSMutableDictionary *dict4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"man",@"sex",@"23",@"age", nil];//另一种定义字典方法。
[dict3 setObject:@"zhangsan" forKey:@"name"];//添加一对建和值,但值不能为空nil,重复添加会覆盖掉原来的值。
[dict3 setValue:nil forKey:@"name"];//添加新的一对建和值,可以传空值。更为高级。
[dict3 removeObjectForKey:@"name"];//删除字典中的name对应的值。
//快速枚举。(for in).
NSArray *arr=[NSArray arrayWithObjects:@"1111",@"2222",@"333",@"4444", nil];
for (NSArray *art in arr){
NSLog(@"%@",art);
}
for (NSMutableDictionary *dic in dict4){
NSLog(@"%@",[dict4 objectForKey:dic]);
}
NSMutableDictionary *a=[NSMutableDictionary dictionaryWithObject:@"sanfen" forKey:@"name"];
NSMutableDictionary *b=[NSMutableDictionary dictionaryWithObject:@"man" forKey:@"sex"];
NSMutableDictionary *c=[NSMutableDictionary dictionaryWithObject:@"23" forKey:@"age"];
NSMutableArray *arry=[NSMutableArray arrayWithObjects:a,b,c, nil];
for (NSMutableDictionary *dic in arry)
{NSLog(@"%@",dic);}
//不可变集合。
NSArray *rry=[NSArray arrayWithObjects:@"211",@"222",@"333",@"121", nil];
NSSet *set=[NSSet setWithArray:rry];//创建集合,集合中元素不能重复且无序排列(将数组放入集合)。
NSSet *set2=[NSSet setWithObjects:@"qwe",@"eret",@"666",@"444", nil];//创建集合。
NSLog(@"%@ %@",set,set2);
[set2 anyObject];
[set2 member:@"qwe"];//判断集合中是否有元素@“qwe”,有则返回这个元素,没有则返回空null.
[set2 containsObject:@"qwe"];//判断集合中是否有@“qwe”,有返回1,没有返回0.(为BOOL类型)。
//可变集合。
NSMutableSet *set3=[NSMutableSet setWithObjects:@"222",@"rtre",@"6666",@"czv", nil];//可变集合的创建。
[set3 addObject:@"333"];//将元素添加到集合中。
[set3 removeObject:@"222"];//删除@“222”元素。
NSCountedSet *set4=[NSCountedSet setWithObjects:@"sa",@"dfg",@"ewr",@"czzc", nil];//创建元素出现次数的集合。
NSLog(@"%@",set4);
//数组冒泡。
NSMutableArray *rray1=[NSMutableArray arrayWithObjects:@"21",@"43",@"21",@"65",@"45",@"13", nil];
// for(int i=0;i<[rray1 count]-1;i++){
// for(int j=0;j<[rray1 count]-1-i;j++){
// if([[array1 objectAtIndex:j]compare: [array1 objectAtIndex:j+1] ]==1){
// [array1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
// }
// }
// }
//方法:数组排序方法。(sortUsingSelector)冒泡排序.
[rray1 sortUsingSelector: @selector(compare:)]; //方法选择器selector.
NSLog(@"%@",rray1);
*/
//3用字典和bolck进行排序。
/*
NSMutableDictionary*dic1=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"20012",@"classnum", nil];
NSMutableDictionary *dic2=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",@"20006",@"classnum", nil];
NSMutableDictionary *dic3=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"wangwu",@"name",@"20078",@"classnum", nil];
NSMutableDictionary *dic4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"kongjiu",@"name",@"20000",@"classnum", nil];
NSMutableArray *rry1=[NSMutableArray arrayWithObjects:dic1,dic2,dic3,dic4, nil];
NSComparisonResult (^myblok)(NSMutableDictionary *,NSMutableDictionary *)=^(NSMutableDictionary *a1,NSMutableDictionary *a2){
NSString *str1=[a1 objectForKey:@"classnum"];
NSString *str2=[a2 objectForKey:@"classnum"];
return -[str1 compare:str2];
};
NSLog(@"%@",[rry1 sortedArrayUsingComparator:myblok]);
*/
//4、用类和数组和Block进行排序。
/*
Students1 *stu1=[Students1 studentWithName:@"zhangsan" calss:@"000112"];
Students1 *stu2=[Students1 studentWithName:@"lisi" calss:@"000102"];
Students1 *stu3=[Students1 studentWithName:@"wangwu" calss:@"000123"];
NSMutableArray *array=[NSMutableArray arrayWithObjects:stu1,stu2,stu3, nil];
NSComparisonResult (^mybook)(Students1 *,Students1 *)=^(Students1 *str1,Students1 *str2){
NSString *stu1=[str1 getName];//控制由姓名进行排序。
NSString *stu2=[str2 getName];
// NSString *stu1=[str1 getCalss];//控制学号的排序
// NSString *stu2=[str2 getCalss];
return +[stu1 compare:stu2];//加-或+可以控制升序降序,-降序,+升序可省略。
};
NSArray *p1=[array sortedArrayUsingComparator:mybook] ;//将得到的字符串放入一个新的数组。
for (int i=0; i<[array count]; i++) {
NSLog(@"姓名:%@ 学号:%@",[p1[i] getName],[p1[i] getCalss]);
}
*/
//5、字面量的使用。
/*
NSString *str=[NSString stringWithUTF8String:"Hello word"];//等价于下面写法。
NSString *str1=@"Hello word";
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];//等价于arr的写法。
NSArray *arr=@[@"1",@"2",@"3"];
[array objectAtIndex:1];//等价于array[1]的写法。
//array[1];
NSDictionary *dict=[NSDictionary dictionaryWithObjects:@[@"zhangsan",@"23"] forKeys:@[@"name",@"age"]];//等价于dict1
NSDictionary *dict1=@{@"name":@"zhangsan",@"age":@"23"};
NSDictionary *dict2=[NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"23",@"age", nil];//dict2==dict3;
NSDictionary *dict3=@{@"name":@"zhangsan",@"age":@"23"};
[dict2 valueForKey:@"name"];//等价于下面写法。
dict2[@"name"];
*/
//6、用block判断一个数组中是否有和一个字符串相同的元素。有返会这个元素下标,没有返回-1.
/*
NSUInteger (^b2)(NSArray *,NSString *)=^(NSArray *am,NSString *sm){
if ([am containsObject:sm]) {
return [am indexOfObject:sm];
}
else
{
return (NSUInteger)(-1);
}
};
NSLog(@"%ld",b2(@[@"aa",@"sd",@"er",@"ad"],@"ad"));
*/
//7、用block判断两个字符串是否相等。
/*
BOOL (^mu)(NSString *,NSString *)=^(NSString *a1,NSString *b1){
return [a1 isEqualToString:b1];
};
NSLog(@"%d",mu(@"asda",@"asd"));
*/
//8、用block对数组进行排序。
/*
NSArray *arry=@[@"123",@"21",@"33",@"69",@"108",@"256"];
NSComparisonResult (^paixiu)(NSString *,NSString *)=^(NSString *str1,NSString *str2){
return [str1 compare:str2];
};
NSArray *p=[arry sortedArrayUsingComparator:paixiu];
NSLog(@"%@",p);
*/
//9、使用代理类进行任务再次分配,有参数代理实现转换时间和字符时间。
/*
NSString *string=@"2013-05-30 15:45:32";//字符时间。
Muqing *mq=[[Muqing alloc] init];//创建一个母亲对象
Shouliren *sr=[[Shouliren alloc] init];//创建一个受理人对象。
[mq setDelegate:sr string:string];//两者进行代理授权并传入参数。
[mq goDOboyy];///给受理人发送消息执行命令。
[mq goDocook];//给受理人发送消息执行命令。
[mq goZhuanHuan];//将字符时间转换成时间输出。
[mq goZhuanHuan1];//将时间转换成字符串输出。
*/
//10.类的扩展。
/*
//方法1 获取当前时间
NSDate *date1=[NSDate date];
NSLog(@"%@",date1);
//方法2 在当前时间上加/减一定的时间(就是前后的参数,以秒为单位。)
NSDate *date2=[NSDate dateWithTimeIntervalSinceNow:8*60*60];
NSLog(@"%@",date2);
NSTimeInterval t=[ date1 timeIntervalSinceDate: date2];//时间1和2相差多少时间(秒)
NSLog(@"%f",t/3600);
*/
//11.把时间转换成字符串。
/*
NSDate *date=[NSDate date];//获取当前系统的时间。
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];//创建时间格式的对象。
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];//设定时间格式。(yyyy-MM-dd HH:mm:ss 为24小时制)例题中为12小时制。
NSString *string=[formatter stringFromDate:date];//将获取的时间转换成字符串。
NSLog(@"%@",string);
*/
//12.把字符串转化成时间。(字符串时间格式要与设置的时间格式相同)
/*
NSDateFormatter *formatter1=[[NSDateFormatter alloc] init];//设置时间格式对象。
[formatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss a" ];//设置时间格式
NSString *string1=@"2008-08-23 01:23:35 下午";//字符串时间。
NSDate *date1=[formatter1 dateFromString:string1];//将对应字符串转换成相应格式时间。
NSLog(@"%@",date1);//由于默认时区为+0000,输出结果会有误差。
*/
//13、使用category类方法转换时间。(此方法只能添加方法,不能添加实例变量,不需要源代码,自己额外添加的方法根据需要。)。
/*
NSString *str=@"2015-3-12 22:35:45";//定义一个时间字符串。
[NSString zhuanhuan:str];//发送消息,传入时间字符串。
*/
//14.属性使用。
/*
Shuxing *p1=[[Shuxing alloc] init];//定义对象。
p1._name=@"zhangsan";//给实例变量赋值这里相当于set方法。
p1._sex=@"man";
p1.age=23;
NSLog(@"%@ %@ %d",p1._name,p1._sex,p1.age);//从实例变量取值,这里相当于get方法。
*/
//15.Kvc方法使用。
/*
//第一种形式。
//是用来给实例变量赋值取值的(在没有get ,set 方法时)
//key一定是和实例变量一样,(下划线可以,没有。)
Kvc *kvc1=[[Kvc alloc] init];
[kvc1 setValue:@"zhangsan" forKey:@"name"];
[kvc1 setValue:@"man" forKey:@"sex"];
NSLog(@"%@ %@",[kvc1 valueForKey:@"name"],[kvc1 valueForKey:@"sex"]);
*/
0 0
- 字符串数组集合OC简单介绍
- oc 字符串 数组 简单使用
- OC的字符串 数组 字典 集合
- OC 字符串 数组 集合
- OC基础—字符串数组字典集合
- OC初学必看,字符串,数组,字典,集合,NSNumber 等的简单操作
- OC数组字典集合
- OC--数组、字典、集合
- OC-集合类(数组)
- OC-字符串集合补充
- OC字典 OC数组 OC字符串
- OC 数组与字符串
- OC 字符串及数组
- OC字符串和数组
- OC-集合1(数组)
- OC中的数组、字典、集合
- OC - 第五章 集合 ( 数组、字典、集合 )
- oc 字典集合简单使用
- Rect类的使用结果
- (转载)富人孩子和穷人孩子,差的真不仅仅是钱
- Swift(十四、构造过程(1))
- 将社交网络集成到 BPM,第 1 部分: 收集 Twitter 数据
- PostgreSQL新手入门
- 字符串数组集合OC简单介绍
- 把钱投资自己的头脑上(二)
- c 语言中的!和~
- Python 定时执行线程 执行任务
- 黑马程序员_IO流BufferWriter、BufferReader
- 大数据架构和模式(三)理解大数据解决方案的架构层
- 操作系统与网络实现 之九
- 小马哥----高仿小米4 tc01刷机拆机主板图与开机界面图 分联通版与移动版
- 黑马程序员——C语言基础01—C语言概述