2016-08-12 46 views
-1

我打电话给我的服务器,并收到一个长字符串的响应,我分解并存储到数组中。我在Android上做得很完美,但似乎无法在Xcode中找到它。这就是我对IOS的事:IOS阵列分解错误

安卓

while (flag) { 
        ProductListArray.add(ProductListOneString.substring(0, ProductListOneString.indexOf('_'))); 
        ProductListOneString = ProductListOneString.substring(ProductListOneString.indexOf('_')); 

        if (ProductListOneString.equals("_")) { 
         flag = false; 
        } else { 
         ProductListOneString = ProductListOneString.substring(1); 
        } 
       } 


       int index = 2; 
       int ArraySize = ProductListArray.size(); 
       ProductKeycodes = new ArrayList<>(); 
       while(ArraySize > 0){ 
        ProductKeycodes.add(ProductListArray.get(index)); 
        index = index + 3; 
        ArraySize = ArraySize - 3; 
       } 

我至今在Xcode:

NSString *ProductListOneString = response; 
    NSMutableArray *ProductListArray; 


    bool flag = true; 

    while (flag) { 

     //[ProductListArray addObject: [ProductListOneString substringFromIndex:0 ProductListOneString: index(@"_", 0)]]; 


     [ProductListArray addObject: [[ProductListOneString componentsSeparatedByString:@"_"] objectAtIndex:0]]; 
     ProductListOneString = [[ProductListOneString componentsSeparatedByString:@"_"] objectAtIndex:0]; 

     if ([ProductListOneString isEqualToString: @"_"]) { 
      flag = false; 
     } else { 
      ProductListOneString = [ProductListOneString substringFromIndex: 1];//I get a *signal sigabrt* error here 
     } 
    } 



    NSInteger index = 2; 
    int ArraySize = [ProductListArray count]; 

    NSMutableArray *ProductKeycodes; 

    while(ArraySize > 0){ 

     [ProductKeycodes addObject: [ProductListArray objectAtIndex: index]]; 

     index = index + 3; 
     ArraySize = ArraySize - 3; 
    } 

回答

1

你先分手串入的子字符串数组打破“_”,然后测试以查看第一个这样的子字符串是否为“_” - 它不能作为分隔符不是子字符串的一部分。

您的Android代码正在迭代以实现componentsSeparatedByString:在一次调用中为您所做的一切。您可以完全替换while,并将componentsSeparatedByString:的结果添加到您的ProductListArray

BTW在Objective-C中以小写字母开头的变量 - 注意在使用大写字母时语法着色是错误的。

HTH