ARC 错误:init 方法必须返回与接收者类型相关的类型 [4]
ARC下这段代码有什么问题吗?我收到以上错误:
- (Moment *)initMoment:(BOOL)insert {
if (insert) {
self.moment = [NSEntityDescription insertNewObjectForEntityForName:@"Moment" inManagedObjectContext:self.managedObjectContext];
} else {
self.moment = [NSEntityDescription insertNewObjectForEntityForName:@"Moment" inManagedObjectContext:nil];
}
return self.moment;
}
Whats wrong with this code under ARC? I get above error:
- (Moment *)initMoment:(BOOL)insert {
if (insert) {
self.moment = [NSEntityDescription insertNewObjectForEntityForName:@"Moment" inManagedObjectContext:self.managedObjectContext];
} else {
self.moment = [NSEntityDescription insertNewObjectForEntityForName:@"Moment" inManagedObjectContext:nil];
}
return self.moment;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题中发布的
init
方法的形式错误。init
方法(通常)应采用以下形式:上面代码的问题在于它是作为类方法完成的,因此如果发布者想要执行此操作,他必须执行
moment = [[Moment alloc] init]
并返回它。The
init
method that was posted in the question was in the wrong form. Theinit
method should (usually) have the form:The problem with code above was that it was done as a class method, so if the poster wanted to do this he had to do
moment = [[Moment alloc] init]
and return it.