OC第四讲 数组字典集.docx
- 文档编号:5012424
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:14
- 大小:19.40KB
OC第四讲 数组字典集.docx
《OC第四讲 数组字典集.docx》由会员分享,可在线阅读,更多相关《OC第四讲 数组字典集.docx(14页珍藏版)》请在冰豆网上搜索。
OC第四讲数组字典集
OC第四讲数组、字典、集
1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())
3、创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组
4、选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值
5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组
王政-成都睿峰科技有限公15:
25:
29
研究性知识点:
1、NSSetKVC
*/
//main.m
//Homework4
//
//Createdbyrimion15/5/21.
//Copyright(c)2015年rimi.Allrightsreserved.
//
#import
#import"Student.h"
intmain(intargc,constchar*argv[]){
@autoreleasepool{
#pragmamark--1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
NSString*string=@"123-456-789-000";
//替换
NSString*newString=[stringstringByReplacingOccurrencesOfString:
@"-"withString:
@""];
NSLog(@"newString=%@",newString);
//可变字符串删除
//遍历可变字符串,进行删除
#pragmamark--2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())
//2
//NSMutableArray*mutableArray=[NSMutableArrayarrayWithCapacity:
100];
//for(inti=0;i<100;i++){
//NSString*string=[NSStringstringWithFormat:
@"%d",arc4random()%51+50];
//[mutableArrayaddObject:
string];
//}
////NSLog(@"compare=%@",[mutableArraysortedArrayUsingSelector:
@selector(compare:
)]);
//NSLog(@"mutableString=%@",mutableArray);
//NSLog(@"fjjj=%ld",mutableArray.count);
//
//NSSet*set=[NSSetsetWithArray:
mutableArray];
//NSLog(@"set=%@",set);
#pragmamark-3、创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组
//4、选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值
//5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组
Student*student1=[StudentstudentWithName:
@"tom1"
age:
20
gender:
@"男"
code:
@"110101199403021210"];
Student*student2=[[Studentalloc]initWithName:
@"tom2"
age:
14
gender:
@"女"
code:
@"110101200010100303"];
Student*student3=[StudentstudentWithName:
@"tom3"
age:
12
gender:
@"男"
code:
@"11010120120303101"];
NSDictionary*dictionary1=@{student1.name:
student1};
NSDictionary*dictionary2=@{student2.name:
student2};
NSDictionary*dictionary3=@{student3.name:
student3};
NSArray*array=@[dictionary1,dictionary2,dictionary3];
NSLog(@"array=%@",array);
Student*student=[[Studentalloc]init];
intresult=[studentdifferenceBetweenAges:
array];
NSLog(@"result=%d",result);
}
return0;
}
Student.h文件
#import
@interfaceStudent:
NSObject
{
NSString*_name;//姓名
NSInteger_age;//年龄
NSString*_gender;//性别
NSString*_code;//身份证号
}
-(instancetype)initWithName:
(NSString*)name
age:
(NSInteger)age
gender:
(NSString*)gender
code:
(NSString*)code;
+(instancetype)studentWithName:
(NSString*)name
age:
(NSInteger)age
gender:
(NSString*)gender
code:
(NSString*)code;
-(void)setName:
(NSString*)name;
-(NSString*)name;
-(NSInteger)age;
-(int)differenceBetweenAges:
(NSArray*)array;
-(NSComparisonResult)compareAge:
(Student*)student;
@end
Student.m文件
#import"Student.h"
@implementationStudent
-(instancetype)initWithName:
(NSString*)name
age:
(NSInteger)age
gender:
(NSString*)gender
code:
(NSString*)code
{
//NSLog(@"%s",__FUNCTION__);
self=[superinit];
if(self){
_name=name;
_age=age;
_gender=gender;
_code=code;
}
returnself;
}
+(instancetype)studentWithName:
(NSString*)name
age:
(NSInteger)age
gender:
(NSString*)gender
code:
(NSString*)code
{
//NSLog(@"%s",__FUNCTION__);
Student*student=[[Studentalloc]initWithName:
name
age:
age
gender:
gender
code:
code];
returnstudent;
}
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)name
{
return_name;
}
-(NSInteger)age
{
return_age;
}
-(int)differenceBetweenAges:
(NSArray*)array
{
NSMutableArray*array1=[NSMutableArrayarrayWithArray:
array];
NSDictionary*dictionary1=array1[0];
NSLog(@"arr=%@",array1[0]);
Student*student1=[[dictionary1allValues]firstObject];
NSLog(@"value=%@",[dictionary1allValues]);
NSDictionary*dictionary2=array1[2];
Student*student2=[[dictionary2allValues]firstObject];
NSLog(@"student1name=%@,student2name=%@",student1.name,student2.name);
//取到学生年龄
//得到差值
returnabs((int)student1.age-(int)student2.age);
}
-(NSComparisonResult)compareAge:
(Student*)student
{
if(_age returnNSOrderedAscending; } if(_age>student.age){ returnNSOrderedDescending; } returnNSOrderedSame; } -(NSString*)description { return[NSStringstringWithFormat: @"name: %@,age: %ld,gender: %@,code: %@",_name,_age,_gender,_code]; } @end 课堂代码 //main.m //0521 // //Createdbyrimion15/5/21. //Copyright(c)2015年ruifeng.Allrightsreserved. // #import #import"Person.h" intmain(intargc,constchar*argv[]){ @autoreleasepool{ NSString*string=@"abc"; NSString*string1=@"cd"; NSIntegercount=3; NSNumber*number=[NSNumbernumberWithInteger: 3]; NSIntegercounts[3]; //oc中的集合 //1.只能放对象 //2.基本数据类型需要格式化才能放 //3.每个元素都不能为nil,所有元素依次放完,最后添加nil作为结束符 NSArray*array=[NSArrayarrayWithObjects: @0,string,string1,nil]; NSLog(@"%@",array); NSArray*array1=[NSArrayarrayWithObjects: string,string1,nil]; NSLog(@"%@",array); //数组的3个初始化 NSArray*array0=[[NSArrayalloc]initWithObjects: string1,string,nil]; NSArray*array2=@[string,string1];//不要加nil //数组的操作 //取值index idstring4=[arrayobjectAtIndex: 0]; [string4appendFormat: @"ab"]; NSString*string5=[arrayobjectAtIndex: 1];//取到的值必须与存放 [arrayfirstObject]; [arraylastObject]; //截取数组 //NSArray*newArray=[arraysubarrayWithRange: (0,2)]; //拼接数组 NSArray*newArray19=[arrayarrayByAddingObject: @"abc"]; //@"1",@"2",@"3" //@"4",@"5" // NSArray*array7=@[@"1",@"2",@"3"]; //array7 NSArray*array8=[NSArrayarrayWithObjects: @"4",@"5",nil]; NSArray*array9=[array7arrayByAddingObjectsFromArray: array8]; NSLog(@"%@",array9); //数组与字符串的转化 NSString*stringAll=[array9componentsJoinedByString: @"-"]; NSArray*array10=[stringAllcomponentsSeparatedByString: @"-"]; NSLog(@"%@",stringAll); //数组排序 NSArray*numbers=@[@"1",@"01",@"11",@"12",@"4"]; SELsel=@selector(compare: ); NSArray*sortedArray=[numberssortedArrayUsingSelector: sel]; NSLog(@"%@",sortedArray); NSString*text=@"goodmorningiosprogram"; //NSString*chars=@"asfesfiuahgfiasghisag"; //NSArray*charects=[charscomponentsSeparatedByCharactersInSet: ]; //NSLog(@"%@",sortedArray) //将它按照单词首字母排序,并生成新的字符串 NSArray*words=[textcomponentsSeparatedByString: @""];//将语句用空格分割,成单词的数组 //NSLog(@"@",words); NSArray*sortedWords=[wordssortedArrayUsingSelector: @selector(compare: )]; NSString*newText=[sortedWordscomponentsJoinedByString: @""];//用空格 NSLog(@"%@",newText); //============================================================================ //字典索引 //字典: 用键值对(key-value)的形式存放对象数据的集合 NSDictionary*dic=[[NSDictionaryalloc]initWithObjectsAndKeys: @"张三",@"name",nil]; NSDictionary*dic1=[[NSDictionaryalloc]dictionaryWithObjectsAndKeys: @"张三",@"name",nil]; NSDictionary*dic2=@{@"name": @"张三", @"age": @"20"};//key-value,key在前,每个键值对为一个元素 NSLog(@"%@",dic2); NSDictionary*dic3=[[NSDictionarydictionaryWithObjects: @"123",@"456",@"789"]forKeys: @[@"1"@"4"@"7"]]; NSLog(@"%@",dic3); //假设一个人有姓名,年龄, Person*per=[[[Personalloc]init]; per.name=@"Tom"; per.age=20; per.birthday=@"19880920"; NSDictionary*personInfo=[NSDictionarydictionaryWithObjectsAndKeys: per.name,@"name", per.age,@"age", per.birthday,@"birthday",nil]; NSLog("@"%@",personInfo); //建立一个新的Person,将字典中的数据取出来放到它的属性里 Person*newPerson=[[Personalloc]init]; newPerson.name=[personInfoobjectForKey: @"name"]; newPerson.age=[[personInfoobjectForKey: @"age"]integerValue]; newPerson.birthday=[personInfoobjectForKey: @"birthday"]; NSDictionary*personInfo=[NSDictionary] //研究性知识点: KVC //字典中不可能出现相同的key //字典也不能放nil //mutable数组和字典的遍历运算 NSMutableArray*mArray=[NSMutableArrayarray]; [mArrayaddObject: @"abc"]; [mArrayaddObject: @"def"];//数组添加元素,直接加在最后 NSLog(@"%@",mArray); [mArrayremoveObject: @"abc"];//移除所有等于它的元素 [mArrayremoveObjectAtIndex: 0];//只移除那个位置上的元素 NSLog(@"%@",mArry); //可变数组的其他方法 [mArrayinsterObject: @"ghi"atIndex: 0];//插入到指定下标的位置 //排序 [mArraysortUsingSelector: @selector(compare: )]; //将20个1~20的随机数字字符串放到数组,并排序 NSMutableArray*[numberArrayarray]; for(NSIntegeri=0;i<20;i++){ //制造20个随机数数字符串 NSIntegernumber=arc4random()%20+1; NSString*numberString=[NSStringstringWithFormat: @"%ld",number]; [numberArrayaddObject: numberString]; } [numberArraysortUsingSelector: @selector(compare: )]; NSLog(@"%@",numberArray); //将上述数组中,大于10的数去掉 //不能在遍历数组的同时删除元素 //建立标记对象的数组,储存要删除的元素, for(NSIntegeri=0;i<20;i++){ NSString*string=[numberArrayobjectAtIndex: i]; } for(NSString*aNumberinnumberArray){//列举法 if(aNumber.integerValue>10){ [deleteArrayaddObject: aNumber]; } } [numberArrayremoveObjectsInArray: deleteArray]; NSLog(@"%@",[numberArray]; //NSMutableDictionary NSMutableDictionary*mDic=[NSMutableDictionarydictionary]; [mDicsetObject: @"张三"forKey: @"name"]; [mDicremoveObjectForKey: @"name"];//操作字典,绝大部分情况都是操作key //字典的遍历(重点) for(NSString*keyinmDic){ //NSLog(@"%@",sring); NSString*value=[mDicobjectForKey: key]; //如果要删除,保存要删除的key,循环结束之后一次性删 } //idobj[2]={string,string1}; //NSArray*array3=[NSArrayarrayWithObjects: objscount: 2]; } return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- OC第四讲 数组字典集 OC 第四 数组 字典
![提示](https://static.bdocx.com/images/bang_tan.gif)