[英]For Loop for checking whether specific id is found in a array
I need to check the id in a array list , id has been generated previously and just I need to compare with the response generated later, For example: take "defaultcardid" is the previous generated id, so I need to compare the "defaultcard" with later response.我需要检查数组列表中的 id ,id 之前已经生成,只是我需要与稍后生成的响应进行比较,例如:“defaultcardid”是之前生成的 id,所以我需要比较“defaultcard”稍后回复。
I am getting a "cardlist" as a "Anyobject" response.我收到一个“卡片列表”作为“Anyobject”响应。 so need to check "defaultcardid" inside "cardlist" array.所以需要检查“cardlist”数组中的“defaultcardid”。
if let xlist = (cardlist.value(forKey: "data")as? NSArray)
{
for 0 .. i in xlist
{
cardfinder.adding((xlist.object(at: id)as AnyObject).value(forKey: "id")as! String)
}
}
cardfinder is a NSMUTABLEARRAY to store the particular id into an array. cardfinder 是一个 NSMUTABLEARRAY,用于将特定的 id 存储到一个数组中。
My son response is below我儿子的回答如下
{
"status": "success",
"data": [
{
"id": "card_1D42hlLJSnYRR6BMULHyeobB",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "FwLfSpHMDz1b3PTH",
"funding": "debit",
"last4": "5556",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_1D441WLJSnYRR6BMNpOsFgg0",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "dvLGAxROCikDisQx",
"funding": "credit",
"last4": "0093",
"metadata": {},
"name": null,
"tokenization_method": null
},
{
"id": "card_1D42RjLJSnYRR6BMgSELUUut",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_DV2VCweBfI1dH0",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2019,
"fingerprint": "NnWxCq6FamUKl226",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
}
]
} }
All the objects have field called "id" I already have a default id = card_1D42RjLJSnYRR6BMgSELUUut , I need to exactly take in which object my card is .所有对象都有名为“id”的字段我已经有一个默认的 id = card_1D42RjLJSnYRR6BMgSELUUut ,我需要准确地接受我的卡是哪个对象。 so I need a for loop for checking all the objects in array.所以我需要一个 for 循环来检查数组中的所有对象。
You can simply call contains
method on array
to check whether a value exist in it.您可以简单地调用array
上的contains
方法来检查其中是否存在值。 Implementation depends upon the type of array
you are checking.实现取决于您正在检查的array
类型。
Example:例子:
Let's assume "123" is the default id
.让我们假设"123" is the default id
。
var cardList1 = ["123", "abc", "xyz"]
cardList1.contains("123") //true
cardList1.contains("456") //false
var cardList2 = [["id": "123"], ["id": "abc"], ["id": "xyz"]]
cardList2.contains { //true
return $0["id"] == "123"
}
cardList2.contains { //false
return $0["id"] == "564"
}
Let me know in case you face any issue with this.如果您遇到任何问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.