0
NSString *title = [myWebView 
                   stringByEvaluatingJavaScriptFromString:@"document.title"];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:8];  
int j = 0;
int i=0;
int count=0;   
int len;
len = [title length];

for(i=0;i<len;i++){
    NSString *c = [title substringWithRange:NSMakeRange(i, 1)];

    if([c isEqualToString:@","])
    {
        //array[count]= [title substringWithRange:NSMakeRange(j, 2)];
        NSString *xxx = [title substringWithRange:NSMakeRange(j,(i-j))];
        NSLog(xxx);

        //insert the string into array
        [array insertObject:xxx atIndex:count];
        j=i;
        count = count + 1;
    }

}

My app always crashes at the line

[c isEqualToString:@","]

and gives the error - Thread1 : Program received signal: "SIGBART".

I know for sure that the problem is occurring while comparing strings since the app runs if I remove that one line of code.

Can someone please help? Thanks

6
  • The only way for the app to crash on the isEqualToString line is that the c variable was somehow released. Is your sample code unedited ? Maybe you could try to enable zombie debugging for your application : cocoaforbreakfast.wordpress.com/2011/02/25/… Commented Nov 13, 2011 at 11:14
  • 2
    Also, if your NSRange is invalid, you can get a NSRangeException. Are you sure there is none? Commented Nov 13, 2011 at 11:17
  • int i = 0; Right after I declared NSString *title Commented Nov 13, 2011 at 11:20
  • What other intervening code have you left out then? :) Commented Nov 13, 2011 at 11:23
  • edited the question. now it has all the source code Commented Nov 13, 2011 at 11:29

2 Answers 2

1

Consider using:

- (NSArray *)componentsSeparatedByString:(NSString *)separator

Example:

NSString *title = [myWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSMutableArray *array = [title componentsSeparatedByString:@","];  
Sign up to request clarification or add additional context in comments.

7 Comments

It crashes at the line you mentioned. It printed this line before crashing - [Switching to process 2307 thread 0x207] Current language: auto; currently objective-c
I also tried running the NSZombie and didn't discover anything out there :(
Perhaps title is not a valid string.
NSLog(title) prints out the following line - "part1","part2",number,"11/11/2011","part3","4567.21",4423322
I think CocoaFu is on the right track. Maybe the webview returns some strange subclass of NSString that causes the problem. Try NSLog("Class: %@",[title class]);. Maybe you can circumvent the problem using something like title = [NSString stringWithFormat:@"%@", title]. Or just use -isEqual: rather than isEqualToString:
|
0

If I ends up longer than the string, or is undefined, you've got issues. If you replace i with 1, it doesn't crash, right?

Incidentally, you could use:

unichar c = [title characterAtIndex:i];

5 Comments

It still crashed at the same line
if c were NULL, (or rather nil since it is an Obj-C object), then the expression would immediately evaluate to 0. In Objective C it is allowed to send messages to nil.
NSLog(@"%@", c); just before the crash line and tell us the output?
Good points, reckless me. Answer edited to focus on the point I was trying to make (still apparently incorrectly).
It printed all the characters of NSString *title (one in each line)

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.