Home > Articles > Programming > General Programming/Other Languages

Effective Objective-C and Swift: Q&A with Matt Galloway

Matt Galloway, author of Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Programs, and Senior Acquisitions Editor Trina MacDonald discuss the Objective-C programming language and how Apple’s new Swift programming language will alter the landscape of future iOS application development.
Like this article? We recommend

Trina Macdonald: For those with little no experience with iOS application development, perhaps you can tell us a bit about what Apple’s Swift language is and why it’s so important.

Matt Galloway: For as long as iOS development has been around, the language that you had to use was Objective-C. Apple has given the language a lot of love over the past few years, adding features like Automatic Reference Counting, which is an excellent memory management solution. They even added huge language features like blocks and literals. This brought it in line with many other modern languages. But the reality was that Objective-C was not going to scale.

At WWDC 2014, Apple announced a brand new language called Swift, which is to eventually replace Objective-C as the language used to develop iOS and Mac applications. Swift being brand new means that it is able to take the best features from other, existing languages. For example, those developers who are familiar with functional programming will find themselves at home with Swift. Many concepts of functional programming exist in Swift. That’s not to say you have to write your application in a functional way, but Swift provides the tools to do so if you wish.

Swift is important for all iOS and Mac developers because eventually Swift will replace Objective-C as the de facto language. Apple is going to start releasing system libraries that make use of Swift features. It may mean that certain APIs are only available if you’re using Swift. Therefore if you want to stay at the cutting edge of iOS and Mac development then you’re going to need to use Swift.

Trina: Swift’s been available now for programmers to work with since June. Now that you’ve spent some time working with the language, what do you think is the biggest challenge for Objective-C developers working now with Swift?

Matt: The biggest pain point has been the fact that Swift is constantly evolving. The language has already gone through a few major changes during its short public life to date. Syntax has changed, methods renamed and features added. This has all lead to having to go back over code written previously and work out how to make it work after the changes. The changes are often trivial, but it means that you have to spend some time bringing your codebase back into line after a new Xcode version is released.

We’ve only seen one subsequent non-beta release after 1.0. This came with Xcode 6.1 when Mac OS X 10.10 Yosemite was launched. Even this had a few important changes. One such change was that originally, Swift initializers had to return something, but now they can return nil. This is an often used feature of Objective-C’s initializers which was sorely missed in the first versions of Swift. It was great to see Apple respond so quickly.

The fact that Apple is letting the language evolve so quickly is a testament to it. It will certainly be able to stand the test of time if it is allowed to be molded over the first period of public release, just like any other product would.

Trina: How much and how well do Objective-C and Swift integrate with each other?

Matt: Apple has thought very well about interoperability of Objective-C and Swift. The core system libraries are all written in Objective-C, as it would be a lot of work to port these to Swift. They make heavy use Objective-C design patterns and conventions. Apple has made interoperating with these a breeze. Sometimes the syntax can get a little verbose, but you soon get the hang of it

Mixing Objective-C and Swift in a single project is also possible and also just as easy. It’s possible to call Objective-C code from Swift, and Swift code from Objective-C. However, some language features of Swift are unavailable to the Objective-C consumer, such as Swift struct and enum types. This means that until an application is fully ported to Swift, it won’t be able to make full use of Swift’s features.

The fact that interoperability is so easy means that porting an existing codebase can happen in stages. You don’t have to go and rewrite the entire app in one go. That would be very painful for a large codebase and potentially impossible if the rate new code is being written outpaces the rate at which code can be ported. Instead, you can take portions of your app and rewrite them one by one. Where you haven’t ported code yet you can use interoperability to bridge the gap. Once portions of the app are using Swift, you can start to use the Swift features that are not part of the Objective-C interoperability in those portions.

Trina: It seems like Apple is really making a push to bring Swift to the forefront of iOS development, especially when you consider this page on the Apple site. They emphasize that anyone can build an iOS app in Swift, even those who have never coded before. Do you think this means Objective-C developers should consider switching completely to Swift sooner rather than later?

Matt: Apple has certainly done a great job of emphasizing the simplicity of Swift. They’ve also released a lot of very good content such as the iBook which guides you through the language. It draws a lot of ideas from other languages, so many developers will feel somewhat at home with it. This makes it very accessible for all developers.

That said, Swift is not the only thing you need to know to develop iOS applications. Existing Objective-C developers will be very familiar with the patterns and intricacies of the system frameworks found in Cocoa. This knowledge is still just as relevant with Swift. Therefore I think that Objective-C developers should consider themselves already somewhat proficient in Swift.

For the most part, learning Swift is learning a new syntax. Once you’ve done that, you can write code that will feel at home with today’s system libraries. Though as I said before, there will come a time when system libraries are written in Swift and make full use of Swift’s features. When this time comes it will be important to be writing your apps in Swift. We can assume this is a long way off. It would be prudent to start learning Swift now though, so that you are ready when that day comes. And what better way to learn than by porting some existing code that you know inside out.

Trina: Are there elements of Objective-C programming that currently outperform Swift?

Matt: Certain things cannot be done in Swift that could be done in Objective-C. An example is deep interaction with the runtime, which is possible in Objective-C but not in Swift. For most developers this is a non-issue as they will never need to make use of runtime features. But it does show the direction that Swift is going in. It removes a lot of the flexibility of Objective-C. This can also be seen in the fact that Swift makes it a lot harder to use reflection. It exists to a certain extent, but not in the same way that it does in Objective-C.

The fact that Swift is not as flexible does mean that it can be more performant though. Limiting the language in certain ways means that optimizations can be made by the compiler which simply cannot happen in Objective-C. For example, Swift makes use of v-table based polymorphism as well as inlining. Objective-C cannot ever do this because it’s possible to rip methods out at runtime, change them or even change the type of an object.

Interacting with C APIs is also quite tricky in Swift. The type system of Swift makes it a little complicated sometimes to pass objects to and from C APIs. The syntax can become very verbose. It’s also currently impossible to create a C function pointer to a Swift function. This can make it tricky to work with some APIs which require this.

Where Swift lacks in flexibility, it makes up for in features that don’t exist in Objective-C.

Trina: One of the benefits of writing iOS apps in Swift is that the Swift language is supposed to be more safe. In your experience with the language do you find that to be the case?

Matt: Swift is said to be “safe by default.” One way in which it is safer is that types are much more strict than in Objective-C. When you have a variable of a certain type, you can guarantee that it is of that type. Whereas in Objective-C it’s possible to get types muddled up. A good example of this is with arrays. In Objective-C, arrays can contain any object. But in Swift, you declare what type can be stored in the array. The array carries the type information along with it. When you read an object back out of the array, you know what type it’s going to be.

Another example of safety is optionals. In Objective-C, sentinel values are often used to denote error, or empty state. The sentinel value is itself a valid value for the type, such as nil for an object type. However this can be problematic. Some APIs that return an integer use 0 to indicate error, some use -1 and some use the maximum integer. You need to know which one is being used as the sentinel. Optionals solve this problem in a much better way. An optional type can either be a value, or “none”. The latter being the case that replaces the sentinel. This is far superior because there is absolutely no ambiguity.

Overall, the fact that Swift is “safe by default” does make it a pleasure to work with in my experience.

Trina: Where you think Swift will be two years from now?

Matt: Swift certainly has a long road ahead. One day it will become the de facto language for iOS and Mac development. But that is longer than two years away in my opinion. I think that within two years we will see the first Swift enhanced system libraries. They will probably still have Objective-C counterparts, or at least use the interoperability functionality.

I think that there will be a lot of open source activity in Swift within the first two years as well. This will help pave the way for the future of the language. Patterns will start to emerge and the “right way” to do things will become apparent. This is an exciting time because we all have the ability to shape the future of the language.

While I think that some apps will be fully Swift in two years, I don’t believe the majority will be. There will be a lot of apps that use Swift to some extent, but I don’t think there will be many that are fully written in Swift. Those that are will likely be new apps that don’t have to port any existing Objective-C code.

The future is exciting for all iOS and Mac developers. These first few years really are an opportunity for all of us to mold the language and rethink how we develop our apps.

Be sure to check out Matt's book, Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Programs, published by Addison-Wesley Professional.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020