无法使用类型为“(字符串,withIntermediateDirectories:B)的参数列表调用“createDirectoryAtPath”
Posted
技术标签:
【中文标题】无法使用类型为“(字符串,withIntermediateDirectories:B)的参数列表调用“createDirectoryAtPath”【英文标题】:Cannot invoke 'createDirectoryAtPath' with an argument list of type '(String, withIntermediateDirectories: B 【发布时间】:2015-10-07 07:42:13 【问题描述】:我正在为 sqlite 数据库使用 SwiftData 框架。当我将 Xcode 表单 6.4 更新到 7.0.1 时,swift data.swift 文件显示此错误'。我提供了下面的代码。
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
【问题讨论】:
【参考方案1】: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)
【讨论】:
以上是关于无法使用类型为“(字符串,withIntermediateDirectories:B)的参数列表调用“createDirectoryAtPath”的主要内容,如果未能解决你的问题,请参考以下文章
无法使用类型为“(UInt32)”的参数列表调用类型“CGBitmapInfo”的初始化程序