【问题标题】:check if a value matches a key in a list of custom array elements and retrieve its value检查值是否与自定义数组元素列表中的键匹配并检索其值
【发布时间】:2017-02-20 12:51:16
【问题描述】:

我在 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.placeIdString,您尝试与 Int (Int(destId)) 进行比较。那可能是你的问题。 { $0.placeId == destId } 而不是?
  • 在第一行代码尝试as Int,然后在第二行将Int(destId)更改为destId

标签: arrays swift dictionary filter


【解决方案1】:

首先,我会重新考虑使用 Dictionary 作为模型对象。改用structsclassesenums 更可靠。 因此,您的模型看起来类似于:

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

【讨论】:

  • 谢谢,在我的代码中它们是结构体。但我确实喜欢你是否也将它们组织成枚举。我可能会这样做,但都在同一个结构中。
【解决方案2】:

把你的定义变成粗略的代码,这会给你想要的:

请注意,在您的示例中,您给出了其他对象,因此只需将其转换为您的需要。

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"])")
    }
}

【讨论】:

    【解决方案3】:

    你可以试试

    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"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多