Swift is great, but there's a lot of useful libraries that still exist in ObjC that we want to be able to use alongside our swift projects. Let's look at how to use Objective-C
code in a Swift
project!
Let's start a new project. A single view project is fine for what we're doing, just make sure the language is set to Swift
. Once a project is created, I'm personally a big fan of running it before I add anything just to make sure everything is configured properly.
Ok, everything is working fine, let's move right in and add an Objective-C
class. When you click 'New File' you'll see a new option with Xcode 6 called 'Cocoa Class'. Click that. Once the window is up, let's name our class 'ColorfulLabel', make it a subclass of UILabel
, and make sure that our language is set to Objective-C
. This way Xcode will generate our .h
and .m
file simultaneously. Click "Next" and then "Create" our new class.
You should be hit with a prompt that says "Would you like to configure an Objective-C bridging