我在Swift中有两个自定义数据数组。我想找到包含字典值的对象,其“Destination”等于第二个数组中的'PlaceId'。这是带有一些数据的结构,只是为了显示它们的样子。
function validate()
{
if(isBlank(document.loginForm.loginid.value) || isNull(document.loginForm.loginid.value))
{
alert('Please Enter Login Id.');
document.loginForm.loginid.focus();
return false;
}
if(isBlank(document.loginForm.password.value) || isNull(document.loginForm.password.value))
{
alert('Please Enter Password.');
document.loginForm.password.focus();
return false;
}
var hashObj = new jsSHA("mySuperPassword", "ASCII");
var password = hashObj.getHash("SHA-512", "HEX");
var textval =document.loginForm.password.value; //The actual password entered by the user
var encryptedString = $.jCryption.encrypt(textval, password); //The password is encrypted and stored
document.loginForm.password.value = encryptedString; //The password field of the form is updated with "encrypedString"
document.loginForm.hiddenfield.value=password;
document.loginForm.service_id.value=get_cookie(document.loginForm.loginid.value.toUpperCase());
document.loginForm.action="http://pict.ethdigitalcampus.com:80/DCWeb/authenticate.do";
return true;
}
到目前为止我得到了这个:
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) }
答案 0 :(得分: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"])")
}
}
答案 1 :(得分: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"]
答案 2 :(得分:0)
首先,我会重新考虑使用Dictionary作为模型对象。使用structs
,classes
和enums
代替它是非常可靠的。
因此,您的模型看起来与此类似:
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