拆分数组对象" 1; item1"分为1和item1

时间:2016-02-06 09:31:00

标签: ios arrays

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.我该怎么做?

1 个答案:

答案 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)
}