Storing to a variable vs passing directly (does that implicitly create a temp variable?)

前端 未结 1 1657
北荒
北荒 2021-01-26 06:46

Model 1

NSString *name = (NSString *)[response valueForKey:@\"name\"];
[someObject doSomethingWith:name];

Model 2

相关标签:
1条回答
  • 2021-01-26 07:09

    The code generated by the compiler will be the same, however, there's no variable in the second case. A variable is a high-level programming concept, the resulting assembly code knows about registers and memory only. And in both cases, the return value of the innermost method call needs to be stored somewhere, so either a register or place on the stack will be used for that.

    Also, id (what - [NSDictionary valueForKey:] returns) is generic and implicitly compatible with any object pointer type - please, don't cast its return value to NSString *.

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