- 创建
// 第一种创建方式
NSString *s = @"a";
// 第二种方式
NSString *s1 = [NSString new];
s1 = @"b";
// 第三种
for (int i = 0; i<10; i++) {
NSString *imgName = [NSString stringWithFormat:@"xxxxx%02d.jpg",i];
// xxxxx00.jpg、xxxxx01.jpg....xxxxx09.jpg
}
// 第四种
NSString *s2 = [[NSString alloc]initWithString:s1];
- 长度
NSString *str = @"abcd";
NSUInteger len = [str length];
NSLog(@"str length:%ld",len);
- 字符
unichar ch = [str characterAtIndex:0]
- 相等
[str1 isEqualToString:str2]
- 查找某个字符串在另外一个字符串中首次出现的位置
NSString *str1= @"slkioslkgilkdiosflgkhldsiosfndslfdsn";
NSString *str2= @"ios";
NSRange range = [str1 rangeOfString:str2];
NSLog(@"%lu,%lu",range.location,range.length);
- 类型转换
NSString *str = @"1";
int a = 19;
//把字符串类型转换成其他类型
int b = [str intValue]; //intValue,floatValue,doubleValue
int sum = a+b;
NSMutableArray和NSArray是OC中使用的数组类,前者是可变数组,后者是不可变数组。
- NSArray
NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSLog(@"arr = %@", arr);
NSArray *arr1 = [NSArray arrayWithArray:arr];
NSLog(@"arr1 = %ld", [arr1 count]);
NSArray *arr2 = @[@"a", @"b", @"c"];
NSLog(@"arr2 = %@", arr2);
NSString *str0 = [arr2 objectAtIndex:0];
NSLog(@"str0 = %@", str0);
NSString *str = [arr2 lastObject];
NSLog(@"last str = %@", str);
NSString *str1 = [arr firstObject];
NSLog(@"first str = %@", str1);
if([arr containsObject:@"a"]) {
NSLog(@"含有此对象");
}
//数组的遍历
for (NSInteger i = 0; i < [arr count]; i++) {
NSLog(@"arr[%ld] = %@",i,arr[i]);
}
//快速枚举法(快速遍历)
for (id arr2Q in arr2) {
NSLog(@"arr2Q = %@",arr2Q);
}
}
return 0;
}
执行结果:
2018-10-09 11:45:04.892806+0800 Hello[2621:36790] arr = (
a,
b,
c
)
2018-10-09 11:45:04.892984+0800 Hello[2621:36790] arr1 = 3
2018-10-09 11:45:04.893036+0800 Hello[2621:36790] arr2 = (
a,
b,
c
)
2018-10-09 11:45:04.893070+0800 Hello[2621:36790] str0 = a
2018-10-09 11:45:04.893103+0800 Hello[2621:36790] last str = c
2018-10-09 11:45:04.893115+0800 Hello[2621:36790] first str = a
2018-10-09 11:45:04.893145+0800 Hello[2621:36790] 含有此对象
2018-10-09 11:45:04.893156+0800 Hello[2621:36790] arr[0] = a
2018-10-09 11:45:04.893168+0800 Hello[2621:36790] arr[1] = b
2018-10-09 11:45:04.893179+0800 Hello[2621:36790] arr[2] = c
2018-10-09 11:45:04.893251+0800 Hello[2621:36790] arr2Q = a
2018-10-09 11:45:04.893284+0800 Hello[2621:36790] arr2Q = b
2018-10-09 11:45:04.893296+0800 Hello[2621:36790] arr2Q = c
- NSMutableArray
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr addObject:@"a"];
NSLog(@"arr = %@",arr);
[arr addObjectsFromArray:@[@"b",@"D"]];
NSLog(@"arr = %@",arr);
[arr insertObject:@"c" atIndex:2];
NSLog(@"arr = %@",arr);
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:arr];
[arr replaceObjectAtIndex:3 withObject:@"d"];
NSLog(@"arr = %@",arr);
NSString *result = [arr objectAtIndex:0];
NSLog(@"arr[0] = %@",result);
[arr removeObject:@"d"];
NSLog(@"arr = %@",arr);
[arr removeObjectAtIndex:2];
NSLog(@"arr = %@",arr);
[arr removeLastObject];
NSLog(@"arr = %@",arr);
[arr removeAllObjects];
NSLog(@"arr = %@",arr);
[arr1 removeObjectsInArray:@[@"c",@"d"]];
NSLog(@"arr1 = %@",arr1);
[arr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"arr1 = %@",arr1);
}
return 0;
}
执行结果:
2018-10-09 14:17:24.754916+0800 Hello[6931:141861] arr = (
a
)
2018-10-09 14:17:24.755226+0800 Hello[6931:141861] arr = (
a,
b,
D
)
2018-10-09 14:17:24.755261+0800 Hello[6931:141861] arr = (
a,
b,
c,
D
)
2018-10-09 14:17:24.755291+0800 Hello[6931:141861] arr = (
a,
b,
c,
d
)
2018-10-09 14:17:24.755322+0800 Hello[6931:141861] arr[0] = a
2018-10-09 14:17:24.755433+0800 Hello[6931:141861] arr = (
a,
b,
c
)
2018-10-09 14:17:24.755461+0800 Hello[6931:141861] arr = (
a,
b
)
2018-10-09 14:17:24.755479+0800 Hello[6931:141861] arr = (
a
)
2018-10-09 14:17:24.755495+0800 Hello[6931:141861] arr = (
)
2018-10-09 14:17:24.755553+0800 Hello[6931:141861] arr1 = (
a,
b,
D
)
2018-10-09 14:17:24.755570+0800 Hello[6931:141861] arr1 = (
b,
a,
D
)
Java中的map
NSDictionary(不可变数据字典) NSMutableDictionary(可变数据字典)
- 由键值对(key-value)组成的数据集合
- 任何对象都可以作键,但一般都是字符串
- 字典内元素无序排列,键相同的元素不能重复
- 可以通过键来快速枚举
//1、用一个或多个键值对初始化一个字典对象: 以值,建,值,键,...,nil的顺序
NSDictionary *dict01=[[NSDictionary alloc] initWithObjectsAndKeys:@"key0",@"value0",@"key1",@"value1",@"key2",@"value2", nil];
NSLog(@"dict01:%@",dict01);
NSDictionary *dict1=[NSDictionary dictionaryWithObjectsAndKeys:@123,@"key0",@456,@"key1",@678,@"key2", nil];
NSLog(@"dict1:%@",dict1);
//2、用两个数组初始化一个字典,一个key数组,一个value数组,两个数组大小必须相等,否则Crash
NSArray *keyArray=@[@"name0",@"name1",@"name2"];
NSArray *valueArray=@[@"hong",@"wang",@"zhao"];
NSDictionary *dict02=[[NSDictionary alloc] initWithObjects:keyArray forKeys:valueArray];
NSDictionary *dict2=[NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSLog(@"dict02:%@",dict02);
NSLog(@"dict2:%@",dict2);
//3、用一个数据字典初始化另一个数据字典
NSDictionary *dict03=[[NSDictionary alloc] initWithDictionary:dict1];
NSDictionary *dict3=[NSDictionary dictionaryWithDictionary:dict1];
NSLog(@"dict03:%@",dict03);
NSLog(@"dict3:%@",dict3);
//1、保存字典对象内容到文件
[dict3 writeToFile:@"/Users/apple/Desktop/tesk.txt" atomically:YES];
//2、从之前保存的文件读取数据到字典对象
NSDictionary *dict4=[NSDictionary dictionaryWithContentsOfFile:@"/Users/apple/Desktop/tesk.txt"];
执行结果:
2018-10-09 14:42:13.099190+0800 Hello[7982:155863] dict01:{
value0 = key0;
value1 = key1;
value2 = key2;
}
2018-10-09 14:42:13.099487+0800 Hello[7982:155863] dict1:{
key0 = 123;
key1 = 456;
key2 = 678;
}
2018-10-09 14:42:13.099544+0800 Hello[7982:155863] dict02:{
hong = name0;
wang = name1;
zhao = name2;
}
2018-10-09 14:42:13.099564+0800 Hello[7982:155863] dict2:{
name0 = hong;
name1 = wang;
name2 = zhao;
}
2018-10-09 14:42:13.099601+0800 Hello[7982:155863] dict03:{
key0 = 123;
key1 = 456;
key2 = 678;
}
2018-10-09 14:42:13.099624+0800 Hello[7982:155863] dict3:{
key0 = 123;
key1 = 456;
key2 = 678;
}
- 元素获取及遍历
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary *dict1=[NSDictionary dictionaryWithObjectsAndKeys:@123,@"key0",@456,@"key1",@678,@"key2", nil];
//1、分别获取字典的所有键、值
NSArray *allkeys=[dict1 allKeys];
NSLog(@"allkeys:%@",allkeys);
NSArray *allvalues=[dict1 allValues];
NSLog(@"allvalues:%@",allvalues);
//2、通过指定的key取对应的value对象,没有这个key的话.返回nil
NSNumber *num1 = [dict1 objectForKey:@"key1"];
NSNumber *num2 = [dict1 objectForKey:@"key5"];
NSLog(@"%@",num1);
NSLog(@"%@",num2);
//3、获取字典中键值对的个数
NSUInteger paircount=[dict1 count];
NSLog(@"%lu",paircount);
for (id key in dict1) {
id value=[dict1 objectForKey:key];
NSLog(@"%@=%@",key,value);
}
}
return 0;
}
执行结果:
2018-10-09 14:47:58.817023+0800 Hello[8191:159050] allkeys:(
key1,
key0,
key2
)
2018-10-09 14:47:58.817297+0800 Hello[8191:159050] allvalues:(
456,
123,
678
)
2018-10-09 14:47:58.817331+0800 Hello[8191:159050] 456
2018-10-09 14:47:58.817343+0800 Hello[8191:159050] (null)
2018-10-09 14:47:58.817353+0800 Hello[8191:159050] 3
2018-10-09 14:47:58.817402+0800 Hello[8191:159050] key1=456
2018-10-09 14:47:58.817427+0800 Hello[8191:159050] key0=123
2018-10-09 14:47:58.817439+0800 Hello[8191:159050] key2=678
- NSMutableDictionary
int main(int argc, const char * argv[]) {
@autoreleasepool {
//可变数据字典初始化
NSMutableDictionary *mDict1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaa",@"name0",@"bbb",@"name1",@"ccc",@"name2", nil];
NSLog(@"%@",mDict1);
//1、加入元素到NSMutableDictionary中 (注意:当key值中存在要添加的键值对中的key值时,则添加,否则覆盖)
[mDict1 setObject:@"ddd" forKey:@"name3"];
[mDict1 setObject:@"eee" forKey:@"name1"];
NSLog(@"add: %@",mDict1);
//2、查看 中所有元素个数:
NSLog(@"%lu",mDict1.count);
NSNumber *num3 =[mDict1 objectForKey:@"name1"];
NSLog(@"%@",num3);
NSNumber *num4 = [mDict1 objectForKey:@"name10"];
NSLog(@"%@",num4);
//3、根据key值删除
[mDict1 removeObjectForKey:@"name1"];
NSLog(@"---%@",mDict1);
//4、删除所有
[mDict1 removeAllObjects];
NSLog(@"%@",mDict1);
}
return 0;
}
运行结果:
2018-10-09 15:16:28.171512+0800 Hello[8960:169380] {
name0 = aaa;
name1 = bbb;
name2 = ccc;
}
2018-10-09 15:16:28.171748+0800 Hello[8960:169380] add: {
name0 = aaa;
name1 = eee;
name2 = ccc;
name3 = ddd;
}
2018-10-09 15:16:28.171768+0800 Hello[8960:169380] 4
2018-10-09 15:16:28.171794+0800 Hello[8960:169380] eee
2018-10-09 15:16:28.171805+0800 Hello[8960:169380] (null)
2018-10-09 15:16:28.171825+0800 Hello[8960:169380] ---{
name0 = aaa;
name2 = ccc;
name3 = ddd;
}
2018-10-09 15:16:28.171841+0800 Hello[8960:169380] {
}
用来操作磁盘上的文件、文件夹,对他们进行创建、删除、赋值、拷贝、移动
这个类是以单例模式创建的,想要得到该类对象,需要通过下面的方式:
NSFileManager *fileManager = [NSFileManager defaultManager];
常用方法:
- (BOOL)fileExistsAtPath:(NSString *)path;
- (BOOL)isREadableFileAtPath:(NSString *)path;
NSDate *date=[NSDate date];
NSLog(@"%@",date);
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
dateFormatter.dateFormat=@"yyyy年MM月dd日 HH:mm:ss";
NSLog(@"%@",[dateFormatter stringFromDate:date]);
- 邮箱 :charon.chui@gmail.com
- Good Luck!