-3

我有一个这样的数组。

(

2对, 1, 3, 4

)

我想让它分开。像这样

(

2、1、3、4

)

我试过这个但没有工作。

NSMutableArray * array=[self.AnswerDic objectForKey:@"first"];
NSLog(@"%@",array);
optionA.text=[[[array objectAtIndex:0]componentsSeparatedByString:@"right"]objectAtIndex:0];
optionB.text=[[[array objectAtIndex:1]componentsSeparatedByString:@"right"]objectAtIndex:1];
optionC.text=[[[array objectAtIndex:2]componentsSeparatedByString:@"right"]objectAtIndex:2];
optionD.text=[[[array objectAtIndex:3]componentsSeparatedByString:@"right"]objectAtIndex:3];

给我警告。我怎样才能分离出字符串?

4

3 回答 3

1

用这个:

NSMutableArray * array=[self.AnswerDic objectForKey:@"first"];
NSLog(@"%@",array);
optionA.text=[[[array objectAtIndex:0]componentsSeparatedByString:@"right"]objectAtIndex:0];
optionB.text=[[[array objectAtIndex:1]componentsSeparatedByString:@"right"]objectAtIndex:0];
optionC.text=[[[array objectAtIndex:2]componentsSeparatedByString:@"right"]objectAtIndex:0];
optionD.text=[[[array objectAtIndex:3]componentsSeparatedByString:@"right"]objectAtIndex:0];

使用时将只有一个对象:

optionB.text=[[[array objectAtIndex:1]componentsSeparatedByString:@"right"]objectAtIndex:1];

所以索引 1 会引起问题。(对于连续代码有类似的问题)。所以使用:

optionB.text=[[[array objectAtIndex:1]componentsSeparatedByString:@"right"]objectAtIndex:0];
于 2013-09-26T06:46:43.213 回答
0
NSMutableArray * array=[self.AnswerDic objectForKey:@"first"];
optionA.text=[[[array objectAtIndex:0]componentsSeparatedByString:@"right"]objectAtIndex:0] ;

这样做

于 2013-09-26T06:22:01.630 回答
0

希望它会帮助你。

NSMutableArray *array=[self.AnswerDic objectForKey:@"first"];
NSMutableArray *FinalArray=[[NSMutableArray alloc] initWithCapacity:[array count]];

for(int i=0; i<[array count]; i++)
{
    [FinalArray addObject:[[(NSString *)[array objectAtIndex:i] componentsSeparatedByString:@"right"] objectAtIndex:0]];
}

NSLog(@"%@", FinalArray);
于 2013-09-26T06:22:48.133 回答