OC第四讲 数组字典集文档格式.docx
《OC第四讲 数组字典集文档格式.docx》由会员分享,可在线阅读,更多相关《OC第四讲 数组字典集文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
,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);
fjjj=%ld"
mutableArray.count);
//
//NSSet*set=[NSSetsetWithArray:
mutableArray];
set=%@"
set);
#pragmamark-3、创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组
//4、选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值
//5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组
Student*student1=[StudentstudentWithName:
tom1"
age:
20
gender:
男"
code:
110101199403021210"
Student*student2=[[Studentalloc]initWithName:
tom2"
14
女"
110101200010100303"
Student*student3=[StudentstudentWithName:
tom3"
12
11010120120303101"
NSDictionary*dictionary1=@{student1.name:
student1};
NSDictionary*dictionary2=@{student2.name:
student2};
NSDictionary*dictionary3=@{student3.name:
student3};
NSArray*array=@[dictionary1,dictionary2,dictionary3];
array=%@"
array);
Student*student=[[Studentalloc]init];
intresult=[studentdifferenceBetweenAges:
array];
result=%d"
result);
}
return0;
}
Student.h文件
@interfaceStudent:
NSObject
{
NSString*_name;
//姓名
NSInteger_age;
//年龄
NSString*_gender;
//性别
NSString*_code;
//身份证号
-(instancetype)initWithName:
(NSString*)name
(NSInteger)age
(NSString*)gender
(NSString*)code;
+(instancetype)studentWithName:
-(void)setName:
(NSString*)name;
-(NSString*)name;
-(NSInteger)age;
-(int)differenceBetweenAges:
(NSArray*)array;
-(NSComparisonResult)compareAge:
(Student*)student;
@end
Student.m文件
@implementationStudent
(NSString*)code
//NSLog(@"
%s"
__FUNCTION__);
self=[superinit];
if(self){
_name=name;
_age=age;
_gender=gender;
_code=code;
returnself;
Student*student=[[Studentalloc]initWithName:
name
age
gender
code];
returnstudent;
-(NSString*)name
return_name;
-(NSInteger)age
return_age;
(NSArray*)array
NSMutableArray*array1=[NSMutableArrayarrayWithArray:
NSDictionary*dictionary1=array1[0];
arr=%@"
array1[0]);
Student*student1=[[dictionary1allValues]firstObject];
value=%@"
[dictionary1allValues]);
NSDictionary*dictionary2=array1[2];
Student*student2=[[dictionary2allValues]firstObject];
student1name=%@,student2name=%@"
student1.name,student2.name);
//取到学生年龄
//得到差值
returnabs((int)student1.age-(int)student2.age);
(Student*)student
if(_age<
student.age){
returnNSOrderedAscending;
if(_age>
returnNSOrderedDescending;
returnNSOrderedSame;
-(NSString*)description
return[NSStringstringWithFormat:
name:
%@,age:
%ld,gender:
%@,code:
%@"
_name,_age,_gender,_code];
课堂代码
//0521
//Copyright(c)2015年ruifeng.Allrightsreserved.
Person.h"
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];
NSArray*array1=[NSArrayarrayWithObjects:
string,string1,nil];
//数组的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:
//@"
1"
@"
2"
3"
4"
5"
//
NSArray*array7=@[@"
//array7
NSArray*array8=[NSArrayarrayWithObjects:
nil];
NSArray*array9=[array7arrayByAddingObjectsFromArray:
array8];
array9);
//数组与字符串的转化
NSString*stringAll=[array9componentsJoinedByString:
NSArray*array10=[stringAllcomponentsSeparatedByString:
stringAll);
//数组排序
NSArray*numbers=@[@"
01"
11"
12"
SELsel=@selector(compare:
);
NSArray*sortedArray=[numberssortedArrayUsingSelector:
sel];
sortedArray);
NSString*text=@"
goodmorningiosprogram"
//NSString*chars=@"
asfesfiuahgfiasghisag"
//NSArray*charects=[charscomponentsSeparatedByCharactersInSet:
sortedArray)
//将它按照单词首字母排序,并生成新的字符串
NSArray*words=[textcomponentsSeparatedByString:
"
//将语句用空格分割,成单词的数组
words);
NSArray*sortedWords=[wordssortedArrayUsingSelector:
)];
NSString*newText=[sortedWordscomponentsJoinedByString:
//用空格
newText);
//============================================================================
//字典索引
//字典:
用键值对(key-value)的形式存放对象数据的集合
NSDictionary*dic=[[NSDictionaryalloc]initWithObjectsAndKeys:
张三"
name"
NSDictionary*dic1=[[NSDictionaryalloc]dictionaryWithObjectsAndKeys:
NSDictionary*dic2=@{@"
:
@"
age"
20"
};
//key-value,key在前,每个键值对为一个元素
dic2);
NSDictionary*dic3=[[NSDictionarydictionaryWithObjects:
123"
456"
789"
]forKeys:
@[@"
7"
]];
dic3);
//假设一个人有姓名,年龄,
Person*per=[[[Personalloc]init];
per.name=@"
Tom"
per.age=20;
per.birthday=@"
19880920"
NSDictionary*personInfo=[NSDictionarydictionaryWithObjectsAndKeys:
per.name,@"
per.age,@"
per.birthday,@"
birthday"
NSLog("
personInfo);
//建立一个新的Person,将字典中的数据取出来放到它的属性里
Person*newPerson=[[Personalloc]init];
newPerson.name=[personInfoobjectForKey:
newPerson.age=[[personInfoobjectForKey:
]integerValue];
newPerson.birthday=[personInfoobjectForKey:
NSDictionary*personInfo=[NSDictionary]
//研究性知识点:
KVC
//字典中不可能出现相同的key
//字典也不能放nil
//mutable数组和字典的遍历运算
NSMutableArray*mArray=[NSMutableArrayarray];
[mArrayaddObject:
def"
//数组添加元素,直接加在最后
mArray);
[mArrayremoveObject:
//移除所有等于它的元素
[mArrayremoveObjectAtIndex:
//只移除那个位置上的元素
mArry);
//可变数组的其他方法
[mArrayinsterObject:
ghi"
atIndex:
//插入到指定下标的位置
//排序
[mArraysortUsingSelector:
//将20个1~20的随机数字字符串放到数组,并排序
NSMutableArray*[numberArrayarray];
for(NSIntegeri=0;
i<
20;
i++){
//制造20个随机数数字符串
NSIntegernumber=arc4random()%20+1;
NSString*numberString=[NSStringstringWithFormat:
%ld"
number];
[numberArrayaddObject:
numberString];
[numberArraysortUsingSelector:
numberArray);
//将上述数组中,大于10的数去掉
//不能在遍历数组的同时删除元素
//建立标记对象的数组,储存要删除的元素,
NSString*string=[numberArrayobjectAtIndex:
i];
for(NSString*aNumberinnumberArray){//列举法
if(aNumber.integerValue>
10){
[deleteArrayaddObject:
aNumber];
[numberArrayremoveObjectsInArray:
deleteArray];
[numberArray];
//NSMutableDictionary
NSMutableDictionary*mDic=[NSMutableDictionarydictionary];
[mDicsetObject:
forKey:
[mDicremoveObjectForKey:
//操作字典,绝大部分情况都是操作key
//字典的遍历(重点)
for(NSString*keyinmDic){
sring);
NSString*value=[mDicobjectForKey:
key];
//如果要删除,保存要删除的key,循环结束之后一次性删
//idobj[2]={string,string1};
//NSArray*array3=[NSArrayarrayWithObjects:
objscount:
2];