注意一下, 下面代码中很多 nil nil 结尾的, 是csdn的bug, 实际上只有一个nil
- (void)constructNSString
{
NSLog(@"Next, I will construct kinds of string");
NSString* string00 = @"String construct by '='";
NSLog(@"\t string00=%@", string00);
NSString* string01 = [[NSString alloc] init];
NSLog(@"\t string01=%@", string01);
NSString* string02 = [[NSString alloc] initWithCString:"String construct by [initWithCString:encoding:]!" encoding:NSASCIIStringEncoding];
NSLog(@"\t string02=%@", string02);
NSString* string03 = [[NSString alloc] initWithUTF8String:"String construct by [initWithUTF8String:]!"];
NSLog(@"\t string03=%@", string03);
NSString* string04 = [NSString stringWithFormat:@"String construct by [stringWithFormat:]! param=%d", 0];
NSLog(@"\t string04=%@", string04);
NSString* string05 = [NSString stringWithCString:"String construct by [stringWithCString:encoding:]!" encoding:NSASCIIStringEncoding];
NSLog(@"\t string05=%@", string05);
NSString* string06 = [NSString stringWithUTF8String:"String construct by [stringWithUTF8String:]!"];
NSLog(@"\t string06=%@", string06);
unichar uniChar[] = {'H', 'E', 'L', 'L', 'O', 0};
NSString* string07 = [NSString stringWithCharacters:uniChar length:sizeof(uniChar)/sizeof(uniChar[0])];
NSLog(@"\t string07=%@", string07);
}
- (void)compareNSString
{
BOOL bRet;
NSComparisonResult cr;
NSString* stringA = @"String a!";
NSString* stringB = @"String A, too!";
NSLog(@"Next, I will compare '%@' with '%@'", stringA, stringB);
// 判断字符串是否相等
bRet = [stringA isEqualToString:stringB];
NSLog(@"\t [isEqualToString:] ==> %d", bRet);
// 比较字符串大小
cr = [stringA compare:stringB];
NSLog(@"\t [compare:] ==> %d", cr);
// 忽略大小写比较
cr = [stringA caseInsensitiveCompare:stringB];
NSLog(@"\t [caseInsensitiveCompare:] ==> %d", cr);
// NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
cr = [stringA compare:stringB options:NSCaseInsensitiveSearch|NSNumericSearch];
NSLog(@"\t [compare:options:] ==> %d", cr);
}
- (void)handleNSString
{
BOOL bRet;
NSString* stringC = @"~/0123456789.txt";
NSLog(@"Next, I will deal with '%@'", stringC);
// 在字符串中搜索另一个字符串的位置
NSRange range = [stringC rangeOfString:@"3456"];
NSLog(@"\t [rangeOfString:] ==> NSRange(%d, %d)", range.location, range.length);
// 检查字符串是否以另一个字符串开头
bRet = [stringC hasPrefix:@"~/01234"];
NSLog(@"\t [hasPrefix:] ==> %d", bRet);
// 检查字符串是否以另一个字符串结尾
bRet = [stringC hasSuffix:@"789.txt"];
NSLog(@"\t [hasSuffix:] ==> %d", bRet);
// 获取子字符串
NSLog(@"\t [substringToIndex:] ==> %@", [stringC substringToIndex:3]); // 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSLog(@"\t [substringFromIndex:] ==> %@", [stringC substringFromIndex:3]); // 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSLog(@"\t [substringWithRange:] ==> %@", [stringC substringWithRange:NSMakeRange(2, 6)]); // 按照所给出的位置,长度,任意地从字符串中截取子串
// 如果是字符串是文件路径时, 获取扩展名
NSLog(@"\t [pathExtension] ==> %@", [stringC pathExtension]);
// 构造各种文件目录
NSString* absolutePath = [stringC stringByExpandingTildeInPath];
NSString* relativePath = [absolutePath stringByAbbreviatingWithTildeInPath];
NSLog(@"\t [stringByExpandingTildeInPath] ==> %@", absolutePath);
NSLog(@"\t [stringByAbbreviatingWithTildeInPath] ==> %@", relativePath);
// 构造一个可编辑的字符串
NSMutableString* stringD = [NSMutableString stringWithString:@"This is a NSMutableString"];
NSLog(@"Next, I will deal with '%@'", stringD);
// 把字符串接在另一个字符串的末尾
[stringD appendString:@", xxxxxxx"];
NSLog(@"\t [appendString:] ==> %@", stringD);
// 按照指定格式在字符串末尾追加
[stringD appendFormat:@", aaaaaaa%d", 100];
NSLog(@"\t [appendFormat:] ==> %@", stringD);
// 在指定位置插入字符串
[stringD insertString:@"Hi!" atIndex:6];
NSLog(@"\t [insertString:atIndex:] ==> %@", stringD);
// 重新赋值
[stringD setString:@"Hello Word! I am a new string, please give me screem!"];
NSLog(@"\t [setString:] ==> %@", stringD);
// 一个指定的字符串, 替换字符串中指定位置长度的字符串
[stringD replaceCharactersInRange:NSMakeRange(4, 8) withString:@"1111"];
NSLog(@"\t [replaceCharactersInRange:withString:] ==> %@", stringD);
// 一个指定的字符串, 替换字符串中指定字符串, 只推荐在短字符串中使用
NSString* str = [stringD stringByReplacingOccurrencesOfString:@"string" withString:@"gnirts"];
NSLog(@"\t [stringByReplacingOccurrencesOfString:withString:] ==> %@", str);
}
- (void)constructNSArray
{
// 数组可以一次性添加完全, 以nil作为结尾标志. 添加空元素
NSArray* array00 = [[NSArray alloc] initWithObjects:@"Monday", @"Tuesday", [NSNull null], nil];
NSLog(@"array00, length=%i, %@", [array00 count], array00);
NSArray* array01 = [NSArray arrayWithObjects:@"Monday", @"Tuesday", [NSNull null], nil];
NSLog(@"array01, length=%i, %@", [array01 count], array01);
NSArray* array02 = @[@"Monday", @"Tuesday", [NSNull null]];
NSLog(@"array02, length=%i, %@", [array02 count], array02);
// 合并字符串数组
NSString* string = [array00 componentsJoinedByString: @"|"];
NSLog(@"\t componentsJoinedByString: '|' ==> %@", string);
// 字符串切割成数组
NSArray* array03 = [string componentsSeparatedByString: @"|"];
NSLog(@"\t componentsSeparatedByString: '|' ==> %@", array03);
}
- (void)handleNSArray
{
// 同字符串一样, 可以建立可变更的数组, 并设置一个初始量, 然后动态添加数组元素
NSMutableArray *array00 = [NSMutableArray arrayWithCapacity:50];
NSLog(@"array00 = %@", array00);
// 追加元素
[array00 addObject:@"apple"];
NSLog(@"\t addObject ==> %@", array00);
// 追加数组
[array00 addObjectsFromArray:[NSArray arrayWithObjects:@"banana", @"pear", nil]];
NSLog(@"\t addObjectsFromArray ==> %@", array00);
// 插入元素
[array00 insertObject:@"hello" atIndex:1];
NSLog(@"\t insertObject ==> %@", array00);
// 替换对象
[array00 replaceObjectAtIndex:2 withObject:@"已被替换"];
NSLog(@"\t replaceObjectAtIndex ==> %@", array00);
// 删除对象
[array00 removeObjectAtIndex:1];
NSLog(@"\t removeObjectAtIndex ==> %@", array00);
// 追加一个整型数据
int intValue = 5;
NSNumber* _intValue = [NSNumber numberWithInt:intValue];
[array00 addObject:_intValue];
// 追加一个字符型数据
char charValue = 'c';
NSNumber* _charValue = [NSNumber numberWithChar:charValue];
[array00 addObject:_charValue];
// 追加一个浮点型数据
float floatValue = 3.1415;
NSNumber* _floatValue = [NSNumber numberWithFloat:floatValue];
[array00 addObject:_floatValue];
// 追加一个C结构体
struct structType{
int m1;
int m2;
}structValue = {10, 20};
NSValue* _structValue = [NSValue valueWithBytes:&structValue objCType:@encode(struct structType)];
[array00 addObject:_structValue];
NSLog(@"\t add int, char, float and struct ==> %@", array00);
}
- (void)searchNSArray
{
NSArray* array00 = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday", nil];
NSLog(@"array00 = %@", array00);
// 第一种迭代方法, 通过索引取得对象
for(int i = 0; i < [array00 count]; i++)
{
id item = [array00 objectAtIndex:i];
NSLog(@"\t Iteration with 'for(;;)' ==> array00[%d]=%@", i, item);
}
// 第二种迭代方法, 通过迭代器进行查找
id item;
NSEnumerator* enumerator = [array00 objectEnumerator]; // 实现一个迭代器
while(item = [enumerator nextObject])
{
NSLog(@"\t Iteration with 'NSEnumerator' ==> obj=%@", item);
}
// 第三种迭代方法
for (id item in array00)
{
NSLog(@"\t Iteration with 'for(in)' ==> obj=%@", item);
}
}
- (void)constructNSDictionary
{
NSString* path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"dictionary.plist"];
NSLog(@"%@", path);
// 从内存创建, 并写成plist文件. 注意:元素以及id必须是NS基础类型, 写文件才能成功
NSDictionary* dictionary00 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"value3", @"key3", @"value4", @"key4", nil];
NSLog(@"dictionary00:%@", dictionary00);
[dictionary00 writeToFile:path atomically:YES];
// 读取plist文件, 存在读取失败的可能
NSDictionary* dictionary01 = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dictionary01:%@", dictionary01);
// 嵌套
NSDictionary* dic04_0 = [NSDictionary dictionaryWithObjectsAndKeys:@"04-0-value1", @"04-0-key1", @"04-0-value2", @"04-0-key2", @"04-0-value3", @"04-0-key3", @"04-0-value4", @"04-0-key4", nil];
NSDictionary* dic04_1 = [NSDictionary dictionaryWithObjectsAndKeys:@"04-1-value1", @"04-1-key1", @"04-1-value2", @"04-1-key2", @"04-1-value3", @"04-1-key3", @"04-1-value4", @"04-1-key4", nil];
NSDictionary* dic04_2 = [NSDictionary dictionaryWithObjectsAndKeys:@"04-2-value1", @"04-2-key1", @"04-2-value2", @"04-2-key2", @"04-2-value3", @"04-2-key3", @"04-2-value4", @"04-2-key4", nil];
NSDictionary* dic04 = [NSDictionary dictionaryWithObjectsAndKeys:dic04_0, @"dic04-0", dic04_1, @"dic04-1", dic04_2, @"dic04-2", nil];
[dic04 writeToFile:path atomically:YES];
NSLog(@"dic04:%@", dic04);
// 数组
NSArray* dic05 = [[NSArray alloc] initWithObjects:dic04_0, dic04_1, dic04_2, nil];
[dic05 writeToFile:path atomically:YES];
NSLog(@"dic05:%@", dic05);
}
- (void)handleNSDictionary
{
NSMutableDictionary* dictionary00 = [NSMutableDictionary dictionary];
NSLog(@"dictionary00:%@", dictionary00);
// 添加
[dictionary00 setObject: @"mdic_value01" forKey:@"mdic_key01"];
NSLog(@"\t setObject(insert) result ==> %@", dictionary00);
// 修改
[dictionary00 setObject: @"mdic_valueEx" forKey:@"mdic_key01"];
NSLog(@"\t setObject(modify) result ==> %@", dictionary00);
// 删除
[dictionary00 removeObjectForKey:@"mdic_key01"];
NSLog(@"\t removeObjectForKey result ==> %@", dictionary00);
}
- (void)searchNSDictionary
{
NSDictionary* dictionary00 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"value3", @"key3", @"value4", @"key4", nil];
NSLog(@"dictionary00:%@", dictionary00);
// 查询
NSString* values = [dictionary00 objectForKey:@"key2"];
NSLog(@"\t objectForKey ==> %@", values);
}