0

i have this code :

NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i <[titleNews count]; ++i) {
    TFHppleElement *someText =[titleNews objectAtIndex:i];
    NSString *result = [someText content];

    [arr addObject:result];
    if ([[arr objectAtIndex:i]isEqualToString:@"-- : --"]) {
        [arr replaceObjectAtIndex:i withObject:@"x"];
        [arr addObject:@"/"];

    }

    NSLog(@"%@",[arr objectAtIndex:i]);
}

the (replaceObjectAtIndex: withObject:) work fine but the (addObject)method not working

3
  • How can you tell it's not working? Did you dump the entire array? Commented Nov 6, 2011 at 23:36
  • @ Daniel ,no i want to add the (/) object after the (x) in the array Commented Nov 6, 2011 at 23:39
  • Add an NSLog(@"%@", arr); after the closing } of your loop, to see what's in the array where. You'll be surprised. Commented Nov 6, 2011 at 23:41

1 Answer 1

2

You're presuming that the "result" that you add to the array will always be at index i. But the first time that you fall into your "if" body, you'll add an i+1 element ("/") and from then on everything will be off by one.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks daniel but i am new to iphone programing .so how i can solve this problem
You may be new to iPhone programming, but it's a simple general programming problem, not at all unique to iPhone. The NSMutableArray spec lists dozens of methods that may (or may not) help. Read them, think through what you're doing, and do some real programming, vs just modifying what you've already seen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.