2017-02-20 25 views
1

我在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

'$ 0 placeId'是一個'String',並且你試圖和'Int'('Int(destId)')進行比較。這可能是你的問題。而不是「{$ 0.placeId == destId}」? – Larme

+0

在第一行代碼中嘗試'作爲Int',然後在第二行中將'Int(destId)'更改​​爲'destId' – Florensvb

回答

0

首先,我會重新考慮使用字典作爲模型對象。代替使用structs,classesenums是非常可靠的。 因此,你的模式將類似於此:

enum FlightType: String { 
    case a = "A" 
    case b = "B" 
} 

enum PlaceType: String { 
    case station = "station" 
    case port = "port" 
} 

struct Flight { 
    var name: String 
    var destinationId: Int 
    var type: FlightType 
} 

struct Place { 
    var identifier: Int 
    var title: String 
    var type: PlaceType 
} 

假設你有數組是這樣的:

let flights = [ 
    Flight(name: "Trenton", destinationId: 403, type: .a), 
    Flight(name: "Houston", destinationId: 288, type: .a), 
    Flight(name: "Boston", destinationId: 244, type: .a) 
] 

let places = [ 
    Place(identifier: 111, title: "Leslie", type: .station), 
    Place(identifier: 228, title: "Mary", type: .station), 
    Place(identifier: 684, title: "Joe", type: .station), 
    Place(identifier: 288, title: "Paul", type: .station), 
    Place(identifier: 465, title: "Stephanie", type: .station), 
    Place(identifier: 569, title: "Steve", type: .station), 
    Place(identifier: 663, title: "Doug", type: .station) 
] 

你可以飛行的目的地標題是這樣的:

let destinationId = flights[indexPath.row].destinationId 
let destinationTitle = places.filter{ $0.identifier == destinationId }.first?.title 
+0

謝謝,在我的代碼中,它們是結構體。但我確實喜歡你把它們組織成枚舉。我可能會這樣做,但都在同一個結構中。 – SlurBeast

0

以你的定義,以粗糙的代碼,這會給你想要的東西:

注意,在你的榜樣,你給了其他對象,所以只需將其轉換爲您的需求。

let flightsArray = 
    [[ "Name": "Trenton", "destination": 403, "type":"A" ], 
    [ "Name": "Houston", "destination": 288, "type":"A" ], 
    [ "Name": "Boston", "destination": 244, "type":"A" ]] 

var placedata = [ 
    ["PlaceId": "111", "title": "Leslie", "type": "station"], 
    ["PlaceId": "228", "title": "Mary", "type": "station"], 
    ["PlaceId": "684", "title": "Joe", "type": "station"], 
    ["PlaceId": "288", "title": "Paul", "type": "station"], 
    ["PlaceId": "465", "title": "Stephanie", "type": "station"], 
    ["PlaceId": "569", "title": "Steve", "type": "station"], 
    ["PlaceId": "663", "title": "Doug", "type": "station"], 
] 

for flightAt in flightsArray 
{ 
    let places = placedata.filter({ Int($0["PlaceId"]!)! == (flightAt["destination"] as! NSNumber).intValue }) 

    for place in places 
    { 
     print ("\(place["title"])") 
    } 
} 
0

你可以嘗試

let flightsArray = 
    [[ "Name": "", "destination": 403, "type":"A" ], 
[ "Name": "", "destination": 288, "type":"A" ], 
[ "Name": "", "destination": 244, "type":"A" ]] as [[String : Any]] 
var placedata = [ 
    ["PlaceId": "111", "title": "Leslie", "type": "station"], 
    ["PlaceId": "228", "title": "Mary", "type": "station"], 
    ["PlaceId": "684", "title": "Joe", "type": "station"], 
    ["PlaceId": "288", "title": "Paul", "type": "station"], 
    ["PlaceId": "465", "title": "Stephanie", "type": "station"], 
    ["PlaceId": "569", "title": "Steve", "type": "station"], 
    ["PlaceId": "663", "title": "Doug", "type": "station"], 
] 
let destId = flightsArray[indexPath.row]["destination"] as! Int 
let results = placedata.filter { $0["PlaceId"]! as String == "\(destId)" } 
cell.destinationLabel.text = results[0]["placeID"] 
相關問題