簡體   English   中英

XCode 8無法編譯適用於Xcode 9的Swift 3代碼

[英]XCode 8 failing to compile Swift 3 code that works on Xcode 9

有問題的代碼就是來自此答案的 ,並檢查給定索引是否可用。

具體來說,只有在XCode 8.3中才會出現以下問題(代碼在XCode 9 beta中可以很好地編譯。該代碼肯定是Swift 3,而不是4)。

在此處輸入圖片說明

我調查了與“缺少參數”錯誤有關的問題,典型的響應是確保包括可選參數。 但是,這里所有3個都包括optional i ,它是Index類型。

我在這里用語法做錯了什么?

語法更改為:

public func contains(where predicate: (Element) throws -> Bool) rethrows -> Bool

現在你應該使用這個

self.indices.contains(where: { (object) -> Bool in
     //make comparison here
})

要么

self.indices.contains(where: {$0 == "equal to something"})

或者,您可以使用如下所示的內容:

if self.indices.first(where: {$0 == "equal something"}) != nil {
    return self[i]
}else{
    return nil
}

順便說一句,在調用“ contains”后,集合仍然具有布爾值。 檢查一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM