2015-11-26 326 views
2

我想將BarCode Reader添加到我的項目中,但當我嘗試時遇到了一些問題。 在這裏我給出了錯誤代碼,如何解決這個請任何一個幫我解決這個問題。無法將類型NSMutableDictionary的值轉換爲期望的參數類型'[NSObject:AnyObject]?'

let dict = NSMutableDictionary() 
     dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 
     dict[NSLocalizedFailureReasonErrorKey] = failureReason 
     dict[NSUnderlyingErrorKey] = error 
     error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) //-----> In this line i have a error. 
+1

我得到一個類型不匹配的正是這樣當我用*轉換爲最新斯威夫特在Xcode 7.1中的語法*工具:( –

回答

7

你創建一個NSMutableDictionary,但NSError init方法已經更新到本地接受斯威夫特字典。試試這個:

var dict = [String: AnyObject]() 
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" 
dict[NSLocalizedFailureReasonErrorKey] = failureReason 
dict[NSUnderlyingErrorKey] = error 
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) 
+0

Oh ..謝謝,這是行之有效的... @Pascal。 –

1

您可以使用本機迅速字典帕斯卡的建議,或只是將它轉換爲[NSObject : AnyObject]

error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict as [NSObject : AnyObject]) 
+0

感謝您的回答,它也可以。.. @ Anni S. –

相關問題