-1

I have a class defined as :

@interface invItem : NSObject
{
    NSString* rowID;
    NSString* expDate;
    NSString* location;
}

@property(nonatomic, retain)NSString *rowID;
@property(nonatomic, retain)NSString *expDate;
@property(nonatomic, retain)NSString *location;

@end

I am reading the 3 strings from XML (using XMLParser) returned from a Web Service and then adding them to my invItem object

For Example:

currentItem.rowID = currentNodeContent; etc..

At the end of each row I read, I need to add the 'currentItem' with all 3 strings, to my array, I am using:

[myArray addObject:currentItem];

but when I inspect the array in the debugger it returns nil, as you can see below, what am I missing??

(lldb) po currentItem.rowID
1889

(lldb) po [[myArray objectAtIndex:0] rowID]
 nil
Rohit
  • 5,631
  • 4
  • 31
  • 59

2 Answers2

0

The problem seems to be that myArray itself is nil. You need to instantiate an array at some point, or you are just fruitlessly sending messages to nil. (And it had better be an NSMutableArray or you will crash when you send addObject to it.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I had forgotten to instantiate the array, however, after doing so I now get the following error: po [[myArray objectAtIndex:0]rowid] error: Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector. – Brian Ford Mar 15 '14 at 21:30
  • 1
    Do you actually know the basics of Objective-C? How to declare an object variable? An instance variable? Maybe you should stop and learn that stuff. – matt Mar 15 '14 at 21:45
0

Well, looks like you add allocated object to the array, otherwise you would get a crash. It can be easily tested with line:

[myArray addObject:nil];

Since you added non-nil object to the array, there are two possible reasons:

  1. myArray is nil
  2. rowID is nil

Check the code where you assign a value to the rowID, you can also use this approach:

currentItem.rowID = (currentNodeContent.length > 0) ? currentNodeContent : @"";

It will check the value and avoid of assign nil to rowID.

Igor Matyushkin
  • 778
  • 4
  • 4