4

在 Objective-C 中,该方法stringWithFormat:似乎非常慢,实际上是我们的一个应用程序中的一个很大的瓶颈(我们使用分析器来发现这一点)。有没有办法优化它或使用一些更快的 C 代码?

4

2 回答 2

8

sprintf的,在 c http://www.cplusplus.com/reference/cstdio/sprintf/ 中使用,然后将 char* 推送到 NSString 中[NSString stringWithUTF8:];

例子:

char cString[255];
sprintf (cString, "%d", 36);
NSString* OCstring = [[NSString alloc] initWithUTF8String:cString];
于 2012-11-26T17:10:42.537 回答
6

If you're doing extensive string manipulations and operations - it sounds like you might well be doing so, and NSString really is becoming a bottleneck for your app, I recommend trying to use C++ for your string needs rather then C.

Apple admits that while NSString is great, it is top level, in fact, to make their autocorrect algorithm's for iOS they ran into a similar problem, NSString was too slow to compute and compare so many things. They then switched to C++ and got all the performance they needed.

Just a suggestion. You should definitely put up some code, I am surprised this is happening to you unless you're doing some awesome new feature !

于 2012-11-26T17:15:35.737 回答