我正在为sqlite数据库使用SwiftData框架。当我将Xcode表单6.4更新到7.0.1时,swift data.swift文件显示此错误无法使用类型'的参数列表调用'createDirectoryAtPath'(String,withIntermediateDirectories:Bool,attributes:NilLiteralConvertible,error:NilLiteralConvertible) “即可。我已经提供了以下代码。
public static func saveUIImage(image: UIImage) -> String? {
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
let imageDirPath = docsPath.stringByAppendingPathComponent("SwiftDataImages")
if !NSFileManager.defaultManager().fileExistsAtPath(imageDirPath) {
if !NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: nil, error: nil) {
print("Error creating SwiftData image folder")
return nil
}
}
let imageID = NSUUID().UUIDString
let imagePath = imageDirPath.stringByAppendingPathComponent(imageID)
let imageAsData = UIImagePNGRepresentation(image)
if !imageAsData!.writeToFile(imagePath, atomically: true) {
print("Error saving image")
return nil
}
return imageID
}
答案 0 :(得分:2)
createDirectoryAtPath
语法更改为:
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
因此您可以更新代码,例如“:
NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: [:])
您使用swift 2.0完成代码:
func saveUIImage(image: UIImage) -> String? {
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
let imageDirPath = (docsPath as NSString).stringByAppendingPathComponent("SwiftDataImages")
if !NSFileManager.defaultManager().fileExistsAtPath(imageDirPath) {
do{
try NSFileManager.defaultManager().createDirectoryAtPath(imageDirPath, withIntermediateDirectories: false, attributes: [:])
} catch {
print("Error creating SwiftData image folder")
return nil
}
}
let imageID = NSUUID().UUIDString
let imagePath = (imageDirPath as NSString).stringByAppendingPathComponent(imageID)
let imageAsData = UIImagePNGRepresentation(image)
if !imageAsData!.writeToFile(imagePath, atomically: true) {
print("Error saving image")
return nil
}
return imageID
}
或者您可以使用stringByAppendingPathComponent
的扩展程序,因为它在2.0中不适用于String
。
extension String {
func stringByAppendingPathComponent(path: String) -> String {
let nsSt = self as NSString
return nsSt.stringByAppendingPathComponent(path)
}
}
你可以这样使用它:
let imageDirPath = docsPath.stringByAppendingPathComponent("SwiftDataImages")
let imagePath = imageDirPath.stringByAppendingPathComponent(imageID)