Cannot convert value of type '[String : Date]' to expected argument type '[FileAttributeKey : Any]?'

ε祈祈猫儿з 提交于 2019-12-24 18:32:58

问题


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)
            }

Error :

Cannot convert value of type '[String : Date]' to expected argument type '[FileAttributeKey : Any]?'

other line

    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)
  }

again this error

Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedString.DocumentReadingOptionKey : Any]'

and other line

 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

回答1:


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

let directoryAttributes = [
    FileAttributeKey.creationDate : creationDate,
    FileAttributeKey.modificationDate : creationDate
]

And for the other:

let opt: [NSAttributedString.DocumentReadingOptionKey: Any] = [
    .documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue
]

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.

And you also need to change:

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

to:

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

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



来源:https://stackoverflow.com/questions/46719761/cannot-convert-value-of-type-string-date-to-expected-argument-type-filea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!