i’m having this problem when i’m separating a string into an array using the componentsSeparatedByString function in xcode.
so i create an array from this string:
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:@" "]];
so in my mind i expect:
stickerarray = {@"1",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"1",@"0",@"1",@"1",@"1",@"0",@"0",@"0"}
so when i chuck it through an if statement to check if an index is = 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 is what i get:
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
I was amazed when i found this didn’t work so i tried applying some queries:
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
I suspect that it’s the @”1″ when i’m comparing it inside the if statement. Be a great help if you can fix this for me. Thanks 🙂
Your use of componentsSeparatedByString is probably working, but your test is flawed. In objective-c you need to use NSString’s isEqualToString. “==” compares the pointers of the two strings and would only be equal if they were pointing to the same instance of the string. You should use something more like: