Retaining ARC objects in c++ classes

后端 未结 2 645
傲寒
傲寒 2021-02-19 12:07

I have some code that must remain as c++, but I need to store objective-c objects in these c++ classes. The objects will not be referenced anywhere else while they are stored h

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 12:36

    I had some success using bridge casting when converting some old code to Xcode 4.3. It wouldn't let me bridge directly but I was able to bridge via a void *. I only did this to get some old code working so I can't guarantee it works perfectly.

    struct {
      __unsafe_unretained UIView *view;
    } blah;
    ...
    blah = malloc(sizeof(blah));
    ...
    // Cast the pointer with +1 to retain count
    blah->view = (__bridge UIView *) (__bridge_retained void *) myView;
    ...
    // blah->view can be used as normal
    ...
    // Transfer ownership back to ARC for releasing
    myView = (__bridge_transfer UIView *)(__bridge void *)blah->view;
    

提交回复
热议问题