1

Let me preface this question by saying that I believe it to be a memory management mistake on my end. I just can't seem to figure out why it is happening.

I have a viewcontroller and a model class named Part.

#import <Foundation/Foundation.h>

@interface Part : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *partType;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSNumber *price;

- (id)initWithName:(NSString *)name AndType:(NSString *)type;

@end

In the view controller I have a property set as follows:

@property (nonatomic, strong) Part *part;

In the init function of ViewController I create some static arrays and create objects out of them:

- (id)init {
    self = [super init];

    self.partList = [[NSMutableArray alloc] init];

    NSArray *inputArray = @[@"Part1",
    @"Part2",
    @"Part3",
    @"Part4",
    @"Part5",
    @"Part6",
    @"Part7",
    @"Part8"];

    NSString *tempType = @"PartCategory";

    // Add dummy static data
    for (int i = 0; i < [inputArray count]; i++) {
        Part *partInput = [[Part alloc] initWithName:[inputArray objectAtIndex:i] AndType:tempType];
        //partInput.name = [inputArray objectAtIndex:i];
        //partInput.partType = tempType;
        NSLog(@"Inserting Part %@", partInput);
        [self.partList addObject:partInput];
    }

    return self;
}

The NSLog I call in that loop returns Inserting Part *nil description* for every part. I just can't track down what is happening here.

EDIT: Here is the initWithName method from Part that the controller uses:

- (id)initWithName:(NSString *)name AndType:(NSString *)type {
    if(self = [super init]) {
        self.name = name;
        self.partType = type;
    }

    return self;
}
Alex
  • 1,233
  • 2
  • 17
  • 27
  • 1
    Can you please add the init method of `Part`. And you should always use `if (self)` after doing `self = [super init];`. – iDev Jan 18 '13 at 18:44
  • You should still check if `self` is a valid pointer in your `-(id)init` method ;) – Boris Prohaska Jan 18 '13 at 18:46
  • Show us `- (id)initWithName:(NSString *)name AndType:(NSString *)type` in implementation of your Part class. – 0x8badf00d Jan 18 '13 at 18:49
  • Sorry about that! I've added the `init` method as requested. Thanks for the swift replies. – Alex Jan 18 '13 at 18:51
  • 1
    Does the `Part` class override the `description` or `debugDescription` methods? – Tim Jan 18 '13 at 18:55
  • @Tim Nope. It does not override either of those. – Alex Jan 18 '13 at 18:56
  • 4
    @orbv, rename `NSString *description` to something else. You shouldnt be using `description` here. `description` is already used for printing the description of the object. – iDev Jan 18 '13 at 19:03
  • @Tim I suppose it does. I didn't even notice that. Thanks for pointing that out. – Alex Jan 18 '13 at 19:44

1 Answers1

7

When using %@ to print NSObject, it calls debugDescription that by default calling the description method of that object and your Part object always have nil description.

You better solve this by changing the description property name to something else, because it conflicts with the description method of NSObject.

See also: NSObject description and debugDescription

Community
  • 1
  • 1
tia
  • 9,518
  • 1
  • 30
  • 44