Which iPhone OS memory management rules and how-to's do you know?

后端 未结 4 1200
臣服心动
臣服心动 2021-02-09 23:03

Currently I am jumping into the ice cold water called \"memory management in iPhone OS\".

Here\'s one rule i\'ve learned:

Every time I see an alloc in my method,

4条回答
  •  温柔的废话
    2021-02-09 23:54

    Actually, any time you initialize an object and the method name includes "init" you are responsible for releasing it. If you create an object using a Class method that does not include the word "init" then you don't.

    For example:

      NSString *person = [NSString stringWithFormat:"My name is %@", name];
    

    does not need a release. But:

      Person *person = [[Person alloc] init];
    

    needs a release (as you stated in your question). Likewise:

      Person *person = [[Person alloc] initWithName:@"Matt"]];
    

    also needs a release.

    This is a convention, not a rule of the language, but you will find that it is true for all Apple-supplied APIs.

提交回复
热议问题