一。不可变字符串
字符串赋值:
1.NSString *name = @"TOM";
2.NSString *name = [[NSString alloc] initWithFormat:@"TOM"];
3.NSString *name = [NSString stringWithFormat:@"TOM"];
三种方式赋值
NSInteger len = [name length];字符串的长度
NSString *newName = [name lowercaseString];//将字母转化为小写
NSString *newName = [name uppercaseString];将字母转化为大写
NSString *newName = [name capitalizedString];将首字母转化为大写
字符串截取
NSString *subName = [name substringWithRange:NSMakeRange(1,3)];//按范围截取子串
NSString *subName = [name substringToIndex:5];//从第一个字符截到指定的字符
NSString *subName = [name substringFrom:5];//从指定的字符截到尾部
字符串类型转换
NSString *val =@"3123";
NSString *val =@"3123.3";
NSInteger num = [val integerValue];//转化为整型
CGFloat num1 = [val floatValue];//转化为浮点型
字符串前后辍问题
NSString *image = @"aaaa.png";
BOOL isnot = [image hasPrefix:@"aa"];是否以aa为前辍
BOOL isnot1 = [image hasSuffix:@"png"];是否以png为后辍
字符串替换问题
NSString *word = @"I love my mother";
NSString *word1 =[ word stringByReplacingOccurrencesOfString:@"love" withString:@"like"];//把love替换为like
NSString *word2=[word stringByReplacingCharactersRange:NSMakeRange(2,4) withString:@"like"];//按范围替换
//字符串的比较
NSString *strCom = @"abcde";
NSString *strCom2=@"fasfda";
NSComparisonResult res = [strCom compare:strCom2];
NSLog(@"%ld" , res);
获取子字符串在整个字符串中的范围
NSString *st1 = @"I love movie";
NSRange num = [st1 rangeOfString:@"I"];
NSLog(@"%ld,%ld" , num.length,num.location);
//字符串拼接
NSString *words = @"I love ";
NSString *newWords = [words stringByAppenddingString:@"movie"];
二.可变字符串的操作
NSMutableString *mulStr = [[NSMutableString alloc] initWithFormat:@"xiao ma"];
[mulStr insertString:@"love" atIndex:4];//在指的位置插入内容
拼接内容
[mulStr appendString:@"movie"];
//删除
[mulStr deleteCharactersInRange:NSMakeRange(8, 3)];
//修改
[mulStr replaceCharactersInRange:NSMakeRange(4, 4) withString:@" like "];
三.不可变数组
1.NSArray *arr = @[@"abc",@"bcd",@"aa"];
2.NSString *str[10] = {@"abc",@"dbc"};
NSArray *arr4 = [[NSArray alloc] initWithObjects:str count:2];
3.NSArray *arr1= [[NSArray alloc] initWithObjects:@"abc",@"fds",nil];
4.NSArray *arr2 = [NSArray arrayWithObject:@"ag",nil];
5.NSArray *arr3=[NSArray arrayWithArray:arr];
以上是给不可变数组进行初始化赋值
查询某个元素在数组的位置
NSInteger *num = [arr1 indexOfObject:@"abc"];
可变数组:
NSMutableArray *arr =[NSMutableArray Array];
[arr addObject:@"aa"];
[arr addObjectsFromArray:arr1];
//插入
[mulArr insertObject:@"xiao ma" atIndex:3];
NSLog(@"%@" , mulArr);
//删除
[mulArr removeObject:@"xiao ma"];
NSLog(@"%@" , mulArr);
//替换
[mulArr replaceObjectAtIndex:0 withObject:@"zhangsan"];
NSLog(@"%@" , mulArr);
//交换
[mulArr exchangeObjectAtIndex:0 withObjectAtIndex:3];
数组保存基本数据类型,要将数据类型转化为对象才可存储
NSString *name = @"TOM";
NSInteger age = 18;
CGFloat score = 13.3;
BOOL isOrNot = YES;
char gender = 'f';
//将非对象类型转为对象类型
//第一种方法
NSNumber *age1 = @(age);
//第二种方法
NSNumber *age11 = [[NSNumber alloc] initWithInteger:age];
//第三种方法
NSNumber *age111 = [NSNumber numberWithInteger:age];
NSNumber *score1 = @(score);
NSNumber *isOrNot1 = @(isOrNot);
NSNumber *gender1 = @(gender);
//赋值方式1
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:name,age1,score1,isOrNot1,gender1, nil];
NSRange range = NSMakeRange(1, 4);
NSValue *rangeObje = [NSValue valueWithRange:range];
NSLog(@"%@" , rangeObje);
NSRange crs = [rangeObje rangeValue];
NSLog(@"%ld" , crs.length)
NSLog(@"%@" , arr2);
//赋值方式2
NSMutableArray *arr = [NSMutableArray new];
[arr addObject:name];
[arr addObject:age1];
[arr addObject:score1];
[arr addObject:isOrNot1];
[arr addObject:gender1];
字典的存储
1.NSDictionary *disct2 = [[NSDictionary alloc] initWithObjects:@[@"ab",@"ac"] forKeys:@[@"1" ,@"2"]];
NSDictionary *disct = @{
@"name" : @"TOM",
@"gender" : @"f",
@"hobby":@"read",
@"age" : @(18)
};
NSDictionary *dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"ab",@"1",@"bc",@"2", nil];
可变字典初始化
NSMutableDictionary *dictionarys = [NSMutableDictionary dictionary];
NSMutableDictionary *dictionary1 = [NSMutableDictionary dictionaryWithObject:@"fd" forKey:@"a"];
[dictionarys setObject:person5 forKey:@"4"];//添加元素
[dictionary1 removeObjectForKey:@"3"];//删除元素
[dictionarys setDictionary:@{
@"a": @"mahy"
}];//修改与添加功能,内容前面为键,后面为值
不可修改集合
NSSet *setRs =[NSSet setWithObjects:@"234",@"dfas",@"fs",nil];
NSArray *arr = @[@12,@32,@54,@34];
NSSet *setRs1 = [NSSet setWithArr:arr];
可以修改集合
NSMutableSet *setRs1 = [NSMutableSet set];
[setRs1 addObject:@"3234"];
[setRs1 removeObject:@"3234"];
//获取重复对象元素的个数
NSArray *ages= @[@12,@13,@425,@32,@12];
NSCountedSet *agescount = [NSCountedSet setWithArray:ages];
NSLog(@"%@" ,agescount);
NSUInteger count = [agescount countForObject:@12];
NSLog(@"%lu" , count);
字符串操作
NSString *file = [NSString stringWithContentsOfFile:@"/Users/aa.txt" encoding:NSUTF8StringEncoding error:nil];
NSSArray *arr = [file componentsSeparatedByString:@"\n"];//进行对字符串分割,分割完放入数组
//取出所有key排序
NSArray *keyArr = dictionary.allKeys;
keyArr= [keyArr sortedArrayUsingSelector:@selector(compare:)];//对数组进行排序
NSLog(@"%@" , keyArr);
字符串赋值:
1.NSString *name = @"TOM";
2.NSString *name = [[NSString alloc] initWithFormat:@"TOM"];
3.NSString *name = [NSString stringWithFormat:@"TOM"];
三种方式赋值
NSInteger len = [name length];字符串的长度
NSString *newName = [name lowercaseString];//将字母转化为小写
NSString *newName = [name uppercaseString];将字母转化为大写
NSString *newName = [name capitalizedString];将首字母转化为大写
字符串截取
NSString *subName = [name substringWithRange:NSMakeRange(1,3)];//按范围截取子串
NSString *subName = [name substringToIndex:5];//从第一个字符截到指定的字符
NSString *subName = [name substringFrom:5];//从指定的字符截到尾部
字符串类型转换
NSString *val =@"3123";
NSString *val =@"3123.3";
NSInteger num = [val integerValue];//转化为整型
CGFloat num1 = [val floatValue];//转化为浮点型
字符串前后辍问题
NSString *image = @"aaaa.png";
BOOL isnot = [image hasPrefix:@"aa"];是否以aa为前辍
BOOL isnot1 = [image hasSuffix:@"png"];是否以png为后辍
字符串替换问题
NSString *word = @"I love my mother";
NSString *word1 =[ word stringByReplacingOccurrencesOfString:@"love" withString:@"like"];//把love替换为like
NSString *word2=[word stringByReplacingCharactersRange:NSMakeRange(2,4) withString:@"like"];//按范围替换
//字符串的比较
NSString *strCom = @"abcde";
NSString *strCom2=@"fasfda";
NSComparisonResult res = [strCom compare:strCom2];
NSLog(@"%ld" , res);
获取子字符串在整个字符串中的范围
NSString *st1 = @"I love movie";
NSRange num = [st1 rangeOfString:@"I"];
NSLog(@"%ld,%ld" , num.length,num.location);
//字符串拼接
NSString *words = @"I love ";
NSString *newWords = [words stringByAppenddingString:@"movie"];
二.可变字符串的操作
NSMutableString *mulStr = [[NSMutableString alloc] initWithFormat:@"xiao ma"];
[mulStr insertString:@"love" atIndex:4];//在指的位置插入内容
拼接内容
[mulStr appendString:@"movie"];
//删除
[mulStr deleteCharactersInRange:NSMakeRange(8, 3)];
//修改
[mulStr replaceCharactersInRange:NSMakeRange(4, 4) withString:@" like "];
三.不可变数组
1.NSArray *arr = @[@"abc",@"bcd",@"aa"];
2.NSString *str[10] = {@"abc",@"dbc"};
NSArray *arr4 = [[NSArray alloc] initWithObjects:str count:2];
3.NSArray *arr1= [[NSArray alloc] initWithObjects:@"abc",@"fds",nil];
4.NSArray *arr2 = [NSArray arrayWithObject:@"ag",nil];
5.NSArray *arr3=[NSArray arrayWithArray:arr];
以上是给不可变数组进行初始化赋值
查询某个元素在数组的位置
NSInteger *num = [arr1 indexOfObject:@"abc"];
可变数组:
NSMutableArray *arr =[NSMutableArray Array];
[arr addObject:@"aa"];
[arr addObjectsFromArray:arr1];
//插入
[mulArr insertObject:@"xiao ma" atIndex:3];
NSLog(@"%@" , mulArr);
//删除
[mulArr removeObject:@"xiao ma"];
NSLog(@"%@" , mulArr);
//替换
[mulArr replaceObjectAtIndex:0 withObject:@"zhangsan"];
NSLog(@"%@" , mulArr);
//交换
[mulArr exchangeObjectAtIndex:0 withObjectAtIndex:3];
数组保存基本数据类型,要将数据类型转化为对象才可存储
NSString *name = @"TOM";
NSInteger age = 18;
CGFloat score = 13.3;
BOOL isOrNot = YES;
char gender = 'f';
//将非对象类型转为对象类型
//第一种方法
NSNumber *age1 = @(age);
//第二种方法
NSNumber *age11 = [[NSNumber alloc] initWithInteger:age];
//第三种方法
NSNumber *age111 = [NSNumber numberWithInteger:age];
NSNumber *score1 = @(score);
NSNumber *isOrNot1 = @(isOrNot);
NSNumber *gender1 = @(gender);
//赋值方式1
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:name,age1,score1,isOrNot1,gender1, nil];
NSRange range = NSMakeRange(1, 4);
NSValue *rangeObje = [NSValue valueWithRange:range];
NSLog(@"%@" , rangeObje);
NSRange crs = [rangeObje rangeValue];
NSLog(@"%ld" , crs.length)
NSLog(@"%@" , arr2);
//赋值方式2
NSMutableArray *arr = [NSMutableArray new];
[arr addObject:name];
[arr addObject:age1];
[arr addObject:score1];
[arr addObject:isOrNot1];
[arr addObject:gender1];
字典的存储
1.NSDictionary *disct2 = [[NSDictionary alloc] initWithObjects:@[@"ab",@"ac"] forKeys:@[@"1" ,@"2"]];
NSDictionary *disct = @{
@"name" : @"TOM",
@"gender" : @"f",
@"hobby":@"read",
@"age" : @(18)
};
NSDictionary *dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"ab",@"1",@"bc",@"2", nil];
可变字典初始化
NSMutableDictionary *dictionarys = [NSMutableDictionary dictionary];
NSMutableDictionary *dictionary1 = [NSMutableDictionary dictionaryWithObject:@"fd" forKey:@"a"];
[dictionarys setObject:person5 forKey:@"4"];//添加元素
[dictionary1 removeObjectForKey:@"3"];//删除元素
[dictionarys setDictionary:@{
@"a": @"mahy"
}];//修改与添加功能,内容前面为键,后面为值
不可修改集合
NSSet *setRs =[NSSet setWithObjects:@"234",@"dfas",@"fs",nil];
NSArray *arr = @[@12,@32,@54,@34];
NSSet *setRs1 = [NSSet setWithArr:arr];
可以修改集合
NSMutableSet *setRs1 = [NSMutableSet set];
[setRs1 addObject:@"3234"];
[setRs1 removeObject:@"3234"];
//获取重复对象元素的个数
NSArray *ages= @[@12,@13,@425,@32,@12];
NSCountedSet *agescount = [NSCountedSet setWithArray:ages];
NSLog(@"%@" ,agescount);
NSUInteger count = [agescount countForObject:@12];
NSLog(@"%lu" , count);
字符串操作
NSString *file = [NSString stringWithContentsOfFile:@"/Users/aa.txt" encoding:NSUTF8StringEncoding error:nil];
NSSArray *arr = [file componentsSeparatedByString:@"\n"];//进行对字符串分割,分割完放入数组
//取出所有key排序
NSArray *keyArr = dictionary.allKeys;
keyArr= [keyArr sortedArrayUsingSelector:@selector(compare:)];//对数组进行排序
NSLog(@"%@" , keyArr);