OC中的集合类:分为三大类:数组NSArray,集NSset,字典NSdictionary,平常使用的集合都是这3类,或其他子类。
***基本数据类型放到集合是坚决不行的!***
1,数组NSArray
NSString *string = @"ab"; //如果nil,不能把string放进去
NSString *string1 = @"cd";
NSInteger count = 3;//基本类型绝对不能放到OC数组,注意!需要格式化才能放,所有元素都不能为nil,所有元素依次放完,最后添加nil作为结束符
NSInteger counts[3];
// NSInteger *number = *[4];
// NSNumber *number = [NSNumber numberWithInteger:count];
// NSArray *array = [NSArray arrayWithObjects:(count),string,nil];
//数组的3个初始化
NSArray *array = [NSArray arrayWithObjects:@(count),string1,nil];
NSArray *array1 = [[NSArray alloc] initWithObjects:string1,string, nil];
NSArray *array2 = @[string,string1]; //不加nil
NSString *obj[2] = {string,string1};
NSArray *array3 = [NSArray arrayWithObjects:obj count:2];
NSLog(@"%@",array2);
//数组操作
//取值 index
NSString *string4 = [array objectAtIndex:1];//取到的值必须与存放数组的值类型一致
NSString *string5 = [string4 stringByAppendingString:@"ab"];
NSLog(@"Hello, World!%@ %@",string4,string5);
[array firstObject];
[array lastObject];
//截取数组
NSArray * newArray= [array subarrayWithRange:NSMakeRange(0, 2)];
//拼接数组
NSArray *newArray1 = [array arrayByAddingObject:@"abc"];
NSArray *array5 = @[@"1",@"2",@"3"];
NSArray *array6 = [[NSArray alloc] initWithObjects:@"4",@"5", nil];
NSArray *newArray2 = [array5 arrayByAddingObject:array6];
NSLog(@"Hello, World!%@",newArray2);
//componentsJoinedByString 分割图片,分割姓名
NSString *stringAll = [array6 componentsJoinedByString:@"-"];
//NSString *array7 = [stringAll componentsJoinedByString:@"-"];
NSLog(@"Hello, World!%@",stringAll);
//数组排序
NSArray *number = @[@"1",@"2",@"4",@"3",@"0"];//比的是字符串
SEL sel = @selector(compare:);//SEL是方法,调用所指的方法
NSArray *sortedArray = [number sortedArrayUsingSelector:sel];
NSLog(@"%@",sortedArray);
NSString *text = @"good moni ios programing a";
//将他按照单词首字母排序,并生成新字符串
NSArray *array7 = [text componentsSeparatedByString:@" "];//字符串放到数组
// NSArray *chars = [text componentsSeparatedByCharactersInSet:array7];
//
// NSLog(@"%@",chars);拆分单个字符排序
SEL sel1 = @selector(compare:);//调用比较方法
NSArray *sorted1 = [array7 sortedArrayUsingSelector:sel1];
NSString *text1 = [sorted1 componentsJoinedByString:@" "];
NSLog(@"%@",text1);
2,可变数组NSMutableArray
NSMutableArray *mArray = [NSMutableArray array];//数组添加元素,直接加到最后
[mArray addObject:@"abc"];
[mArray addObject:@"def"];
[mArray addObject:@"fgh"];
NSLog(@"%@",mArray);
//[mArray removeObjectAtIndex:2];//移除这个下标位置的元素
NSLog(@"%@",mArray);
//[mArray removeObject:@"abc"];//移除所有等于它的元素
NSLog(@"%@",mArray);
//可变数组的其他方法
[mArray insertObject:@"121" atIndex:0]; //atindex插入之后,放到哪个位置
NSLog(@"%@",mArray);
//排序 ,不需要接收返回值,跟不可变的相比
[mArray sortedArrayUsingSelector:@selector(compare:)];//描述已经排好序的数组
NSLog(@"%@",mArray);
//将20个1-20的随机数字字符串放到数组,并排序
NSMutableArray *numRomdom = [NSMutableArray array];
for (NSInteger i = 0; i < 20;i++ ) {
//制造20个随机数字符串
NSInteger number =arc4random()% 20 + 1;
NSString * numRomdomString = [NSString stringWithFormat:@"%ld",number];
[numRomdom addObject:numRomdomString];
[numRomdom sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",numRomdom);
}
for (NSInteger i = 0; i < 20; i ++) {
NSString *string = [numRomdom objectAtIndex:i];
}
//将上述数组中,大于10的数去掉
//不能在遍历的同时,删除元素,思路:1,建立标记对象的数组,存储要删除的元素,结束之后一次性删除
NSMutableArray *deleteArray =[NSMutableArray array];
for (NSString *aNumber in numRomdom) { //列举法
if (aNumber.integerValue > 10) {
[deleteArray addObject:aNumber];
}
}
[numRomdom removeObjectsInArray:deleteArray];
NSLog(@"%@",numRomdom);
3,字典NSDictionary
//字典 用键值对的形式存放对象数据的集合
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"张三",@"name",nil];
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"张五",@"name2",nil];
//字典中不能同时存放两个相同的键值对
NSDictionary *dic2 =@{@"name":@"张三",@"age":@"20"}; //字典中的元素是键值对,不是单个元素
//
NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:@[@"123",@"456",@"789"] forKeys:@[@"1",@"4",@"7"]];
NSLog(@"%@",dic3);
Person *p = [[Person alloc] init];
p.name = @"ron";
p.age = 23;
p.birth =@"19000121";
//基本数据类型,不能往集合里面放,需要转换一下
NSDictionary *personInfo = [NSDictionary dictionaryWithObjectsAndKeys:p.name,@"ron",[NSString stringWithFormat:@"%ld",p.age],23,p.birth,@"birhday", nil];
NSLog(@"%@",personInfo);
//见一个新的person 取字典的值
Person *NewPerson = [[Person alloc]init];
NewPerson.name = [personInfo objectForKey:@"name"];
NewPerson.birth = [personInfo objectForKey:@"birhday"];
NewPerson.age = [[personInfo objectForKey:@"age"]integerValue];//integerValue取值
//*KVC是什么?KVO是什么?有什么特点?
KVC是键值编码,特点是通过指定表示要访问的属性名字的字符串标识符,可以进行类的属性读取和设置
KVO是键值观察,特点是利用键值观察可以注册成为一个对象的观察者,在该对象的某个属性变化时收到通知
*
//字典中不放nil
4,可变字典NSMutableDictionary
//可变字典
NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
[mDic setObject:@"张三" forKey:@"name"];//操作字典,绝大部分情况都是操作key
//字典的遍历 (一种思路是转换成数组,但是字典本来就可以遍历)
for (NSString *key in mDic) {
NSString *value = [mDic objectForKey:key];
//如果要删除,保存要删除的KEY,循环结束之后一次性删除
}
}
5,集合
作业:
#pragma mark-1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
//方法一
NSString *string = @"123-456-789-000";
NSString *string1 = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"newStr = %@",string1);
//方法二
NSArray *newArray1 = [string componentsSeparatedByString:@"-"];//为什么nsmutable要警报?
NSLog(@"%@",newArray1);
//创建一个字符串,初始包含size的字符
NSMutableString *newStr =[NSMutableString stringWithCapacity:0];
for (NSString *s in newArray1) {
//nsstring 在接受者的末尾附加nsstring
[newStr appendString:s];
}
NSLog(@"newStr = %@",newStr);
#pragma mark-2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())
NSMutableArray *numberRandom =[NSMutableArray arrayWithCapacity:100];
for (int i = 0; i < 100; i++) {
NSString *string =[NSString stringWithFormat:@"%d",arc4random() % 51 + 50];
[numberRandom addObject:string];
}
NSLog(@"%@",numberRandom)
// Student.m
// 5.21.OC.字典.hw
//
// Created by rimi on 15/5/21.
// Copyright (c) 2015年 rectinajh. All rights reserved.
//
#import "Student.h"
@implementation Student
- (instancetype)initWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
ID:(NSString *)Id
{
self = [super init];
if (self) {
_name = name;
_age = age;
_gender = gender;
_ID = Id;
}
return self;
}
+ (instancetype)StudentWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
ID:(NSString *)Id
{
Student *student = [[Student alloc] initWithName:name age:age gender:gender ID:Id];
return student;
}
- (NSInteger)differenceBetweenAges:(NSArray *)array
{
NSMutableArray *array1 = [NSMutableArray arrayWithArray:array];
NSDictionary *dictionary1 = array[0];
NSLog(@"%@",dictionary1);
Student *student1 = [[dictionary1 allValues] firstObject];
NSLog(@"value = %@",student1);
NSDictionary *dictionary2 = array[2];
Student *student2 = [[dictionary2 allValues] firstObject];
NSLog(@"value = %@",student2);
//取学生年龄
//得到差值
return labs((NSInteger)student1.age - (NSInteger)student2.age);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@,age:%ld,gender%@,id:%@",_name,_age,_gender,_ID];
}
@end
// Student.h
// 5.21.OC.字典.hw
//
// Created by rimi on 15/5/21.
// Copyright (c) 2015年 rectinajh. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *_gender;
NSString *_ID;
}
@property (strong,nonatomic)NSString *name;
@property (assign,nonatomic)NSInteger age;
- (instancetype)initWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
ID:(NSString *)Id;
+ (instancetype)StudentWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
ID:(NSString *)Id;
- (NSInteger)differenceBetweenAges:(NSArray *)array;
@end
// 5.21.OC.字典.hw
//
// Created by rimi on 15/5/21.
// Copyright (c) 2015年 rectinajh. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#pragma mark-3、创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组
#pragma mark-4、选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值
Student * student1 = [Student StudentWithName:@"j"
age:24
gender:@"men"
ID:@"123"];
Student * student2 = [Student StudentWithName:@"h"
age:21
gender:@"women"
ID:@"121"];
Student * student3 = [Student StudentWithName:@"a"
age:22
gender:@"men"
ID:@"122"];
NSDictionary *dictionary1 = @{student1.name : student1};
NSDictionary *dictionary2 = @{student2.name : student2};
NSDictionary *dictionary3 = @{student3.name : student3};
NSArray * array = @[dictionary1,dictionary2,dictionary3];
NSLog(@"%@",array);
Student *student = [[Student alloc] init];
NSInteger result = [student differenceBetweenAges:array];
NSLog(@"%ld",result);
// 5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组
NSSet *set =[NSSet setWithArray:numberRandom];
NSLog(@"set = %@",set);
NSLog(@"Hello, World!");
}