2017-02-23 97 views
-2

我已經添加了Objective-C類別到我的Swift 3.0代碼。它有頭的方法:無法將'NSArray'類型的值轉換爲期望的參數類型'[Any]!'

+(UIBezierPath *)interpolateCGPointsWithHermite:(NSArray *)pointsAsNSValues closed:(BOOL)closed; 

當我把它從我的銀行代碼:

antiAliasing = dict.value(forKey: "antAliasing") as! NSArray 
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true) 

我得到了錯誤:

Cannot convert value of type 'NSArray' to expected argument type '[Any]!'

什麼是什麼問題?

+0

'抗鋸齒= dict.value(forKey: 「antAliasing」)作爲? NSArray ?? [ANY]'這將清除錯誤,但檢查r是否得到正確的結果 – Koushik

+0

U需要正確打開可選值 – Koushik

+3

從給定的參數標籤'pointsAsNSValues'我試試'as! [NSValue]'。除非你能解釋你爲什麼需要KVC,否則不要使用'valueForKey'。 – vadian

回答

2
  1. NSArray的替換用[NSValue],如@vadian建議
  2. 使用選購的轉換as?,因爲它是更可靠的,如果轉換失敗,你不會得到崩潰。
  3. 使用下標代替valueForKey方法。

下面是代碼:

if let antiAliasing = dict["antAliasing"] as? [NSValue] { 
    UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true) 
} 
相關問題