无法将“[String : Date]"类型的值转换为预期的参数类型“[FileAttributeKey : Any]?" [英] Cannot convert value of type '[String : Date]' to expected argument type '[FileAttributeKey : Any]?'

查看:43
本文介绍了无法将“[String : Date]"类型的值转换为预期的参数类型“[FileAttributeKey : Any]?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将代码更新为 swift 4 时出现此错误,我该如何解决?错误截图

when i update code to swift 4 get this error , how i can fix this? 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.按原样使用字典中的键:

Don't use rawValue. Use the keys as-is in your dictionary:

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 值确实需要它).并确保您的键和值是正确的类型.并阅读错误消息.它会告诉您问题出在哪里.

Don't use rawValue on the keys (though, as demonstrated by Leo in the comments, you do need it for the utf8 value). And make sure your keys and values are of the proper type. And read the error messages. It tells you what the problem is.

而且你也需要改变:

returnString = try! NSMutableAttributedString(data:data,options:opt as [String:AnyObject],documentAttributes:nil)

到:

returnString = try! NSMutableAttributedString(data: data, options:opt, documentAttributes: nil)

不要不必要地强制转换,尤其是错误的类型.

Don't needlessly cast, especially to the wrong type.

这篇关于无法将“[String : Date]"类型的值转换为预期的参数类型“[FileAttributeKey : Any]?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆