NSMakeCollectable and ARC doesn't work

后端 未结 5 1053
故里飘歌
故里飘歌 2020-12-16 10:46

I\'m trying to convert my old project to ARC. I have a function which creates UUIDs, but apparently this is no longer supported when using ARC:

NSString *uui         


        
5条回答
  •  旧时难觅i
    2020-12-16 11:41

    NSMakeCollectable() is for the benefit of the (essentially deprecated) Objective-C garbage collector. ARC knows nothing about it.

    You must use a special casting attribute, usually __bridge_transfer, to ensure that the memory is not leaked. __bridge_transfer is used like so:

    id MakeUUID(void) {
        id result = nil;
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        if (uuid) {
            result = (__bridge_transfer id)uuid; // this "transfers" a retain from CF's control to ARC's control.
        }
        return result;
    }
    

    Edit: As other answers have mentioned, CFBridgingRelease() does this for you. So instead of using (__bridge_transfer id)uuid, it may be cleaner to write CFBridgingRelease(uuid). They are equivalent though, so it's up to you which you find more readable.

提交回复
热议问题