-2

I have code that scans and returns NSString like this:

NSString *GetText = [[NSString alloc] init];
NSString *ScannedText;
NSScanner *TheScanner = [NSScanner scannerWithString:somLongString];
int start=0;
int index=0;
int ObjectCount;
char c;
for (int i = 0; i < [somLongString length] ; i++) {
    c = [somLongString characterAtIndex:i];
    if (c == '=') {
        start = i+1;
        [TheScanner setScanLocation:start];
        [TheScanner scanUpToString:@"&" intoString:&GetText];
        NSLog( @"%@",GetText);
        [UserValuesObject insertObject:GetText  atIndex:index];
        NSLog(@"%@",[UserValuesObject objectAtIndex:index]);
        index++;
    }
}

Now I want to add the GetText object I am creating each time to an array. When I try printing the first:

NSLog(@"%@",GetText); 

it works! But when I am tring to add it to the object and then print (for debug) I am getting null on each print of the log:

NSLog(@"%@",[UserValuesObject objectAtIndex:index]);

any ideas?

chown
  • 51,908
  • 16
  • 134
  • 170
orthehelper
  • 4,009
  • 10
  • 40
  • 67
  • 1
    What is `[UserValuesObject GetText atIndex:index];`? – Nekto Oct 27 '11 at 13:16
  • Sorry on the code its like this [UserValuesObject insertObject:GetText atIndex:index]; – orthehelper Oct 27 '11 at 13:20
  • 3
    Please include the code where you create `UserValuesObject` in the first place. Also, as a general note, variable names in objective-c start with a lower case letter and class names with an upper. – jrturton Oct 27 '11 at 13:23
  • ok i have try to change it but still doesn't work – orthehelper Oct 27 '11 at 13:27
  • @orazran can you please show the code where you alloc/init `UserValuesObject`? – chown Oct 27 '11 at 13:43
  • possible duplicate of [Having problems with adding objects to NSMutableArray](http://stackoverflow.com/questions/4716876/having-problems-with-adding-objects-to-nsmutablearray) or [NSMutableArray addObject Not Working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working) or [Cannot add items to NSMutableArray](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar) – jscs Oct 27 '11 at 18:41

2 Answers2

0

I presume that the NSMutableArray, UserValuesObject is nil and therefore is not allowing you to set any objects of the array.

Try checking to see if the array is nil or not before you enter the for loop.

i.e

if (UserValuesObject == nil) {
    UserValuesObject = [NSMutableArray array];
}
max_
  • 24,076
  • 39
  • 122
  • 211
0

You may not have initialized your array properly (or at all), try this (syntax improvements included):

NSString *getText = [NSString string];
NSScanner *scanner = [NSScanner scannerWithString:someLongString];
NSUInteger start = 0;
NSUInteger index = 0;
NSMutableArray *userValuesObject = [NSMutableArray array];
//  You should save the length of someLongString before starting the loop 
//   so that each iteration doesn't have to call length to see if i has 
//   reached length yet
for (int i = 0, len = [someLongString length]; i < len; ++i) {
    if ([somLongString characterAtIndex:i] == '=') {
        [scanner setScanLocation:(i + 1)];
        [scanner scanUpToString:@"&" intoString:getText];
        NSLog(@"%@", GetText);
        [userValuesObject insertObject:getText atIndex:index];
        NSLog(@"%@", [userValuesObject objectAtIndex:index]);
        ++index;
    }
}

From NSMutableArray Docs:

insertObject:atIndex:

Parameters

anObject - The object to add to the array's content. This value must not be nil.

Important: RaisesanNSInvalidArgumentException if anObject is nil.

index - The index in the array at which to insert anObject. This value must not be greater than the count of elements in the array.

Important: Raises an NSRangeException if index is greater than the number of elements in the array.

If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room. Note that NSArray objects are not like C arrays. That is, even though you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array. For example, if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds; if you try to add an object at index 3 (when the size of the array is 2), NSMutableArray raises an exception.

chown
  • 51,908
  • 16
  • 134
  • 170