Sharing classes between projects in xcode/objective-c

前端 未结 6 1345
遇见更好的自我
遇见更好的自我 2020-12-14 06:37

I\'ve got a client<=>server app I\'m building for Mac OS X, using Objective-c/Cocoa and xCode. I\'ve created a different project for both the apps I have, and I\'m wonder

6条回答
  •  感情败类
    2020-12-14 06:48

    I had a similar issue, and the accepted answer above may cause issues for a newbie.

    It will be fine the if the two projects communicate by some protocol e.g. TCP/IP, or if they don't communicate. If however one project is a bundle (e.g. a plugin) which needs to access the same classes as an application (whilst being run in the same process) you will get issues with linking or runtime warnings/errors about having classes with the same name. The simplest way to solve this problem is to use a framework. With a framework you can set it up so all three targets are in the same project, or you can even include the framework in separate projects.

    I had a project with an app and a bundle plugin, here are the steps I followed in Xcode 6:

    1. Create a framework, copy all the shared code over.
    2. In the main header of the framework, include the headers of all the shared code.
    3. Build the framework to test it builds (e.g. select the scheme of the framework and click play)
    4. Go to the Build Phases section of both the Application and the Plugin Bundle and add the new framework to ‘target dependencies’ and ‘Link binary with libraries’
    5. To include the frameworks stuff in code in the app and bundle, just use the main header, and use <> rather than “" e.g if your framework was called Foo use #import

    Changes to the framework will be compiled automatically when you run the main app, so you can in effect ignore the fact they are different targets.

提交回复
热议问题