Trying to create a utility project that is shared between my company's iOS apps. I've done this at a previous job, but that predated Swift. I would obviously like to keep all of the implementation in swift, not Obj-C.
I tried the cocoapod route, using this guide, but there are build issues there before I can even start using my utility code in the main project.
I'm now trying with just a 'Cocoa Touch Static Library' whose language is Swift, and still no luck. I imported the entire .xcodeproj
file into my workspace. For now, I just have one .swift
file, plus the header file that is generated by XCode.
My project is just called IosUtilsTest
.
In particular, my test util file:
extension UIBarButtonItem {
class func flexible() -> UIBarButtonItem {
return UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
}
}
And in my app:
import IosUtilsTest
....
toolbar.items = [UIBarButtonItem.flexible(), centeredButton, UIBarButtonItem.flexible()]
....
I get a compiler error that flexible
doesn't exist.
I have tried including both <IosUtilsTest/IosUtilsTest-swift.h>
and <IosUtilsTest/IosUtilsTest.h>
in my bridging header.
Basically it just seems like the extension isn't being included in the build. FWIW -- I remember there was an issue with Categories requiring a special build flag, so I tried this with just a class that I tried to instantiate, and it was the same basic error.
A static library is a collection of compiled object files combined into a single library file, that may be linked into an app or framework target just like single object files are linked.
As mentioned, Apple does allow Swift in static libraries as of Xcode 9 Beta 4.
Create Static Library Project Open Xcode and select Cocoa Touch Static Library . Give it a name and select Swift as the development language. In our case, we will call it Networking and assume that it will contain the code to communicate with a back end. Press Cmd+N and select Swift file .
Yes, you can use static libraries in Swift. Go to your Build Phases and under "Link Binary With Libraries" and add them there. Alternatively, you can go under Build Settings and in "Search Paths" append the "Library Search Paths" value to include the path to the folder that your . a file is in.
We have a project comprising lots of swift framework modules that are all built together into an app, so I should be able to provide some help.
Firstly, you want to create a Cocoa Touch Framework as the project type.
The first gotcha, that got me, was that everything in the framework has to be declared public, as it is internal by default. Well, at least the parts of the API you want to expose to the clients. So use:
public extension UIBarButtonItem {
class func flexible() -> UIBarButtonItem {
return UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
}
}
You shouldn't need to do anymore than this, just import your module in the .swift file and also include the framework in the link binary with libraries.
We also tried cocoapods and spent quite a bit of time on it to no avail. Lots of build errors so we just gave up and used Git submodules. You're right, it's not quite as nice as cocoapod setup - but it works fine for us, just a little more overhead. You can flag your submodules as 'automatically fetch' in .gitmodules if you like.
One thing to be careful with cocoa pods if you get it working is that for the time being, all your app must be built using the same version of Xcode. So if you have a module that is built with an older version of Xcode and then put into cocoa pods, you would need to ensure you rebuild it when Xcode is updated. This is due to the swift runtime that is packaged with the build - see Apples Swift Blog for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With