无法将逗号分隔的字符串转换为 swift 中的整数数组
unable to convert coma seperated string to array of ints in swift
我正在获取整数字符串,如果是整数,我需要将其转换为数组
我需要这样 [8,9,10]
let stringOfints = "8,9,10"
let arrOfIds = stringOfints?.components(separatedBy: ",")
0/页:
["8","9", "10"]
编辑: 这里我需要用整数字符串
检查id
我的json喜欢这样
{
"jsonrpc": "2.0",
"result": {
"sub_category": [
{
"id": 2,
"title": "Logo Design"
},
{
"id": 3,
"title": "banner design"
},
{
"id": 5,
"title": "business web site"
},
{
"id": 10,
"title": "Business Card Design"
}
]
}
}
代码:这里 arrOfIds
是 [2, 3] 但是当我比较 arrOfIds 和 json id
并且如果它匹配然后将它的标题附加到 arrSubCat
但是如果我在 for 循环结束时打印 arrSubCat
那么为什么所有标题都会出现...请帮助
var arrSubCat:[String] = []
private func getSubCategoryServiceIDs() {
let param = ["category_id" : categoryID!]
APIReqeustManager.sharedInstance.serviceCall(param: param, url: CommonUrl.get_sub_category) { [weak self] (resp) in
self?.getSubCategoryIDs = GetSubCatModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
if self?.getServiceDetails?.result?.serviceDetails?.sub_cat_group != nil{
let kat = self?.getSubCategoryIDs?.result?.sub_category ?? []
let value = self?.getServiceDetails?.result?.serviceDetails?.sub_cat_group
let arrOfIds = value?.components(separatedBy: ",").compactMap { Int([=13=]) }
for singleCat in kat{
if ((arrOfIds?.contains(singleCat.id ?? 0)) != nil){
self?.arrSubCat.append(singleCat.title ?? "")
}
}
print("array of sub cat \(self?.arrSubCat)")//getting all titles why?
self?.collectionView.reloadData()
}
}
}
o/p
array of sub cat (["Logo Design", "banner design", "business web site", "Business Card Design"])
上一个答案
您必须将 String
转换为 Int
let stringOfints = "8,9,10"
let arrOfIds = stringOfints.components(separatedBy: ",").compactMap { Int([=10=]) }
更新答案
修改里面的代码 API 像下面这样调用并尝试。
let arrOfIds = value?.components(separatedBy: ",").compactMap { Int([=11=]) } ?? []
for singleCat in kat {
if arrOfIds.contains(singleCat.id ?? 0) {
self?.arrSubCat.append(singleCat.title ?? "")
}
}
我正在获取整数字符串,如果是整数,我需要将其转换为数组
我需要这样 [8,9,10]
let stringOfints = "8,9,10"
let arrOfIds = stringOfints?.components(separatedBy: ",")
0/页:
["8","9", "10"]
编辑: 这里我需要用整数字符串
检查id
我的json喜欢这样
{
"jsonrpc": "2.0",
"result": {
"sub_category": [
{
"id": 2,
"title": "Logo Design"
},
{
"id": 3,
"title": "banner design"
},
{
"id": 5,
"title": "business web site"
},
{
"id": 10,
"title": "Business Card Design"
}
]
}
}
代码:这里 arrOfIds
是 [2, 3] 但是当我比较 arrOfIds 和 json id
并且如果它匹配然后将它的标题附加到 arrSubCat
但是如果我在 for 循环结束时打印 arrSubCat
那么为什么所有标题都会出现...请帮助
var arrSubCat:[String] = []
private func getSubCategoryServiceIDs() {
let param = ["category_id" : categoryID!]
APIReqeustManager.sharedInstance.serviceCall(param: param, url: CommonUrl.get_sub_category) { [weak self] (resp) in
self?.getSubCategoryIDs = GetSubCatModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())
if self?.getServiceDetails?.result?.serviceDetails?.sub_cat_group != nil{
let kat = self?.getSubCategoryIDs?.result?.sub_category ?? []
let value = self?.getServiceDetails?.result?.serviceDetails?.sub_cat_group
let arrOfIds = value?.components(separatedBy: ",").compactMap { Int([=13=]) }
for singleCat in kat{
if ((arrOfIds?.contains(singleCat.id ?? 0)) != nil){
self?.arrSubCat.append(singleCat.title ?? "")
}
}
print("array of sub cat \(self?.arrSubCat)")//getting all titles why?
self?.collectionView.reloadData()
}
}
}
o/p
array of sub cat (["Logo Design", "banner design", "business web site", "Business Card Design"])
上一个答案
您必须将 String
转换为 Int
let stringOfints = "8,9,10"
let arrOfIds = stringOfints.components(separatedBy: ",").compactMap { Int([=10=]) }
更新答案
修改里面的代码 API 像下面这样调用并尝试。
let arrOfIds = value?.components(separatedBy: ",").compactMap { Int([=11=]) } ?? []
for singleCat in kat {
if arrOfIds.contains(singleCat.id ?? 0) {
self?.arrSubCat.append(singleCat.title ?? "")
}
}