0

In the code below, I am trying to add objects to array. No error, but is not adding objects either. Sorry for asking this pretty basic question. Need help

The NS Object Definition
//DataDefinition.h #import

@interface DataDefinition : NSObject
@property (nonatomic, retain) NSString *dataHeader;
@property (nonatomic, retain) NSMutableArray *dataDetails;

@end

The DataDefinition Implementation

#import "DataDefinition.h"
@implementation DataDefinition

@synthesize dataHeader;
@synthesize dataDetails;

@end

The Display header section //DataDisplay.h #import

#import "DataDefinition.h"

@interface DataDisplay : UITableViewController
@property (strong, nonatomic) NSMutableArray *dataSet;
@property (strong, atomic) DataDefinition *individualData;

@end

The Display implementation section

 //DataDisplay.m
 #import "DataDisplay.h"

 @interface DataDisplay ()
 @end

 @implementation DataDisplay
 @synthesize dataSet;
 @synthesize individualData;

- (void)viewDidLoad
{

    [super viewDidLoad];

    individualData.dataHeader  = @"Header1";
    individualData.dataDetails = [[NSMutableArray alloc] initWithObjects:@"Header1-Detail1", @"Header1-Detail2", @"Header1-Detail3", nil];

    //This didnot add
    [dataSet addObject:individualData];
    NSLog(@"Count of objects is %d:",[dataSet count]);

    //Nor did this
    dataSet = [[NSMutableArray alloc] initWithObjects:individualData, nil];
    NSLog(@"Count of objects is %d:",[dataSet count]);

    self.title = @"DataDisplay";
}
user1509593
  • 1,025
  • 1
  • 10
  • 20

1 Answers1

1

The issue is that individualData is never actually set to an instantiated object (in other words, it is never initialized).

These kinds of oversights are common due to Objective-C's non-error policy regarding sending messages to nil; it's perfectly legal and often useful principle. This means that your code will never complain until you try to pass it to some method which will crash if it sees nil. Unfortunately, you are using initWithObjects, which simply sees nil as the end of the (empty) list. If you had instead tried to use [NSArray arrayWithObject:individualData] you may have seen an error which would hint to you that you had nil instead of an object.

Note that setting properties on nil is particularly tricky, since it looks like you are simply dealing with a C-syle lvalue, when actually it translates to a message-send call at runtime:

individualData.dataHeader  = @"Header1"; 
// is *literally* the same as:
[individualData setDataHeader:@"Header1"]; 

You can take your pick of solutions. The "cheap" way is to simply initialize it right there. The "better" way (usually) is lazy-instantiation (i.e. in the getter). Since the object is marked as atomic, you likely need to let the compiler write the getter for you, and just initialize it in viewDidLoad (or awakeFromNib, initWithCoder, or similar):

- (void)viewDidLoad
{

    [super viewDidLoad];
    self.individualData = [[DataDefinition alloc] init];
    ...
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • I tried this. But I get an error which says No visible @interface for 'DataDefinition' declares the selector 'alloc' – user1509593 Aug 26 '12 at 02:23
  • Are you importing the correct headers in your DataDisplay.m file? i.e. does that file have a succinct #import path to the DataDefinition.h file? If that class extends NSObject, there should be no two ways about it; alloc/init are there. – Chris Trahey Aug 26 '12 at 02:29
  • Okay, DataDefinition.h was created by File--> New --> File (Cocoa touch, Objective C) class as NSObject. DataDisplay.h has #import "DataDefinition.h" and DataDefinition.m has #import "DataDisplay.h" – user1509593 Aug 26 '12 at 02:34
  • Since you are only using it in a method signature (as opposed to extending it), DataDisplay.h should not `#import DataDefinition.h`, but instead use "forward declaration": `@class DataDefinition;`, then explicitly `#import DataDefinition.h` inside DataDisplay.m – Chris Trahey Aug 26 '12 at 02:54
  • I still have the problem ARC issue No visible @interface for 'DataDefinition' declares the selector 'alloc' – user1509593 Aug 26 '12 at 03:13
  • I was on it for the whole day and was doing mistake over mistake. Took a break. Watched some TV. Had a relook. You were right. Thanks – user1509593 Aug 26 '12 at 05:28