目录

Objective-C_15.Foundation框架—字符串

一、Foundation框架中一些常用的类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    //字符串型:

    NSString:不可变字符串

    NSMutableString:可变字符串

    //集合型:

    //1)

    NSArrayOC不可变数组

    NSMutableArray:可变数组

    //2)

    NSSet

    NSMutableSet

    //3)

    NSDictiorary

    NSMutableDictiorary

    //其它:

    NSDate

    NSObject

二、NSString和NSMutableString的使用与注意

(一)6种创建字符串的形式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 最简单快速的创建方式(语法糖)
NSString *s1 = @"小明";

// NSString *s2 = [NSString stringWithString:@"hehe"];

// 使用格式
NSString *s3 = [[NSString alloc] initWithFormat:@"my age is %d", 18];

// C字符串转换成OC字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"xiaoming"];
// 反过来 OC 字符串转成 C 字符串
const char *cs = [s4 UTF8String];

// 从文件读取信息到字符串
// NSUTF8stringEncoding 用到中文就可以用这种编码
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"xxx/xxx/xx/xx.txt"
                                               encoding:NSUTF8StringEncoding
                                                  error:nil];

// 视频资源路径读取内容到字符串
// NSString *url = [[NSURL alloc] initWithString:@"file:///Users/xxx/Desktop/1.txt"]; //这里有三个斜杠
NSURL *url = [NSURL fileURLWithPath:@"/Users/xx/xx/xx.txt"]; //这里已经说明,所以不需要再包含协议头
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url
                                              encoding:NSUTF8StringEncoding
                                                 error:nil];

(二)使用注意

(1)字符串的导入导出
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// 字符串的导出
// 把字符串写入到文件, 若这个文件不存在,则创建一个
[@"xiaoming \n haha" writeToFile:@"/Users/xxx/xx/xx/xxtxt"
                  atomically:YES
                    encoding:NSUTF8StringEncoding
                       error:nil];

// 注意这里如果要换行的话 可以使用 \n
// 文件内筒中每一个换行都有一个 \n ,所以,可以通过统计 \n 的个数来测试代码量

// 把字符串导入到资源位置
NSString *str = @"249809234dsjsdj";
NSURL *url = [NSURL fileURLWithPath:@"/Users/xxx/xxx/xx/xx.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
// 这里的 atomically 后面可以是 YES 和 NO ,通常使用 YES,这样更安全,若中途写入失败,则不在创建文件
(2)类方法
1
2
3
4
5
6
7
// 一般都会有一个类方法跟对象方法配对,类方法通常都以类名开头
// 在实际的开发过程中,通常直接使用类方法
[NSURL URLWithString:(nonnull NSString *)];
[NSString stringWithFormat:(nonnull NSString *), ...];
[NSString stringWithContentsOfFile:(nonnull NSString *)
                          encoding:(NSStringEncoding)
                             error:(NSError * _Nullable __autoreleasing * _Nullable)];

(三)NSMutableString的使用与注意

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
NSMutableString *s1 = [NSMutableString stringWithFormat:@"xiaoming age is 22"];

// 修改 + 添加
// 拼接内容到s1的后面
[s1 appendString:@" haha"];

// 修改-删除
// 获取 is 的范围
// 在开发中,通常把这两个方法连起来使用,一个获取范围,一个进行删除
NSRange range = [s1 rangeOfString:@"is"];
[s1 deleteCharactersInRange:range];

// 不可变字符串
NSString *s2 = [NSString stringWithFormat:@"age is 10"];

NSString *s3 = [s2 stringByAppendingString:@"  xx "];
// 调用这s2这个方法,把s2的内容拷贝一份,加上xx拼接成一个新的字符串返回,s2字符串本身不变
NSLog(@" s1 = %@, s2 = %@", s1, s2);

// 打印结果
2016-07-31 00:16:38.498 test[3361:1273892]  s1 = xiaoming age  22 haha, s2 = age is 10

(四)URL补充内容

url:资源路径

格式:协议头:/路径

网络路径协议头:http

本地文件以file为协议头

ftp为协议头,说明资源是ftp服务器上的。