我调用我的服务器并收到一个带有长字符串的响应,我将其分解并存储到数组中。我在Android上做得很完美但似乎无法在xcode中弄明白。这就是我必须为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;
}
答案 0 :(得分:1)
首先通过断开“_”将字符串分解为子串数组,然后测试以查看第一个这样的子字符串是否为“_” - 它不能是因为分隔符不是子字符串的一部分。
您的Android代码正在迭代以实现componentsSeparatedByString:
在一次通话中为您所做的事情。您可以完全替换while
并将componentsSeparatedByString:
的结果添加到ProductListArray
。
在Objective-C中使用小写字母的BTW开始变量 - 注意在使用大写字母时语法着色是如何错误的。
HTH