解析是一个NsDictionary,我通过
从中取出余额如果让findbalances:AnyObject =解析["余额"]
给出了AnyObject格式的余额列表
(
{
"available_amount" = 133519;
currency = AUD;
},
{
"available_amount" = 7854;
currency = CAD;
},
{
"available_amount" = 88581;
currency = EUR;
},
{
"available_amount" = 0;
currency = GBP;
},
{
"available_amount" = 63618;
currency = INR;
},
{
"available_amount" = 375;
currency = NZD;
},
{
"available_amount" = 0;
currency = TRY;
},
{
"available_amount" = 2918958;
currency = USD;
}
)
我知道
let whatCurrency = (findbalances[7]["currency"] as! String)
= USD但如果对象数量发生变化,如何找到该值[7]?
我想匹配美元
我试过
let defaultcurr = "USD"
let findUSD = findbalances.indexOfObject(defaultcurr)
但这给了我9223372036854775807
我如何找到7
答案 0 :(得分:1)
9223372036854775807
是NSNotFound
。
您可以使用闭包作为indexOf
let defaultcurr = "USD"
let findUSDIndex = findbalances.indexOf { ($0["currency"] as! String) == defaultcurr }
或者您可以过滤整行
let findUSD = findbalances.filter { ($0["currency"] as! String) == defaultcurr }.first
indexOfObject
似乎正在使用NSArray
。不要那样做。使用Swift原生Array
并将您的集合对象转换为[[String:AnyObject]]
:
if let findbalances = parsed["balances"] as? [[String:AnyObject]] { ...