String

沙盒文件读写

写入与读取的一个简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 创建一个json对象
NSArray *arr = @[@1,@2,@3];
NSData *data = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
if (!data) {
return;
}
NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

// 在 Documents/MyFolder 中保存这个json文件,文件名为“MyJsonFile”
@"MyFolder/MyJsonFile.json".docPath.saveFile(jsonStr);
// 或者
@"MyFolder".appendPathComponent(@"MyJsonFile").extension(@"json").docPath.saveFile(jsonStr);

// 读取并解析这个json文件
NSArray *readArr = @"MyFolder/MyJsonFile.json".docPath.readJson();

// 输出结果
AXLogOBJ(readArr);

控制台输出为:

1
2
3
4
5
6
➤ func:-[HomeVC viewDidLoad] line:108
💬[
1,
2,
3
]

除此之外,还可以读写数组、字典、字符串、归档解档文件、直接拼接沙盒中的纯文本文件

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
NSString *path = @"MyFolder/MyTextFile.txt".docPath;
// 如果没有文件,则会创建文件并写入传入的值
NSString *str1 = @"hello world!";
path.saveStringByAppendingToEndOfFile(str1);

// 读取并输出
AXLogOBJ(path.readString());

// 拼接
NSString *str2 = @"hello AXKit!";
path.saveStringByAppendingToEndOfFile(str2);

// 读取并输出
AXLogOBJ(path.readString());

// 删除文件
path.removeFile();

控制台输出为:

1
2
3
4
5
6
7
8
2018-03-13 13:49:28.785279+0800 AXKitDemo[43129:936443]
func:-[HomeVC viewDidLoad] line:98
💬hello world!


2018-03-13 13:49:28.785902+0800 AXKitDemo[43129:936443]
func:-[HomeVC viewDidLoad] line:105
💬hello world!hello AXKit!

路径的获取与拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NSString *fileName = @"FileName.txt";
// fileName在mainBundle中的完整路径
AXLogOBJ(fileName.mainBundlePath);
// fileName在Document中的完整路径
AXLogOBJ(fileName.docPath);
// fileName在Cache中的完整路径
AXLogOBJ(fileName.cachePath);
// fileName在Tmp中的完整路径
AXLogOBJ(fileName.tmpPath);

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSArray *allSubpaths = bundlePath.subpaths(nil);
AXLogOBJ(allSubpaths);

NSArray *specificSubpaths = bundlePath.subpaths(@"bundle");
AXLogOBJ(specificSubpaths);

控制台输出为:

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
33
34
func:-[HomeVC viewDidLoad] line:95
💬/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/FileName.txt


2018-03-13 13:56:28.648833+0800 AXKitDemo[43206:947211]
func:-[HomeVC viewDidLoad] line:97
💬/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Data/Application/D289F192-B070-400B-BB70-D57A16B157A4/Documents/FileName.txt


2018-03-13 13:56:28.648982+0800 AXKitDemo[43206:947211]
func:-[HomeVC viewDidLoad] line:99
💬/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Data/Application/D289F192-B070-400B-BB70-D57A16B157A4/Library/Caches/FileName.txt


2018-03-13 13:56:28.649177+0800 AXKitDemo[43206:947211]
func:-[HomeVC viewDidLoad] line:101
💬/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Data/Application/D289F192-B070-400B-BB70-D57A16B157A4/tmp/FileName.txt


2018-03-13 13:56:28.653414+0800 AXKitDemo[43206:947211]
func:-[HomeVC viewDidLoad] line:105
💬[
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/_CodeSignature,
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/_CodeSignature/CodeResources,
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/zh-Hans.lproj,
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/zh-Hans.lproj/LaunchScreen.strings,
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDem
2018-03-13 13:56:28.656689+0800 AXKitDemo[43206:947211]
func:-[HomeVC viewDidLoad] line:108
💬[
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/MJRefresh.bundle,
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/AXKit.bundle,
/Users/xaoxuu/Library/Developer/CoreSimulator/Devices/244FEB41-C498-42D3-B70C-FEAA3754519D/data/Containers/Bundle/Application/69CE97EA-DA87-4ACC-8457-7ED815FD1C4E/AXKitDemo.app/AXCameraKit.bundle
]

API Reference

预览:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma mark read

/**
读取一个数组或可变数组

@return 数组
*/
- (nullable __kindof NSArray *(^)(void))readArray;

/**
读取一个字典或可变字典

@return 字典
*/
- (nullable __kindof NSDictionary *(^)(void))readDictionary;

/**
读取一个json文件

@return 数组或字典
*/
- (nullable id (^)(void))readJson;

/**
读取一个纯文本文件

@return txt文件
*/
- (nullable NSString *(^)(void))readString;

/**
解档一个已归档的文件

@return 文件
*/
- (nullable id (^)(void))readArchivedFile;

#pragma mark - save

/**
保存一个文件

@return 成功与否
*/
- (BOOL(^)(id))saveFile;

/**
归档一个实现NSCoding协议的文件

@return 成功与否
*/
- (BOOL(^)(NSObject<NSCoding> *))saveArchivedFile;

/**
拼接文本到沙盒文件

@return 成功与否
*/
- (BOOL (^)(NSString *))saveStringByAppendingToEndOfFile;


#pragma mark - remove

/**
删除一个文件

@return 成功与否
*/
- (BOOL (^)(void))removeFile;

#pragma mark - path

/**
‘self’在mainBundle中的完整路径

@return ‘self’在mainBundle中的完整路径
*/
- (nullable NSString *)mainBundlePath;

/**
‘self’在documents中的完整路径

@return ‘self’在documents中的完整路径
*/
- (NSString *)docPath;

/**
‘self’在cache中的完整路径

@return ‘self’在cache中的完整路径
*/
- (NSString *)cachePath;

/**
‘self’在tmp中的完整路径

@return ‘self’在tmp中的完整路径
*/
- (NSString *)tmpPath;

/**
‘self’在NSSearchPathDirectory中的完整路径

@return ‘self’在NSSearchPathDirectory中的完整路径
*/
- (NSString *(^)(NSSearchPathDirectory))path;

/**
‘self’路径下的所有文件路径
*/
- (nullable NSArray<NSString *> *(^)(NSString * __nullable))subpaths;

/**
追加一个扩展名
*/
- (NSString *(^)(NSString * __nullable))extension;

/**
路径
*/
- (NSString *(^)(NSString * __nullable))appendPathComponent;

/**
创建路径是否存在

@return 创建路径是否存在
*/
- (BOOL (^)(void))isDirectoryExist;

/**
如果不存在创建路径

@return 路径
*/
- (BOOL (^)(void))createDirectory;

基本数据类型

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
NSString *boolStr = NSStringFromBool(YES);
AXLogOBJ(boolStr);

NSString *intStr = NSStringFromInt(100);
AXLogOBJ(intStr);

NSString *floatStr = NSStringFromFloat(M_PI);
AXLogOBJ(floatStr);

NSString *doubleStr = NSStringFromCGFloat(M_PI);
AXLogOBJ(doubleStr);

NSString *po = NSStringFromPointer(self);
AXLogOBJ(po);

NSString *ascii = NSStringFromASCIIValue(97);
AXLogOBJ(ascii);

NSString *percent = NSStringFromPercent(0.75);
AXLogOBJ(percent);

控制台输出为:

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
33
2018-03-13 14:17:01.399688+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:94
💬1


2018-03-13 14:17:01.399907+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:97
💬100


2018-03-13 14:17:01.400081+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:100
💬3.141593


2018-03-13 14:17:01.400404+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:103
💬3.141592653589793


2018-03-13 14:17:01.400705+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:106
💬0x7f8053c0dd50


2018-03-13 14:17:01.400864+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:109
💬a


2018-03-13 14:17:01.400981+0800 AXKitDemo[43446:973358]
➤ func:-[HomeVC viewDidLoad] line:112
💬75%

汉字转拼音

1
AXLogOBJ(SpellForChinese(@"你好"));

控制台输出为:

1
2
3
2018-03-13 14:19:03.558888+0800 AXKitDemo[43473:976316]
func:-[HomeVC viewDidLoad] line:94
💬ni hao

拼接字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
NSString *str = @"abc";

// 拼接数字
str = str.appendNumber(@1);
AXLogOBJ(str);

// 拼接换行符
str = str.appendReturn();
AXLogOBJ(str);

// 拼接字符串
str = str.append(@"def");
AXLogOBJ(str);

// 在前面拼接
str = str.prefix(@"hello: ");
AXLogOBJ(str);

控制台输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2018-03-13 14:26:09.081900+0800 AXKitDemo[44544:988848]
func:-[HomeVC viewDidLoad] line:96
💬abc1


2018-03-13 14:26:09.082111+0800 AXKitDemo[44544:988848]
func:-[HomeVC viewDidLoad] line:100
💬abc1



2018-03-13 14:26:09.082217+0800 AXKitDemo[44544:988848]
func:-[HomeVC viewDidLoad] line:104
💬abc1
def


2018-03-13 14:26:09.082317+0800 AXKitDemo[44544:988848]
func:-[HomeVC viewDidLoad] line:108
💬hello: abc1
def

URL转换

1
2
3
4
5
6
NSString *str = @"https://xaoxuu.com";
// 判断是不是URL字符串
AXLogBOOL(str.isURLString);

NSURL *url = str.absoluteURL;
AXLogOBJ(url);

控制台输出为:

1
2
3
4
5
6
7
8
2018-03-13 14:38:03.467648+0800 AXKitDemo[45628:1008735]
func:-[HomeVC viewDidLoad] line:95
🔵true


2018-03-13 14:38:03.467922+0800 AXKitDemo[45628:1008735]
func:-[HomeVC viewDidLoad] line:98
💬https://xaoxuu.com

随机字符串

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
随机字符串类型

- RandomStringTypeName: 姓名
- RandomStringTypePassword: 密码
- RandomStringTypeLower: 全部小写
- RandomStringTypeUpper: 全部大写
- RandomStringTypeCapitalize: 首字母大写
*/
typedef NS_ENUM(NSUInteger, RandomStringType){
RandomStringTypeName,
RandomStringTypePassword,

RandomStringTypeLower,
RandomStringTypeUpper,
RandomStringTypeCapitalize,
};

/**
产生随机字符串

@param type 随机类型
@param length 长度范围
@return 字符串
*/
FOUNDATION_EXTERN NSString *AXRandomStringFrom(RandomStringType type, AXUIntegerRange length);


/**
产生指定位数的随机二进制数

@param length 随机数的位数(字符串的长度)
@return 随机数字
*/
FOUNDATION_EXTERN NSString *AXRandomBinStringWithLength(NSUInteger length);


/**
产生指定位数的随机八进制数

@param length 随机数的位数(字符串的长度)
@return 随机数字
*/
FOUNDATION_EXTERN NSString *AXRandomOctStringWithLength(NSUInteger length);

/**
产生指定位数的随机十进制数

@param length 随机数的位数(字符串的长度)
@return 随机数字
*/
FOUNDATION_EXTERN NSString *AXRandomDecStringWithLength(NSUInteger length);


/**
产生指定位数的随机十六进制数

@param length 随机数的位数(字符串的长度)
@return 随机数字
*/
FOUNDATION_EXTERN NSString *AXRandomHexStringWithLength(NSUInteger length);



评论