2017-10-12 9 views
0

私は4を迅速に更新するコードをこのエラーが発生すると、これをどのように修正できますか? ERROR SCREEN SHOT'[String:Date]'型の値を期待される引数型 '[FileAttributeKey:Any]に変換できません?'

 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

'directoryAttributes'変数の宣言と初期化を示す必要があります。 – rmaddy

答えて

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を使用しないでくださいutf8の値が必要です)。あなたのキーと値が正しいタイプであることを確認してください。そして、エラーメッセージを読んでください。それは問題が何であるかを伝えます。

そして、あなたはまた、変更する必要があります:不特に間違った型にキャストしないでください

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

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

をします。

+0

修正エラーですが、新しいエラーが発生しました@rmaddy –

+0

このオリジナルの質問とは無関係の新しいエラーが発生した場合は、この問題を解決し、新しい問題について新しい質問を投稿してください。 – rmaddy

+0

'.characterEncoding:String.Encoding.utf8'私はそれがコンパイルされ、動作するはずですが、そうではありません。 rawValue '.characterEncoding:String.Encoding.utf8.rawValue'を渡す必要があります。 –

関連する問題