/* 字符串类 常用方法*/
// 1. 创建字符串 (初始化方法)
NSString *str2 = [[[NSString alloc]initWithString:str]autorelease];
NSLog(@"%@", str2);
NSString *str3 = [[[NSString alloc]initWithFormat:@"%@!%@!", str2, str2]autorelease];
NSLog(@"%@", str3);
// 2. 创建字符串 (便利构造器)
NSString *m = [NSString stringWithString:str2];
NSString *n = [NSString stringWithFormat:@"%@!%@!d%@kjf", str2, str2, str2];
NSLog(@"%@", m);
NSLog(@"%@", n);
// 3. 获取字符串长度
NSLog(@"%lu", [str length]);
// 4. 判断字符串的是否以指定字符串开始或结束。
// 应用案例:判断人名是否有姓
NSString *name1 = @"谢霆锋";
NSString *name2 = @"谢婷婷";
if ([name1 hasPrefix:@"谢"]) {
NSLog(@"%@谢", name1);
}
if ([name1 hasPrefix:@"a"]) {
NSLog(@"%@姓谢", name2);
}
BOOL str5 = [str hasPrefix:@"气"];
NSLog(@"%d", str5);
BOOL str6 = [str hasSuffix:@"错"];
NSLog(@"%d", str6);
// 5. 搜索字符串范围
NSString *guo = @"今天老郭被黑了";
NSRange range = [guo rangeOfString:@"老郭"];
NSLog(@"%lu %lu", range.location, range.length);
NSLog(@"%@", NSStringFromRange(range));
// 6. 字符串截取
NSString *nameKai = @"罗纳尔多";
NSString *nameDa = @"贝克汉姆";
// 练习:分别截取名字中的姓和名
NSLog(@"姓:%@", [nameKai substringToIndex:4]);
NSLog(@"名:%@", [nameKai substringFromIndex:4]);
NSLog(@"中间:%@",
[nameKai substringWithRange:NSMakeRange(2, 4)]);
NSLog(@"姓:%@", [nameDa substringToIndex:2]);
NSLog(@"名:%@", [nameDa substringFromIndex:2]);
NSLog(@"中间:%@",
[nameDa substringWithRange:NSMakeRange(1, 3)]);
// 7.字符串拼接
NSString *nameKaiDa = [[NSString alloc]
initWithFormat:@"%@%@", nameKai, nameDa];
NSLog(@"%@", nameKaiDa);
NSString *nameKaiDa1 = [nameKai stringByAppendingString:nameDa];
NSLog(@"%@", nameKaiDa1);
// 8. 字符串 -> 今天作业
// 9. 字符串比较
NSString *zuoShuHong = @"Zuo Shu Hong";
NSString *shiBoLiang = @"Shi Bo Liang";
NSLog(@"%ld", [zuoShuHong compare:shiBoLiang]);
if (1 == [zuoShuHong compare:shiBoLiang]) {
NSLog(@"Zuo Shu Hong > Shi Bo Liang");
} else if (0 == [zuoShuHong compare:shiBoLiang]) {
NSLog(@"Zuo Shu Hong == Shi Bo Liang");
} else {
NSLog(@"Zuo Shu Hong < Shi Bo Liang");
}
// 10.字符串与基本数据类型的转换
// 数据类型->字符串
// 例如:
int b = 213;
NSString *_213 = [NSString stringWithFormat:@"%d", b];
NSLog(@"%@", _213);
// 字符串 -> 基本数据类型
// 例如:@“213” -> 213
NSLog(@"%d", [_213 intValue]);
/* 可变字符串 常用方法*/
// 1. 创建字符串
NSMutableString *mutableString1 = [NSMutableString
stringWithString:zuoShuHong];
NSLog(@"%@", mutableString1);
// 2.字符串拼接
[mutableString1 appendString:@"是一个好女孩"];
NSLog(@"%@", mutableString1);
// 3.字符串插入
[mutableString1 insertString:@"同桌" atIndex:3];
NSLog(@"%@", mutableString1);
// 4.字符串替换
[mutableString1 replaceOccurrencesOfString:@"女"
withString:@"男" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString1 length])];
NSLog(@"%@", m
/* 数组类 */
/* 不可变数组 NSArray 常用方法*/
// 1.创建数组
// 创建空数组
NSArray *array1 = [[[NSArray alloc] init] autorelease];
NSArray *array2 = [NSArray array];
// 创建和某数组一样的数组
NSArray *array3 = [NSArray arrayWithArray:array1];
NSArray *array4 = [[NSArray alloc] initWithArray:array2];
// 创建一个对象的数组
NSArray *array5 = [NSArray arrayWithObject:@"1"];
// 创建多个对象的数组
NSArray *array6 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
NSLog(@"%@", array5);
NSLog(@"%@", array6);
// 2.获取数组元素
NSLog(@"%@", array6[2]);
NSLog(@"%@", [array6 objectAtIndex:2]);
NSLog(@"%@", [array6 firstObject]);
NSLog(@"%@", [array6 lastObject]);
// 3.获取对象在数组中的索引值
NSLog(@"%lu", [array6 indexOfObject:@"2"]);
/* 可变数组 */
NSMutableArray *classmate = [NSMutableArray arrayWithObjects:@"比卡丘",@"可达鸭",@"蜡笔小新",@"超级玛丽",@"樱木花道",@"樱桃小丸子",@"灰太狼",nil];
// 插入
[classmate insertObject:@"顾佳炜" atIndex:3];
// 删除
[classmate removeObject:@"比卡丘"];
// 添加
[classmate addObject:@"比卡丘"];
// 交换
[classmate exchangeObjectAtIndex:0 withObjectAtIndex:[classmate count] - 1];
// 替换
[classmate replaceObjectAtIndex:3 withObject:@"后跟"];
// 排序
[classmate sortUsingSelector:@selector(compare:)];
// 输出
for (int i = 0; i < [classmate count]; i++) {
NSLog(@"%d: %@", i + 1, [classmate objectAtIndex:i]);
}
// 数组的元素时自定义类的对象
Person *p1 = [[[Person alloc]initWithName:@"大白兔"] autorelease];
Person *p2 = [[[Person alloc]initWithName:@"小红帽"] autorelease];
Person *p3 = [[[Person alloc]initWithName:@"狼外婆"] autorelease];
NSArray *array = [NSArray arrayWithObjects:p1, p2, p3, nil];
for (int i = 0; i < [array count]; i++) {
[[array objectAtIndex:i] sayHi];
}
/* 数值类 */
// 1.基本数据类型->数值对象
// char
NSNumber *a = [[NSNumber alloc] initWithChar:'A'];
// short
NSNumber *b = [[NSNumber alloc] initWithShort:'a'];
// int
NSNumber *c = [[NSNumber alloc] initWithInt:5];
// float
NSNumber *d = [[NSNumber alloc] initWithFloat:2.13];
// BOOL
NSNumber *e = [[NSNumber alloc] initWithBool:1];
NSMutableArray *arrNumber = [NSMutableArray arrayWithObjects:a, b, c, d, e, nil];
NSLog(@"%@", arrNumber);
// 2.数值对象, 取出相应的值
char x = [a charValue];
NSLog(@"%c", x);
/* 不可变字典 NSDictionary 常用方法 */
// 1.创建字典对象
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Jay", @"name", @"value:male",@"sex",@"value:35",@"age", nil];
NSLog(@"%@", dictionary);
// 2.已知key求对应的value
NSString *name = [dictionary objectForKey:@"name"];
NSString *sex = [dictionary objectForKey:@"sex"];
NSString *age = [dictionary objectForKey:@"age"];
NSLog(@"\nname = %@;\n sex = %@\n age = %@\n", name, sex, age);
// 3.求所有的key
NSLog(@"%@", [dictionary allKeys]);
// 4.求所有的value
NSLog(@"%@", [dictionary allValues]);
/* 可变字典 NSMutableDictionary 常用方法*/
// 1.创建字典对象
NSMutableDictionary *dictionary2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Jolin", @"name", @"male",@"sex",@"32",@"age", nil];
NSLog(@"%@", dictionary2);
// 2.添加键值对
[dictionary2 setValue:@"sing" forKey:@"hobby"];
NSLog(@"%@", dictionary2);
// 3.修改已有键的值
[dictionary2 setValue:@"嘿嘿" forKey:@"name"];
// 4.删除键值对
[dictionary2 removeObjectForKey:@"name"];
NSLog(@"%@", dictionary2);
// 5.通过for循环遍历所有键值对
NSLog(@"%@", [dictionary2 description]);
/* 集合类 NSSet 常用方法 */
// 1.创建
NSSet *class1 = [NSSet setWithArray:
classmate];
// 2.获取元素个数
NSLog(@"%lu", [class1 count]);
// 3.判断集合中是否包含某个对象
// @“小李飞刀”
BOOL abc = [class1 containsObject:@"小李飞刀"];
NSLog(@"%d", abc);
/* 可变集合 NSMutableSet */
// 1.创建集合对象
NSMutableSet *class = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];
NSLog(@"%@", class);
// 2.添加元素
[class addObject:@"ddd"];
NSLog(@"%@", class);
// 3.删除元素
[class removeObject:@"bbb"];
NSLog(@"%@", class);