Difference between these two NSString methods

前端 未结 5 1404
广开言路
广开言路 2021-01-05 08:59

So I just got asked this at an interview today and after some googling am still unable to figure out the answer (in fact I couldn\'t even find any code at all which used the

5条回答
  •  醉话见心
    2021-01-05 09:43

    Now my initial thoughts were that [NSString string] would return an object which would be autoreleased

    Technically, it’s a placeholder string that is constant, i.e., it lives throughout the entire program execution, never being released. It’s not an autoreleased string. Conceptually, and this is what I’d focus as an interviewer, it’s a string (an empty string) that is not owned by the caller, hence the caller shouldn’t release it.

    whereas using alloc and init would return an object which has been retained

    Technically, it’s a placeholder string that is constant, i.e., it lives throughout the entire program execution. In fact, it’s the same object as the one above, and it is not retained. Conceptually, and this is what I’d focus as an interviewer, it’s a string (an empty string) that is owned by the caller, hence the caller is responsible for releasing it when it’s not needed any longer.

提交回复
热议问题