开发者

Most efficient way to manage formatted strings for constantly updated UIlabels in Xcode

开发者 https://www.devze.com 2023-01-07 00:15 出处:网络
I have a UILabel in an interactive calculator application that is continually being refreshed with a newly formatted message as the user changes a UISlider value. My question is what is the most effic

I have a UILabel in an interactive calculator application that is continually being refreshed with a newly formatted message as the user changes a UISlider value. My question is what is the most efficient way to manage the strings:

NSString *data = [[NSString *alloc] initWithFormat:@"Value A: %0.1f, Value B: %0.1f, valueA,valueB];
myUILabel.text = data;
[data release];
开发者_运维知识库

or

[myMutableString setString:@""];
[myMutableString appendFormat:@"Value A: %0.1f, Value B: %0.1f, valueA,valueB];
myUILabel.text = myMutableString;

Any advice would be appreciated


Creating a single, non-mutable string (your first example) is going to be more efficient than creating and then modifying a mutable string.

However the real answer is that it doesn't matter. The amount of time is negligible in user interaction terms, and any temporary objects created as a side effect will be freed on every pass through the event loop anyway, so worrying about this at all is premature optimization.


As the string is there to be read by the user and so has to remain visible for a time I doubt that the speed of the format matters as it will take much less time than it takes for a user to read the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消