当我使用 xcode 中的 componentsSeparatedByString 函数将字符串分成数组时,我遇到了这个问题。
所以我从这个字符串创建一个数组:
theDataObject.stringstick = @"1 0 0 0 0 0 0 0 1 0 1 1 1 0 0 0";
stickerarray = [[NSMutableArray alloc] initWithArray:[theDataObject.stringstick componentsSeparatedByString:@" "]];
所以在我看来,我期望:
stickerarray = {@"1",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"1",@"0",@"1",@"1",@"1",@"0",@"0",@"0"}
所以当我通过 if 语句检查索引是否 = 1
for ( int n = 0; n <= 15; n++) {
if ([stickerarray objectAtIndex:n] == @"1") {
NSLog(@"this works %i", n);
} else {
NSLog(@"this did not work on %@", [stickerarray objectAtIndex:n]);
}
}
这就是我得到的:
this did not work on 1
this did not work on 0
this did not work on 0
this did not work on 0
this did not work on 0
this did not work on 0
this did not work on 0
this did not work on 0
this did not work on 1
this did not work on 0
this did not work on 1
this did not work on 1
this did not work on 1
this did not work on 0
this did not work on 0
this did not work on 0
当我发现这不起作用时,我很惊讶,所以我尝试应用一些查询:
NSLog(@"ns array value of %@",[stickerarray objectAtIndex:2] );
ns array value of 0
NSLog(@"%@", stickerarray);
(
1,
0,
0,
0,
0,
0,
0,
0,
1,
0,
1,
1,
1,
0,
0,
0
)
NSLog(@"%@", theDataObject.stringstick);
1 0 0 0 0 0 0 0 1 0 1 1 1 0 0 0
当我在 if 语句中比较它时,我怀疑它是 @"1" 。如果你能帮我解决这个问题,那就帮大忙了。谢谢 :)