Ishmael King
Ishmael King

Reputation: 63

Objective-C: Property not found on object of type

So I am very new to Objective-C and I was trying to expand on a working program I made from a lesson book.

So, I have a working class names "Item". Here is Item.h:

@interface Item : NSObject

{
    NSString *_itemName;
    NSString *_serialNumber;
    int _valueInDollars;
    NSDate *_dateCreated;
}

+(instancetype)randomItem;

It has a few functions to manipulate those variables, initialize them in a costume way and print them in a formatted manner.

Then I made a sub-class of "Item" called "Container". I wanted it to be similar to "Item" but have a NSMUtableArray variable that could hold an Array of "Item"s. Here is Container.h:

#import "Item.h"

@interface Container : Item

{
    NSString *_containerName;
    NSDate *_containerDatecreated;
    int _containerValueinDollars;
    NSMutableArray *_subItems;
}

+(instancetype) randomContainer;

Now in my main. I do a number of things with the "Item" class and they work fine. But when I do this:

Container *container=[Container randomContainer];
        for (NSString *item in container.containerName) {
            NSLog(@"%@", item);
        }

It says "Property 'containerName' not found on object of type 'Container *'". But I clearly added that variable to the "Container" subclass.

Note: randomContainer creates a random container instance and does some initializing to it. When I debug in Xcode it seems to be working fine.

What is my problem?

Upvotes: 1

Views: 8893

Answers (1)

bbum
bbum

Reputation: 162712

container.containerName is called dot syntax. It is the equivalent of [container containerName]. But you don't have a property or method on your class named containerName so the compiler is complaining.

Now, that is likely entirely baffling in the context of the book you are using. And that is because the book you are using is way way way out of date.

If it were up to date, your Item class would look more like:

@interface Item:NSObject
@property(copy) NSString *containerName;
@property(copy) NSDate *containerDateCreated;
@property(assign) NSInteger containerValueInDollars;
... etc ...
@end

Get yourself an up to date set of tutorials and/or books before learning anything else. It'll save a lot of frustration.

Upvotes: 3

Related Questions