OC第四讲 数组、字典、集
1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())
3、 创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组
4、 选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值
5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组
王政-成都睿峰科技有限公 15:25:29
研究性知识点:
1、NSSet KVC
*/
// main.m
// Homework4
//
// Created by rimi on 15/5/21.
// Copyright (c) 2015年 rimi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
#pragma mark --1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
NSString *string = @"123-456-789-000";
//替换
NSString *newString = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"newString = %@",newString);
//可变字符串删除
//遍历可变字符串,进行删除
#pragma mark --2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())
//2
// NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:100];
// for (int i = 0; i < 100; i ++) {
// NSString * string = [NSString stringWithFormat:@"%d",arc4random() % 51 + 50];
// [mutableArray addObject:string];
// }
//// NSLog(@"compare = %@",[mutableArray sortedArrayUsingSelector:@selector(compare:)]);
// NSLog(@"mutableString = %@",mutableArray);
// NSLog(@"fjjj= %ld",mutableArray.count);
//
// NSSet *set = [NSSet setWithArray:mutableArray];
// NSLog(@"set = %@",set);
#pragma mark - 3、 创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组
// 4、 选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值
// 5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组
Student * student1 = [Student studentWithName:@"tom1"
age:20
gender:@"男"
code:@"110101199403021210"];
Student * student2 = [[Student alloc] initWithName:@"tom2"
age:14
gender:@"女"
code:@"110101200010100303"];
Student * student3 = [Student studentWithName:@"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 = [[Student alloc] init];
int result = [student differenceBetweenAges:array];
NSLog(@"result = %d", result);
}
return 0;
}
Student.h文件
#import <Foundation/Foundation.h>
@interface Student : 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"
@implementation Student
- (instancetype)initWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
code:(NSString *)code
{
// NSLog(@"%s", __FUNCTION__);
self = [super init];
if (self) {
_name = name;
_age =age;
_gender = gender;
_code = code;
}
return self;
}
+ (instancetype)studentWithName:(NSString *)name
age:(NSInteger)age
gender:(NSString *)gender
code:(NSString *)code
{
// NSLog(@"%s", __FUNCTION__);
Student * student = [[Student alloc] initWithName:name
age:age
gender:gender
code:code];
return student;
}
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (NSInteger)age
{
return _age;
}
- (int)differenceBetweenAges:(NSArray *)array
{
NSMutableArray * array1 = [NSMutableArray arrayWithArray:array];
NSDictionary *dictionary1 = array1[0];
NSLog(@"arr = %@",array1[0]);
Student * student1 = [[dictionary1 allValues] firstObject];
NSLog(@"value = %@",[dictionary1 allValues]);
NSDictionary *dictionary2 = array1[2];
Student * student2 = [[dictionary2 allValues] firstObject];
NSLog(@"student1 name = %@,student2 name = %@",student1.name,student2.name);
//取到学生年龄
//得到差值
return abs((int)student1.age - (int)student2.age);
}
- (NSComparisonResult)compareAge:(Student *)student
{
if (_age < student.age) {
return NSOrderedAscending;
}
if (_age > student.age) {
return NSOrderedDescending;
}
return NSOrderedSame;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@,age:%ld,gender:%@,code:%@", _name, _age, _gender, _code];
}
@end
课堂代码
// main.m
// 0521
//
// Created by rimi on 15/5/21.
// Copyright (c) 2015年 ruifeng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *string =@"abc";
NSString *string1 =@"cd";
NSInteger count = 3;
NSNumber *number = [NSNumber numberWithInteger:3];
NSInteger counts[3];
//oc中的集合
//1.只能放对象
//2.基本数据类型需要格式化才能放
//3.每个元素都不能为nil,所有元素依次放完,最后添加nil作为结束符
NSArray *array = [NSArray arrayWithObjects:@0,string,string1,nil];
NSLog(@"%@",array);
NSArray *array1 = [NSArray arrayWithObjects:string,string1,nil];
NSLog(@"%@",array);
//数组的3个初始化
NSArray *array0 = [[NSArray alloc]initWithObjects:string1,string,nil];
NSArray *array2 = @[string,string1];//不要加nil
//数组的操作
//取值 index
id string4 = [array objectAtIndex:0];
[string4 appendFormat:@"ab"];
NSString *string5 = [array objectAtIndex:1];//取到的值必须与存放
[array firstObject];
[array lastObject];
//截取数组
// NSArray *newArray = [array subarrayWithRange:(0,2)];
//拼接数组
NSArray *newArray19 = [array arrayByAddingObject:@"abc"];
//@"1",@"2",@"3"
//@"4",@"5"
//
NSArray *array7 = @[@"1",@"2",@"3"];
// array7
NSArray *array8 = [NSArray arrayWithObjects:@"4",@"5",nil];
NSArray *array9 = [array7 arrayByAddingObjectsFromArray:array8];
NSLog(@"%@",array9);
//数组与字符串的转化
NSString *stringAll= [array9 componentsJoinedByString:@"-"];
NSArray *array10 = [stringAll componentsSeparatedByString:@"-"];
NSLog(@"%@",stringAll);
//数组排序
NSArray *numbers =@[@"1",@"01",@"11",@"12",@"4"];
SEL sel = @selector(compare:);
NSArray *sortedArray = [numbers sortedArrayUsingSelector:sel];
NSLog(@"%@",sortedArray);
NSString *text = @"good morning ios program";
// NSString *chars = @"asfesfiuahgfiasghisag";
// NSArray *charects = [chars componentsSeparatedByCharactersInSet:];
// NSLog(@"%@",sortedArray)
//将它按照单词首字母排序,并生成新的字符串
NSArray *words = [text componentsSeparatedByString:@" "];//将语句用空格分割,成单词的数组
// NSLog(@"@",words);
NSArray *sortedWords = [words sortedArrayUsingSelector:@selector(compare:)];
NSString *newText = [sortedWords componentsJoinedByString:@" "];//用空格
NSLog(@"%@",newText);
//============================================================================
//字典 索引
//字典:用键值对(key-value)的形式存放对象数据的集合
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"张三",@"name",nil];
NSDictionary *dic1 = [[NSDictionary alloc]dictionaryWithObjectsAndKeys:@"张三",@"name",nil];
NSDictionary *dic2 = @{@"name":@"张三",
@"age":@"20"};//key-value,key在前,每个键值对为一个元素
NSLog(@"%@",dic2);
NSDictionary *dic3 = [[NSDictionary dictionaryWithObjects:@"123",@"456",@"789"] forKeys:@[@"1"@"4"@"7"]];
NSLog(@"%@",dic3);
//假设一个人有姓名,年龄,
Person *per = [[[Person alloc]init];
per.name = @"Tom";
per.age = 20;
per.birthday =@"19880920";
NSDictionary *personInfo = [NSDictionary dictionaryWithObjectsAndKeys:per.name,@"name",
per.age,@"age",
per.birthday,@"birthday",nil];
NSLog("@"%@",personInfo);
//建立一个新的Person,将字典中的数据取出来放到它的属性里
Person *newPerson = [[Person alloc]init];
newPerson.name = [personInfo objectForKey:@"name"];
newPerson.age = [[personInfo objectForKey:@"age"]integerValue];
newPerson.birthday = [personInfo objectForKey:@"birthday"];
NSDictionary *personInfo = [NSDictionary ]
//研究性知识点:KVC
//字典中不可能出现相同的key
//字典也不能放nil
//mutable 数组和字典的遍历运算
NSMutableArray *mArray = [NSMutableArray array];
[mArray addObject:@"abc"];
[mArray addObject:@"def"];//数组添加元素,直接加在最后
NSLog(@"%@",mArray);
[mArray removeObject:@"abc"];//移除所有等于它的元素
[mArray removeObjectAtIndex:0];//只移除那个位置上的元素
NSLog(@"%@",mArry);
//可变数组的其他方法
[mArray insterObject:@"ghi" atIndex:0];//插入到指定下标的位置
//排序
[mArray sortUsingSelector:@selector(compare:)];
//将20个1~20的随机数字字符串放到数组,并排序
NSMutableArray *[numberArray array];
for (NSInteger i=0;i<20;i++) {
//制造20个随机数数字符串
NSInteger number = arc4random()%20+1;
NSString *numberString = [NSString stringWithFormat:@"%ld",number];
[numberArray addObject:numberString];
}
[numberArray sortUsingSelector:@selector(compare:)];
NSLog(@"%@",numberArray);
//将上述数组中,大于10的数去掉
//不能在遍历数组的同时删除元素
//建立标记对象的数组,储存要删除的元素,
for (NSInteger i=0;i<20;i++) {
NSString *string = [numberArray objectAtIndex:i];
}
for (NSString *aNumber in numberArray) {//列举法
if (aNumber.integerValue >10) {
[deleteArray addObject:aNumber];
}
}
[numberArray removeObjectsInArray:deleteArray];
NSLog(@"%@",[numberArray];
//NSMutableDictionary
NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
[mDic setObject:@"张三" forKey:@"name"];
[mDic removeObjectForKey:@"name"];//操作字典,绝大部分情况都是操作key
//字典的遍历(重点)
for (NSString *key in mDic) {
// NSLog(@"%@",sring);
NSString *value = [mDic objectForKey:key];
//如果要删除,保存要删除的key,循环结束之后一次性删
}
// id obj [2] = {string,string1};
// NSArray *array3 = [NSArray arrayWithObjects:objs count:2];
}
return 0;
}