ARC 错误:init 方法必须返回与接收者类型相关的类型 [4]

发布于 2024-12-21 12:38:15 字数 388 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-28 12:38:15

问题中发布的 init 方法的形式错误。 init 方法(通常)应采用以下形式:

-(id)initWithParams:(BOOL)aBoolParam {
    if (self = [super init]) {
        //do stuff
    }
    return self;
}

上面代码的问题在于它是作为类方法完成的,因此如果发布者想要执行此操作,他必须执行 moment = [[Moment alloc] init] 并返回它。

The init method that was posted in the question was in the wrong form. The init method should (usually) have the form:

-(id)initWithParams:(BOOL)aBoolParam {
    if (self = [super init]) {
        //do stuff
    }
    return self;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文