【发布时间】:2012-02-16 09:13:18
【问题描述】:
这是我的尝试:
H 文件:
@interface Strings : NSArray
@end
M 文件:
@implementation Strings
- (id) init
{
[self initWithObjects:
@"One.",
nil];
return self;
}
@end
当我跑步时,我得到了这个:
'NSInvalidArgumentException',原因:'* -[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
【问题讨论】:
-
我以为我可以扩展 NSArray 但即使它编译它也会崩溃。我最终在我所有模型的基类中编写了一个像你这样的静态函数。由于它是静态函数,我可以使用
[[self alloc] init]创建项目并附加到数组。
标签: objective-c ios nsarray extend