views:

183

answers:

1

Hello, i've written a for loop in Objective-C, This is how my code looks like

NSString *string = [NSString stringWithContentsOfFile=@"/Users/Home/myFile.doc"];
NSString *seperator=@"\n";
NSArray *mainarray = [string componentsSeparatedByString:seperator];
// Since i want to parse each element of mainarray
for(NSString *s in mainarray)
{
  //again parising the string using a new separator
    NSString newseparator = @"=";
    NSArray *subarray = [s componentsSeparatedByString : newseparator];

  //Copying the elements of array into key and object string variables

    NSString *key = [subarray objectAtIndex:0];
    NSLog(@"%@",key);
    NSString *class_name= [subarray objectAtIndex:1];
    NSLog(@"%@",class_name);

   // create an instance for the class_name
      //dont knw how it ll take the value from file and ???  

 //Putting the key and objects values into hashtable 
    NSMutableDictionary = [NSDictionary dictinaryWithObject:class_name forKey:key];
}

Whenever i execute this code this crashes my program saying as, Terminating the app due to uncaught exception NSRangeException

How to know the range of array and how to specify the terminating condition in the for loop???and plz let me knw how to handle this exception???

+2  A: 

I'm surprised that code even compiles. If I remember correctly, it can't compile unless you have gone to great lengths to turn off a whole bunch of compiler warnings.

NSString newseparator = @";";

That should give an error write there in that you don't have the *.

NSString *key = [subarray objectAtIndex[0]];
NSString *object = [subarray objectAtIndex[1]];

Neither of these lines of code make any sense.

It would appear that you haven't posted the actual code?


Now, getting back to the exception. A range exception will be tossed if you try to access an item at an index that is outside of the range of indexes available in the array. Thus, if componentsSeparatedByString: returned an array of 0 or 1 elements, then [subarray objectAtIndex: 1]; will cause a range exception to be raised.

What you don't want to do is to try and handle the exception using an @catch block. In Cocoa (and iPhone development), exceptions are treated as non-recoverable errors.

So, instead, use the -count method on NSArray to verify that the array actually contains the # of elements you were expecting. Since you are writing a casual parser, this is probably a good idea as a minimal check of input validity.

bbum
Hey sorry, its a typo error .. i've corrected my code above..
suse
how can i handle this exception? is ther any way of checking the range of array. like ** if(s!=nil)** , but even this didn't make any difference for me.
suse
No worries, just makes answering a bit more difficult and you'll tend to get more pointers to the Objective-C tutorial than you need. :) Answer edited.
bbum
ok, i ll use the -count method for counting the array elements. but how to add it as a terminating condition in for loop? Ones all the array element is parsed, taken into string and updated my hashtable ***How to make the control come out of for loop?***
suse
Given that the `subarray` is derived from `mainarray`, it seems unlikely that you would want to terminate the loop based on the contents of subarray. That is, unless you are OK with one element in the `mainarray` effectively being incomplete. In any case, you use the `break` keyword in C to break out of a `for()` loop.
bbum
ok.... ll try this
suse
hi bbum, in every iteration i need to instantiate a class, where the **NSString *object = [subarray objectAtIndex:1];**, object will be holding the content of class name. now for each iteration i need to create an object for that class. My prob is if i do it in this way, object *obj = [[object alloc]init];.how ll it replace the value of object as its class name?For every iterationthe value is access froma file which is written in above code.
suse
Hi .. do you have any idea how to add the value of 2 string into a hashtable ?. i.e in the above code i want to put the value of key and class_name for each iteration in for loop into hashtable. plz suggest me how to proceed.
suse
That seems like it is beyond the scope of this question. Ask a new question with details specific to these two issues.
bbum