【发布时间】:2014-05-17 13:19:38
【问题描述】:
给定以下类:
MCAchievementCenter:MCModel(子类)
-(id) initWithDelgate:(id<MCAchievementNotifications>)delegate {
self = [super initWithRessource:@"achievements"];
if (self)
{
self.delegate = delegate;
}
return self;
}
MCModel(超类)
-(instancetype)initWithRessource:(NSString *)ressource {
NSString* ressourcePath = [[NSBundle mainBundle] pathForResource:ressource
ofType:@"json"];
NSData* raw = [NSData dataWithContentsOfFile:ressourcePath];
return [super initWithJSONData:raw];
}
注意:-initWithJSONData:raw 是 NSObject 上的一个类别方法,它使用 json 数据填充对象。 (见https://github.com/uacaps/NSObject-ObjectMap)
我的问题:MCAchievementCenter 的初始化失败,因为它变成了nil。
我做错了吗? 任何帮助表示赞赏。
更新:应用进入循环>
【问题讨论】:
-
-initWithJSONData:raw正在工作。示例:[[MCUser alloc] initWithRessource:@"user"];正在返回预期结果。 -
使用调试器,设置断点并检查变量。 ressourcePath == nil 吗?是原始的 == 零吗?
[super initWithJSONData:raw]返回什么?[super initWithRessource:@"achievements"]返回什么?等等…… -
您的屏幕截图仅显示
[[MCAchievementCenter alloc] initWith...]返回 nil。 - 你已经弄清楚 为什么 它返回 nil。正如我所说:单步执行您的代码并检查变量。将它们与您的预期进行比较。 -
@MartinR 您应该在
init方法中设置self;见文末:blog.wilshipley.com/2005/07/self-stupid-init.html -
你能发布你对
initWithJSONData:raw:的实现吗?虽然,我必须注意,初始化程序作为在类别中实现的方法 - 不是传统的
标签: ios objective-c oop subclass