2015-11-04 50 views
0

我必须修复swift2的下列代码。在Swift 2中的错误处理

if !UIImagePNGRepresentation(img).writeToFile(imagePath, options: nil, error: &error) { 
         if let actualError = error { 
          NSLog("Image not saved. \(actualError)") 
         } 
        } 

要编译它,我有if row: Cannot invoke writeToFile这个错误类型(String, options: _, error: inout NSError?)

如何解决它的参数列表。

+3

对于将来的问题,尽量使标题尽可能与问题相关。大多数用户没有时间点击一个模糊的问题并解决一个模糊的问题。这会导致你的问题没有收到很多的流量,你没有得到你的答案。 – Arc676

回答

0

UIImagePNGRepresentation(img)?.writeToFile(imagePath, atomically: true) 

,而不是尝试。检查Apple Docs

编辑:

要回答你的问题更精确地使用error handling in Swift 2

do { 
    try UIImagePNGRepresentation(img)?.writeToFile(imagePath, options: .DataWritingAtomic) 
} catch let error as NSError { 
    print("Image not saved. \(error.description)") 
} 
+0

谢谢。有用。 –

+0

我很高兴!请将我的回答标为正确。 (左侧柜台下方的小勾号)谢谢。 – limfinity