Do I need to release NSString generated using @“…”?

前端 未结 6 514
既然无缘
既然无缘 2020-12-03 10:52

If I make an NSString using the code below, do I need to need to release someString?

NSString *someString = @\"somestring\";
相关标签:
6条回答
  • 2020-12-03 11:22

    If you created an object via a method call that contains alloc, retain, or copy, or starts with new (N-A-R-C = "narc"), then you are responsible for releasing the object. If this is not the case, then you can ignore the object.

    So in the case of strings:

    NSString * myString = @"This is a string";
    

    I don't see a call there to a NARC method, so you are not responsible for releasing it. It's really that simple.

    0 讨论(0)
  • 2020-12-03 11:25

    So, please check what Apple does under the section "Insert Data Using a POST Request" at the following link:

    I see a [urlString release];, why?

    0 讨论(0)
  • 2020-12-03 11:31

    No, since it's a compile-time constant string, you do not need to release it. In fact, doing so will likely cause a run-time error.

    0 讨论(0)
  • 2020-12-03 11:36

    If it's a compile-time constant string, there wouldn't be a need to retain it as well. Is it correct?

    0 讨论(0)
  • 2020-12-03 11:38

    No, it's a compile time constant string object, so it doesn't need releasing. It's the moral equiv of char *c = "hello world" -- where the string hello world is in global data, and you're assigning the address of this data to the pointer c.

    0 讨论(0)
  • 2020-12-03 11:40

    I checked this case is different from NSString *someThing = @"someThing"; they should release urlString because of

    [[NSString alloc] initWithFormat:@"%@%@", baseURLString, queryTerm];
    

    Anywhere you use alloc/init you release it no mater what.

    0 讨论(0)
提交回复
热议问题