当我更新代码到swift 4得到这个错误,我怎么能解决这个问题? ERROR SCREEN SHOT
let fullPath = destination.appendingPathComponent(pathString).path
let creationDate = Date()
let directoryAttributes = [FileAttributeKey.creationDate.rawValue : creationDate,
FileAttributeKey.modificationDate.rawValue : creationDate]
do {
if isDirectory {
try fileManager.createDirectory(atPath: fullPath, withIntermediateDirectories: true, attributes: directoryAttributes )
}
else {
let parentDirectory = (fullPath as NSString).deletingLastPathComponent
try fileManager.createDirectory(atPath: parentDirectory, withIntermediateDirectories: true, attributes: directoryAttributes)
}
错误:
Cannot convert value of type '[String : Date]' to expected argument type '[FileAttributeKey : Any]?'
其他行
let options: [String: Any] = [
NSAttributedString.DocumentAttributeKey.documentType.rawValue: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentAttributeKey.characterEncoding.rawValue: NSNumber(value: String.Encoding.utf8.rawValue)
]
try self.init(data: data, options: options, documentAttributes: nil)
}
再次出现此错误
Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedString.DocumentReadingOptionKey : Any]'
和其他行
let opt = [
NSAttributedString.DocumentAttributeKey.documentType.rawValue: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentAttributeKey.characterEncoding: String.Encoding.utf8
] as! [String : Any]
let data = string.data(using: String.Encoding.utf8)!
returnString = try! NSMutableAttributedString(data:data,options:opt as [String:AnyObject],documentAttributes:nil)
}
ERROR
Cannot convert value of type '[String : Any]' to type '[String : AnyObject]' in coercion
答案 0 :(得分:1)
请勿使用rawValue
。在字典中按原样使用键:
let directoryAttributes = [
FileAttributeKey.creationDate : creationDate,
FileAttributeKey.modificationDate : creationDate
]
而另一方:
let opt: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
不要在密钥上使用rawValue
(但是,如评论中的Leo所示,您确实需要utf8
值。并确保您的键和值是正确的类型。并阅读错误消息。它告诉你问题是什么。
你还需要改变:
returnString = try! NSMutableAttributedString(data:data,options:opt as [String:AnyObject],documentAttributes:nil)
为:
returnString = try! NSMutableAttributedString(data: data, options:opt, documentAttributes: nil)
不要不必要地施放,特别是错误的类型。