Working With Swift & Cocoa Touch On iOS 8: Some Pointers

By | May 5, 2015

For making apps for the iOS 8 platform, Swift is the preferred language for many developers. However, the Objective-C based Cocoa Touch framework is still immensely popular as well. In the following discussion, we have deliberated on how Cocoa Touch can be optimally used on iOS 8, with Swift and Obj-C.

A survey conducted among iOS developers in first quarter of 2015 revealed that over 20% of them work with Apple’s new Swift programming language. There is a significant section among them who use Swift as the primary language, with Objective-C being relegated to the secondary position. Given its speed advantage (almost 9 times faster than Python, and around 2.5 times faster than Objective-C), the impressive adoption rate of Swift does not come as a surprise.


Amidst all the buzz about Swift, there is one potential source of confusion, however. Most iOS app developers to date use the Cocoa Touch framework to create applications. The libraries of Cocoa Touch are in Objective-C, which makes it necessary for coders to know how to seamlessly implement Swift codes in the framework. In addition, some additional knowledge is also required for working with Cocoa Touch on the iOS 8 platform. We will here share a few basic tips involving Swift, Cocoa Touch and iOS 8:


  1. Moving from Obj-C Classes to Swift Methods – After importing the Cocoa Touch framework in their projects (more on that in a bit), iPhone app development experts have to generate objects in Swift. A sample command line for this purpose would be: var myImageView:UIImageView = UIImageView (image:myImage). Unlike in Objective-C classes, ‘initwith’ is not required for class initialization in the Swift environment. There is no need for calling ‘alloc’ either. Another advantage of the new language is that, the variable type does not have to be specified at the time of creating class instances (Swift handles this on its own).
  2. Start with a Universal Framework – You might have to tweak around a bit with the default Xcode settings (make sure you have the latest iteration of Xcode 6) for this. On the target project, the ‘Build Active Architecture’ option has to be set to ‘No’ (this is necessary for making the framework universal). In case you find that the app builds have become laggy or unstable after doing this, check the modularity of the system. Select armv7, arm64 and armv7s as the ‘Valid Architectures’.
  3. Say goodbye to Project Editor for importing frameworks – Programmers working on iOS 8 apps no longer have to use the Project Editor to import Cocoa Touch frameworks in Swift. A one word ‘import’ statement does the job (for instance, import MediaPlayer). The headers of the Obj-C framework are transformed into Swift language APIs (after they have been compiled into modules). All the functions and methods used in Objective-C get changed to appropriate Swift classes (e.g, String in Swift in place of NSString in Objective-C).
  4. Including Frameworks In App Projects – Doing this is easy enough in the Xcode environment on iOS 8. All that mobile app developers have to do is make a /lib directory at the root of the project. In it, all the required custom frameworks and artifacts will be stored. Once that is done, move to ‘Framework Search Paths’, and write: $(PROJECT_DIR)/lib. If any error is generated, repeat the entire process. The same thing has to be added to ‘Runpath Search Paths’, when unit testing is being performed on an app.
  5. Implementation of Obj-C Methods within Swift – Apple has reiterated again and again that Swift is not meant to be a replacement of Objective-C. iOS developers can use the two languages in collaboration – and this is a further proof of that. Three key differences between the message call methods in Obj-C and Swift are: a) the usage of ‘dot syntax’ in the latter, b) the disappearance of the starting parentheses (the third brackets in Objective-C), and c) the non-requirement of the semicolon at the end of the line. The name of the Swift Method is the same as the Selector name in the earlier language. The different components of the Obj-C selector (except the first one) become Swift Arguments. The syntax of the names and arguments change when the Obj-C framework is imported in Swift.
  6. Factory Methods to Initializer Methods – Objective-C gives the option to those coding for iOS apps to init and alloc simultaneously (a new object is generated). This is known as the Factory Method. Once the framework is imported to Swift, this gets changed to the latter’s built-in Initializer Method – which looks something like this: var myXXXX = UIXXXX (named: “abc.png”). At the time of the change from Factory Method to Initializer Method, the name of the parameters also get slightly modified (for example, imageNamed becomes named).
  7. Generating lower-level dependent frameworks – Another smart way to use Cocoa Touch optimally on the iOS 8 platform. After creating a Universal Framework via the method mentioned above, move to the Build Settings of the target object, and make sure that the ‘Always Search User Paths’ option is turned to ‘YES’. Repeat the procedure in the Build Settings of the test object as well. In the Build Phase of test and target objects, you can include the /lib frameworks to the ‘Link Binaries With Libraries’ section. While testing the app, additional paths might have to be declared in ‘Runpath Search Paths’.
  8. Dot Syntax for calling Methods – For long-time users of Objective-C, getting used to the simpler ‘dot syntax’ methods to call Swift methods might take some time. The process is extremely simple (particularly so if there is a single parameter in the method). A single argument has to be passed for calling the one-parameter method. And what if the method to be called has multiple parameters? The solution is easy enough: developers need to name all (except the first) arguments, and add ‘animated:’ and ‘true’ next to each of them. The ‘dot syntax’ method-calling minimizes the chances of coding errors.
  9. The benefits of Code Completion in Xcode – Intelligent code completion, or just Code Completion, had been present since the early days of Xcode, and on Xcode 6 – the feature is smarter than ever before. For new programmers and iOS app developers in particular, it is only natural to feel a bit confused while porting codes from the Objective-C to the Swift platform. This is where Xcode’s Code Completion comes in handy. Developers do not have to rack their brains too much – they only need to pick the right choice from the available suggestions.
  10. Addition of Frameworks to Embedded Binaries – Even experienced mobile app developers can overlook this. At the time of adding frameworks to an app project in Xcode, coders need to include all the frameworks in ‘Embedded Binaries’ (present in the General section of the target project). Once this is done, they have to ensure that the frameworks have become present in ‘Linked Frameworks And Libraries’. Duplication problems, if any, have to be removed.
  11. Syntax to get Obj-C properties in Swift – Unlike the dual syntax options to access properties in Objective-C (dot syntax or square parentheses), Swift allows only one syntax to access the properties of an Obj-C class (dot syntax). The procedure is roughly similar to calling methods. Developers should note that unless a property in Objective-C has been declared with @property, it won’t be converted to a Swift property. Implicit Getters are, however, imported as straightforward methods in Swift.

Within the same Xcode project, both Swift files and Objective-C files can be present – highlighting the interoperability between the two languages. While making apps for the iOS 8 platform, it is easy to optimally use the resources of Cocoa Touch framework with Swift. Accessing Obj-C codes in a Swift environment (and the other way round) is fairly simple too. iOS app developers can make custom applications with no hassles whatsoever – Apple has almost made sure of that.