I wrote a class in Swift. I want to use this code in two separate iOS app projects that I wrote. Both the shared code and the apps are written in Swift. What is the best way of doing that?
I tried to create both a framework and a library in Swift and then add it as a sub-project to my app. In both cases I could not make the app see the module. I tried to add the shared module to "Target Dependencies" and "Link Binary With Libraries" of the main app's target. No luck - the app still can not see the classes of the shared module.
Using Xcode6 Beta6 at the moment.
As Konstantin Koval and Heliem pointed out, all I needed is to use public
in my class and method declarations, in the shared module. Now all works, both if I use workspace and if I add the module as a subproject.
I just found an excellent easy solution for reusing code between projects in Swift. It is called Carthage ( https://github.com/Carthage/Carthage ). This is not a plug as I am not affiliated with it in any way, I just really like it. It works in iOS 8+.
public
import 'LibTargetName'
You can take a more programatic approach by using SWM (Swift Modules): https://github.com/jankuca/swm
It is very similar to npm (node.js package manager) or bower (client-side module manager); you declare your dependencies in a swiftmodule.json
file like below. It does not have a central module registry like the two mentioned JS solutions, it only accepts git URLs.
{
"name": "ProjectName",
"dependencies": {
"Dependency": "git://github.com/…/….git"
}
}
…run swm install
and have the Dependency
module (compiled into a *.swiftmodule
binary) ready for import in the .modules/
directory.
import Dependency
And if you prefer to skip Xcode for app development, you can also build your whole app using swm build
(more info in the project's readme).
The project is still in early stages but it makes itself useful a lot for me at least. It provides the most clean (and clear) way of creating importable modules.
Here is a video which is very straightforward: http://eonil-observatory.tumblr.com/post/117205738262/a-proper-way-to-add-a-subproject-to-another-xcode
The video is for OS X instead of iOS. But you will get it and figure out the process for iOS.
Let's assume that AppA
needs to reused code from SharedProject
.
The following process works for me in Xcode 7 Beta:
SharedProject
. Project type must be Framework
instead of Application
. You write common code in this project and mark the code as public
. AppA
in Xcode, open the folder which contains SharedProject
in Finder. Drag the .xcodeproj file of SharedProject
from Finder and drop it into the root folder of AppA
in Xcode Project Navigator. AppA
--> Build Phases
--> Link Binary with Libraries
. Add SharedProject
. import SharedProject
and reuse code from SharedProject
! Edit:
Nowadays I suggest you use Carthage . It's better than the home made solution above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.