Error Domain=NSCocoaErrorDomain Code=513 “您无权将文件“subash”保存在文件夹“tmp”中。” UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/902FE064-C3EC-42B5-A8F8-3D2923947067/tmp/subash,NSUnderlyingError=0x281e5c6f0 {错误域=NSPOSIXErrorDomain代码=1“操作不允许”}}
do {
var mytmppath:String=FileManager.default.temporaryDirectory.absoluteString+"subash"
try FileManager.default.createDirectory(atPath: mytmppath, withIntermediateDirectories: true, attributes: nil)
print( FileManager.default.subpaths(atPath: FileManager.default.temporaryDirectory.absoluteString))
} catch {
print(error)
}
您使用了错误的API。
absoluteString
适用于远程 URL,因为 API 还将返回 URL 方案(例如,http://
,在本例中为 file://
)。
path
。
尽管如此,强烈建议您不要使用
+
连接路径。始终使用 URL 相关的 API 和专用的路径操作方法。
do {
let defaultManager = FileManager.default
let temporarySubURL = defaultManager.temporaryDirectory.appendingPathComponent("subash")
try defaultManager.createDirectory(at: temporarySubURL, withIntermediateDirectories: true, attributes: nil)
print( defaultManager.subpaths(atPath: FileManager.default.temporaryDirectory.path))
} catch {
print(error)
}
使用 Objective c 尝试一下这个
e NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
documentsPath = [documentsPath stringByAppendingPathComponent:currentUser.mobileNumber];
NSString *filePathAndDirectory = [documentsPath stringByAppendingPathComponent:directoryName];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
}