Writing a C++ Wrapper around Objective-C

后端 未结 4 1888
臣服心动
臣服心动 2020-12-30 08:58

I want to call and work with Objective-C classes from within a C++ project on OS X. It is time to start moving towards all Objective-C, but we need to do this over some time

4条回答
  •  甜味超标
    2020-12-30 09:04

    First rename your files from *.m to *.mm so you get Objective-C++

    I have not tired this, so it is speculation (I will tonight):

    As all Objective-C++ objects (that are reference counted) are controlled via pointers so you can write a special destructor for shared pointer.

    template
    struct ObjCDestruct
    {
        void operator()(T* obj)
        {
            [obj release];
        }
    };
    

    Now you can stick your Objective-C obects in a boost::shared_ptr

    // FuncFile.M
    //
    int func()
    {
        boost::shared_ptr >  data([[MyX alloc] init]);
    
        [data.get() doAction1:@"HI"];
    }
    

提交回复
热议问题