如何扩展 NSArray?
这是我的尝试:
H file:
@interface Strings : NSArray
@end
M file:
@implementation Strings
- (id) init
{
[self initWithObjects:
@"One.",
nil];
return self;
}
@end
当我运行时,我得到这个:
'NSInvalidArgumentException', Reason: '* -[NSArray initWithObjects:count:]: 仅为抽象类定义的方法。定义 -[Strings initWithObjects:count:]!'
这就是我所做的:
H 文件:
@interface Strings : NSObject
+ (NSArray*) getStrings;
@end
M 文件:
@implementation Strings
+ (NSArray*) getStrings
{
NSArray* strings = [[NSArray alloc] initWithObjects:
@"One.",
nil];
return strings;
}
@end
Here's my try:
H file:
@interface Strings : NSArray
@end
M file:
@implementation Strings
- (id) init
{
[self initWithObjects:
@"One.",
nil];
return self;
}
@end
When I run I get this:
'NSInvalidArgumentException', reason: '* -[NSArray initWithObjects:count:]: method only defined for abstract class. Define -[Strings initWithObjects:count:]!'
This is what I did instead:
H file:
@interface Strings : NSObject
+ (NSArray*) getStrings;
@end
M file:
@implementation Strings
+ (NSArray*) getStrings
{
NSArray* strings = [[NSArray alloc] initWithObjects:
@"One.",
nil];
return strings;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
NSArray
是一个 类簇(链接到Apple的文档)。这意味着当您尝试创建NSArray
时,系统会创建NSArray
的一些私有子类。NSArray
类只是定义了一个接口;NSArray
的子类提供接口的实现。您可以编写自己的 NSArray 子类,但必须为数组中的对象提供自己的存储。您必须自己初始化该存储。错误消息告诉您这一点,即您需要在子类中重写
initWithObjects:count:
。您的重写需要将对象放入您作为类实现的一部分分配的任何存储中。可变参数
initWithObjects:
方法的NSArray
实现只是initWithObjects:count:
的包装,因此您不必实现initWithObjects:
。NSArray
is a class cluster (link to Apple's documentation). This means that when you try to create anNSArray
, the system creates some private subclass ofNSArray
. TheNSArray
class just defines an interface; subclasses ofNSArray
provide implementations of the interface.You can write your own subclass of
NSArray
, but you have to provide your own storage for the objects in the array. You have to initialize that storage yourself. The error message is telling you this, by saying that you need to overrideinitWithObjects:count:
in your subclass. Your override needs to put the objects into whatever storage you allocate as part of your class implementation.The
NSArray
implementation of the variadicinitWithObjects:
method is just a wrapper aroundinitWithObjects:count:
, so you don't have to implementinitWithObjects:
.您应该避免从
NSArray
派生。从文档中:这意味着当您初始化数组时,您不会获得
NSArray
的实例。您将获得一个完全不同的类的实例,但它仅具有相同的接口。这就是为什么子类化并不像您想象的那样工作:您必须自己完全实现存储。这就是为什么文档进一步指出:最后但并非最不重要的一点是,无论如何你都会遇到初始化错误。您需要调用
super
:但正如我刚才所说,它并不那么容易工作。您将再次遇到相同的异常。因此,您应该避免从
NSArray
派生。您可以做的是添加一个类别来向所有
NSArray
实例添加方法。Deriving from
NSArray
is something you should avoid. From the documentation:What this means is that when you initialize an array, you don't get an instance of
NSArray
. You'll get an instance of a totally different class that merely has the same interface. That is why subclassing doesn't work the way you think it works: you'll have to completely implement the storage yourself. This is why the documentation further states:Last but not least you would have had the initializing wrong anyway. You would have needed to call
super
:But as I just said, it just doesn't work that easily. You'll get the same exception again. So you should simply avoid doing deriving from
NSArray
.What you can do is add a category to add methods to all
NSArray
instances.NSArray 不支持以这种方式进行子类化。不过,您可以添加一个类别,尽管并不普遍推荐这样做。
有关更多想法,请参阅 Objective C - NSArray 子类。
NSArray doesn't support being subclassed in this way. You can add a category, though, although that's not universally recommended.
See Objective C - Subclassing NSArray for more thoughts.
也许
perhaps
您需要分配 self,并调用您的超类的 init 方法。
You need to assign self, and call your superclass' init method.