country = (
"3;India",
"4;US",
"5;UK",
)
state = (
"9;3;Uttar Pradesh",
"10;3;Maharashtra",
"11;3;Andaman and Nicobar Islands"
)
city = (
"110;3;10;pune",
"111;3;10;mumbai",
"112;3;10;nasik",
)
我想在dropdown
列表中仅显示国家/地区名称,并希望将其ID传递给网络服务。如果用户选择印度,我必须通过3.我该怎么做?
答案 0 :(得分:0)
据我所知,你有一个包含字符串的数组。
如果是这样,你只需要拆分你的字符串。
objective-c code
NSArray *country = @[@"3;India", @"4;US", @"5;UK"];
NSString *countryStr = country[0]; // 3; India
NSArray *countryArr = [countryStr componentsSeparatedByString:@";"];
NSString *countryCode = countryArr.firstObject; //3
NSString *countryName = countryArr.lastObject; //India
NSLog(@"%@", countryCode); //3
NSLog(@"%@", countryName); //India
swift code
let country : NSArray = ["3;India", "4;US", "5;UK"]
let countryStr = country[0]
let countryArr : NSArray = countryStr.componentsSeparatedByString(";")
let countryCode = countryArr.firstObject
let countryName = countryArr.lastObject
if let countryCodePr = countryCode {
print(countryCodePr)
}
if let countryNamePr = countryName {
print(countryNamePr)
}