将数组对象“1; item1”拆分为1和item1

时间:2022-05-27 00:31:35
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",
        )

I want to show just the country name in dropdown list and want to pass it's Id to web service. If user selects India, I have to pass 3. How can I do this?

我想在下拉列表中只显示国家/地区名称,并希望将其ID传递给Web服务。如果用户选择印度,我必须通过3.我该怎么做?

1 个解决方案

#1


0  

As I understand you have an array with strings in it.

据我所知,你有一个包含字符串的数组。

If so, you just need to split your strings.

如果是这样,你只需要拆分你的字符串。

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

#1


0  

As I understand you have an array with strings in it.

据我所知,你有一个包含字符串的数组。

If so, you just need to split your strings.

如果是这样,你只需要拆分你的字符串。

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