2017-10-12 9 views
0

wenn ich Code auf Swift 4 update bekomme diesen Fehler, wie kann ich das beheben? ERROR SCREEN SHOTWert des Typs '[String: Date]' kann nicht in den erwarteten Argumenttyp '[FileAttributeKey: Any]?' Konvertiert werden.

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

Fehler:

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

andere Linie

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

wieder diesen Fehler

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

und andere Linie

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

Sie müssen Ihre Deklaration und Initialisierung Ihrer 'directoryAttributes' -Variable anzeigen. – rmaddy

Antwort

1

Sie rawValue nicht verwenden. Verwenden Sie die Tasten in Ihrem Wörterbuch, wie sie ist:

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

Und zum anderen:

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

Verwenden Sie keine rawValue auf den Tasten (obwohl, wie von Leo in den Kommentaren demonstriert, was Sie tun brauche es für den utf8 Wert). Und stellen Sie sicher, dass Ihre Schlüssel und Werte vom richtigen Typ sind. Und lies die Fehlermeldungen. Es sagt dir, was das Problem ist.

Und Sie müssen auch ändern:

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

zu:

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

nicht unnötig werfen, vor allem auf den falschen Typ.

+0

Dank Fehler beheben, aber neuen Fehler bekommen @rmaddy –

+0

Wenn Sie einen neuen Fehler haben, der nicht mit dieser ursprünglichen Frage zusammenhängt, sollten Sie diese Frage durch Akzeptieren der Antwort abschließen und eine neue Frage zu Ihrem neuen Problem stellen. – rmaddy

+0

'.characterEncoding: String.Encoding.utf8' Ich weiß, es kompiliert und sollte funktionieren, aber es tut nicht. Sie müssen den rawValue '.characterEncoding: String.Encoding.utf8.rawValue übergeben. –

Verwandte Themen