0

As far as I know, identical NSStrings are optimized in such a way that they are actually one and the same object in most(all?) circumstances.

If yes, does that mean that I can use an NSString pointer as a semaphore for the @synchronized directive and have it block whenever the code block is being executed with an identical string as semaphore?

- (void)doSomethingWithAString:(NSString *)myString
{
    @synchronized(myString) {
        //Something time intensive that never happens in parallel for the exact same myString
    }
}
4

1 回答 1

1

不,具有相同字符串的文字NSString实例不再保证是相同地址的相同实例。这是前一段时间所做的更改。

您可以使用全局字符串:

在文件.h

extern NSString *const MY_SYNC_STRING;

在文件.m

NSString *MY_SYNC_STRING = @"MYSyncString";

在 otherFile.m

#import "file.h"
    ...
    @synchronized(MY_SYNC_STRING) {
    ...
于 2013-05-17T13:49:33.563 回答