文章目录
1. 沙盒目录获取
1.1 document目录获取
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)
1.2 cache目录获取
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
2. 字符串拼接
2.1 字符串拼接
stringByAppendingString
NSString *path = [srcPath stringByAppendingString:[NSString stringWithFormat:@"/%@",imageName]];
2.2 路径拼接
stringByAppendingPathComponent
NSString *path = [srcPath stringByAppendingPathComponent:imageName];
2.3 hasPrefix
判断创建的字符串内容是否以某个字符开始,其语法形式如下:
- (BOOL)hasPrefix:(NSString *) aString;
3. NSFileManager
3.1 创建
NSFileManager *manager = [NSFileManager defaultManager];
3.2 浅度遍历目录
查找到目录下所有文件
NSArray *files = [manager contentsOfDirectoryAtPath:path error:nil];
3.3 深度遍历目录
获取当前目录下文件、子目录和文件
NSArray *files = [manager subpathsOfDirectoryAtPath:path error:nil];
3.4 获取文件的 属性
attributesOfItemAtPath获取文件的大小、文件的内容等属性,其语法形式如下:
- (NSDictionary * attributesOfItemAtPath:(NSString *)path error(NSError *_autoreleasing *)error;
// 其中,(NSString *)path用来指定要或者属性的文件;error(NSError *_autoreleasing *)error用来指定错误
文件属性相关的K-V
- NSFileAppendOnly: 文件是否只读
- NSFileBusy: 文件是否繁忙
- NSFileCreationDate: 文件创建日期
- NSFileOwnerAccountName: 文件所有者的名字
- NSFileGroupOwnerAccountName: 文件所有组的名字
- NSFileDeviceIdentifier: 文件所在驱动器的标示符
- NSFileExtensionHidden: 文件后缀是否隐藏
- NSFileGroupOwnerAccountID: 文件所有组的group ID
- NSFileHFSCreatorCode: 文件的HFS创建者的代码
- NSFileHFSTypeCode: 文件的HFS类型代码
- NSFileImmutable: 文件是否可以改变
- NSFileModificationDate: 文件修改日期
- NSFileOwnerAccountID: 文件所有者的ID
- NSFilePosixPermissions: 文件的Posix权限
- NSFileReferenceCount: 文件的链接数量
- NSFileSize: 文件的字节
- NSFileSystemFileNumber: 文件在文件系统的文件数量
- NSFileType: 文件类型
- NSDirectoryEnumerationSkipsSubdirectoryDescendants: 浅层的枚举,不会枚举子目录
- NSDirectoryEnumerationSkipsPackageDescendants: 不会扫描pakages的内容
- NSDirectoryEnumerationSkipsHiddenFile: 不会扫描隐藏文件
3.5 文件夹创建
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error
参数:
1.文件夹路径。
2.YES为如果文件不存在,则创建;NO,文件夹不创建。
3.文件夹的属性,可读可写,一般传nil。
4.错误信息。
3.6 文件创建
- (BOOL)createFileAtPath:(NSString *)path contents:(nullable NSData *)data attributes:(nullable NSDictionary<NSString *, id> *)att
参数:
1.文件路径(最后面拼接文件名)
2.需要新建的文件数据
3.属性
3.7 文件比较
比较两个path下的文件是否相同
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;
4. NSArray
4.1 sortedArrayUsingComparator
以降序为例
// 降序
NSArray *originArray=@[@2,@5,@4,@1,@3,@7,@6];
NSArray *resultArray=[originArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 integerValue]>[obj2 integerValue]) {
return NSOrderedAscending;
}
return NSOrderedDescending;
}];
// ("7","6","5","4","3","2","1",)>>>数组降序
如果你期望的是值小的在前而值大的在后,则可以在比较的时候返回NSOrderedAscending(-1),否则,就是NSOrderedDescending(1)。
5. 时间比较
// 日期之间比较可用以下方法
- (BOOL)isEqualToDate:(NSDate *)otherDate;
// 与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
// 与anotherDate比较,返回较早的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
// 与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending