gpt4 book ai didi

swift - 无法将类型 '[String : Date]' 的值转换为预期的参数类型 '[FileAttributeKey : Any]?'

转载 作者:行者123 更新时间:2023-11-28 15:16:45 28 4
gpt4 key购买 nike

当我将代码更新为 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)

}

错误

Cannot convert value of type '[String : Any]' to type '[String : AnyObject]' in coercion

最佳答案

不要使用 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)

不要不必要地转换,尤其是错误的类型。

关于swift - 无法将类型 '[String : Date]' 的值转换为预期的参数类型 '[FileAttributeKey : Any]?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719761/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com