我在Swift中有兩個自定義數據數組。我想找到包含第二個數組中的「Destination」等於'PlaceId'的字典值的對象。這裏有一些數據結構只是爲了展示他們的樣子。檢查值是否與自定義數組元素列表中的鍵匹配並檢索其值
struct FlightInfo {
var minPrice = "648"
var carrier = "Canada Air ways"
var destination = "973973"
var origin = "983983"
}
struct PlaceData {
var cityName = "Melbourne"
var name = "Melbourne"
var cityId = "MEL"
var type = "blah"
var countryName = "Australia"
var placeId = 983983
}
我得到這個迄今:
let destId = flightsArray[indexPath.row].destination as String
let results = listOfPlaces.filter { $0.placeId == Int(destId) }
,但沒有雪茄。我怎麼可以結束了類似以下內容作爲最終的結果是:
cell.destinationLabel.text = results.placeID
'$ 0 placeId'是一個'String',並且你試圖和'Int'('Int(destId)')進行比較。這可能是你的問題。而不是「{$ 0.placeId == destId}」? – Larme
在第一行代碼中嘗試'作爲Int',然後在第二行中將'Int(destId)'更改爲'destId' – Florensvb