代码之家  ›  专栏  ›  技术社区  ›  Krumelur

目标C:为什么这次财产分配没有按预期进行?

  •  1
  • Krumelur  · 技术社区  · 14 年前

    当谈到Objective-C的细节时,我有时仍然感到困惑。

    .h.:

       AVCaptureSession *capSession;
       @property(nonatomic, retain) AVCaptureSession *capSession;
    

    为什么在ObjC中这样做是正确的:

    m.先生:

    // Create instance locally, then assign to property and release local instance.
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    self.capSession = session;
    [session release];
    

    // Directly assign to property.
        self.capSession = [[AVCaptureSession alloc] init];
    

    我看到的主要问题是,我在第二个版本中缺少一个“版本”。是否可以使用“自动释放”作为替代:

      self.capSession = [[[AVCaptureSession alloc] init] autorelease];
    

    1 回复  |  直到 14 年前
        1
  •  1
  •   Carl Norum    14 年前

    是的,你的 autorelease alloc / init self.capSession = session ,调用 retain release 是的。 自动释放 最终会是一样的。