Bridge helps make interacting with Objective-C and the Objective-C runtime in Swift more Swift-like.
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Bridge into your Xcode project using Carthage, specify it in your Cartfile
:
github "impossibleflight/Bridge" ~> 1.0.0
Run carthage update
to build the framework and drag the built Bridge.framework
into your Xcode project.
If you prefer not to use either of the aforementioned dependency managers, you can integrate Bridge into your project manually.
Let's say you have an Obj-C interface that includes a method like this:
@interface MyStore : NSObject
// Note: raises an exception if object can't be found
- (id) objectForIdentifier:(String)identifier {...}
@end
Using it from Swift looks like this in cases where there is no object for the passed identifier:
let identifierForMissingObject = "acb123"
let object = myStore.object(forIdentifier: identifierForMissingObject) --> 💥
Your app crashes while trying to do that lookup—which is a bit unexpected based on the interface alone—and if you haven't read the docs you have no idea why. This is less than ideal; we would like to catch that situtation and be able to correct the underlying problem or at least let the user know what's happening. And we know Swift lets us catch errors of this sort, right?
Bridge to the rescue:
let identifierForMissingObject = "acb123"
do {
let object = try ObjC.throwing { myStore.object(forIdentifier: identifierForMissingObject) }
// do something with your object
} catch {
// uh oh. let's handle this.
}
Easily create strongly typed object associations:
extension Foo : ObjectAssociable {
var bar: NSNumber {
get {
return get(associatedObjectForKey: &AssociationKeys.bar) ?? 0
}
set {
set(associatedObject: newValue, forKey: &AssociationKeys.bar)
}
}
}
- John Clayton - Initial work - John Clayton
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details