[英]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". 我是Objective c的一个完整的初学者,我正在尝试完成“ iOS编程:大书呆子牧场指南”一书中的挑战。
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). 我正在尝试将一个名为item的对象(属于BNRItem类)放入一个称为subItems的NSMutableArray中,该对象是一个称为容器的对象的一部分(属于BNRContainer类,它是BNRItem的子类,另外还有NSMutableArray来容纳BNRItems)。 BNRItem works fine.
BNRItem工作正常。
The code is as follows: 代码如下:
BNRContainer.h BNRContainer.h
#import <Foundation/Foundation.h>
#import "BNRItem.h"
@interface BNRContainer : BNRItem
{
NSMutableArray *subItems;
}
BNRContainer.m BNRContainer.m
- (id)init
{
return [self initWithItemName:@"Container"
valueInDollars:0
serialNumber:@""];
}
- (void)setSubItems:(BNRItem*)item
{
[subItems addObject:item];
}
Main.m 的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 在[container setSubItems:item]行,出现错误: 容器的无可见@interface声明了选择器setSubItems
The setter method setSubItems doesn't code complete (although other setters do, and work fine). 设置方法setSubItems的代码无法完成(尽管其他设置方法可以完成,并且可以正常工作)。
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. 为了使Xcode生成子项的getter / setter,必须在接口中实际声明其属性。 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. 此外,您实际上从未分配/初始化数组,并且setSubItems:的当前逻辑不会像听起来那样做。 This function would add the array passed as a parameter as an object within SubItems.
此函数会将作为参数传递的数组添加为SubItems中的对象。 If you're trying to add items from an array to subitems, then you should be using:
如果您试图将数组中的项目添加到子项目中,那么您应该使用:
[myMutableArray addObjectsFromArray:<#(NSArray *)#>];
Update BNRContainer.h: 更新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.) (不知道为什么弗雷德删除了他的答案。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.