简体   繁体   中英

No visible @interface for [object] declares the selector [method]

I am a complete beginner at Objective c and I am trying to complete a challenge in the book "iOS programming: The big Nerd Ranch guide".

I'm trying to put an object called item (of the class BNRItem) into an NSMutableArray called subItems which is part of an object called container (of the class BNRContainer, a subclass of BNRItem with the addition of the NSMutableArray to hold BNRItems). BNRItem works fine.

The code is as follows:

BNRContainer.h

#import <Foundation/Foundation.h>
#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
NSMutableArray *subItems;
}

BNRContainer.m

- (id)init
{
return [self initWithItemName:@"Container"
               valueInDollars:0
                 serialNumber:@""];
}

- (void)setSubItems:(BNRItem*)item
{
[subItems addObject:item];
}

Main.m

#import <Foundation/Foundation.h>
#import "BNRItem.h"
#import "BNRContainer.h"

int main(int argc, const char * argv[])
{

@autoreleasepool {

    BNRItem *item = [[BNRItem alloc] init];

    BNRContainer *container = [[BNRContainer alloc] init];

    [container setSubItems:item]

    }

return 0;
}

At the line [container setSubItems:item] I get the error: No visible @interface for container declares the selector setSubItems

The setter method setSubItems doesn't code complete (although other setters do, and work fine).

Am I doing something simple wrong? A simple explanation would be very appreciated!

In order for Xcode to generate the getters/setters for subItems, you have to actually declare the property for it in your interface. Something like this:

#import <Foundation/Foundation.h>
#import "BNRItem.h"

@interface BNRContainer : BNRItem
@property (strong, nonatomic) NSMutableArray *subItems;
@end

Additionally, you aren't ever actually alloc/initing your array, and the current logic for setSubItems: will not do what it sounds like it would do. This function would add the array passed as a parameter as an object within SubItems. If you're trying to add items from an array to subitems, then you should be using:

[myMutableArray addObjectsFromArray:<#(NSArray *)#>];

Update BNRContainer.h:

#import <Foundation/Foundation.h>
#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSMutableArray *subItems;
}
- (void)setSubItems:(BNRItem*)item;
@end

(Dunno why Fred deleted his answer.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM