Custom Object empty after creation

2019-07-19 01:51发布

问题:

I have a custom object: Vendor that extends NSObject. I am initiating it like so:

NSDictionary *vendorObj = [vendors objectAtIndex:i];
Vendor *vendor = [[Vendor alloc] initWithVendorInfo:vendorObj];
NSLog(@"VendorObj: %@", vendorObj);
NSLog(@"Vendor: %@", vendor);

Here is what the class looks like:

@interface Vendor : NSObject

    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, copy) NSString *description;

    - (id)initWithVendorInfo:(NSDictionary *)vendorDetails;

@end

@implementation Vendor

- (id)initWithVendorInfo:(NSDictionary *)vendorDetails
{
    self = [super init];
    if(self)
    {
        _name = [vendorDetails[@"company_name"] copy];
        _description = [vendorDetails[@"description"] copy];
    }
    return self;
}

If I NSLog vendorObj all the details are there. Once I initiate the Vendor object and NSLog it, the log shows:

2013-11-21 22:22:44.769 [48202:a07] Vendor:

I cannot seem to figure out why my object is nothing, no memory address, not even a null. What am I doing wrong here?

回答1:

The problem is your description property. The NSObject class defines a description method. This method is called when you use a %@ format specifier with an object.

Your description property is overriding that method.

Rename your description property to something else.