您的位置:首页 > 移动开发 > IOS开发

iOS 检查本地是否存在某个文件

2016-07-01 00:00 429 查看
Objective-C版

// 取得沙盒目录
NSString *localPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 要检查的文件目录
NSString *filePath = [localPath  stringByAppendingPathComponent:@"abc.doc"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
NSLog(@"文件abc.doc存在");
}
else {
NSLog(@"文件abc.doc不存在");
}

Swift版

// 取得沙盒目录
let localPath: NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first!
// 要检查的文件目录
let filePath = localPath.stringByAppendingPathComponent("abc.doc")
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filePath) {
print("文件abc.doc存在")
}
else {
print("文件abc.doc不存在")
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: