skip to content

When a team picks Swift instead of Objective-C for a new iOS module, which language differences matter most, and what does mixing the two cost?

level: middleimportance: should knowfreq 55%

answer

  1. nil as a type, not a convention
  2. structs and enums as real values
  3. generics that are part of the type
  4. direct calls versus message sends
  5. what the generated header can show

basics

~20 s

Swift makes absence a type (Optional), gives String, Array and Dictionary value semantics, has full generics, calls struct methods directly and reports errors with throws. Objective-C sees only Swift declarations representable in Objective-C, so Swift-only types stay hidden from it.

solid answer

~50 s

In Objective-C any object pointer may be `nil`, and a message to `nil` quietly returns `nil` or zero; in Swift only an `Optional` can be absent, and the compiler makes you handle it. Swift's `String`, `Array` and `Dictionary` are structs with value semantics, and enums can carry associated values. Swift generics are part of the type and work for any type, while Objective-C's lightweight generics are annotations the run time never checks. Swift calls methods on structs, enums and `final` classes directly instead of through `objc_msgSend`, and reports errors with `throws` instead of an `NSError **` out-parameter. The cost of mixing: Objective-C sees Swift only through the generated `-Swift.h` header, and only declarations it can represent, such as `@objc` members of `NSObject` subclasses. A method taking a struct, an enum with associated values or a generic class stays out of reach.

code

swift · 16 lines
swift
import Foundation

struct Money {
    var cents: Int
}

enum CheckoutState {
    case idle
    case failed(reason: String)
}

final class CheckoutController: NSObject {
    @objc func apply(_ money: Money) {}
    @objc func show(_ state: CheckoutState) {}
    @objc func total(cents: Int) -> String { "\(cents)" }
}

go deeper

for a junior

Recall the headline differences: Optional instead of silent nil, structs for String and Array, throws instead of NSError out-parameters.

for a middle

Explain why each difference matters at compile time, and what the generated -Swift.h header can and cannot expose to Objective-C callers.

for a senior

Show you have worked across the boundary: nullability annotations, implicitly unwrapped imports, and keeping a Swift module's @objc surface small and deliberate.

for a principal

Frame the boundary as an interface cost: every type Objective-C must call shapes the Swift API toward classes, so decide early which modules face Objective-C at all.

## Why the comparison still comes up Apple introduced Swift in 2014 as the language for its platforms after decades of Objective-C, and many iOS codebases still contain both. The interview question is rarely "which is better"; it is "what does Swift change in the code you write, and what does keeping both languages in one app cost". The Swift book frames the first half: Swift "defines away large classes of common programming errors" while compiling to optimized native code. ## The differences that change your code | Concern | Objective-C | Swift | |---|---|---| | Absence | any object pointer can be `nil`; a message to `nil` returns `nil` or zero | only an `Optional` can be absent; the compiler requires unwrapping | | Core types | `NSString`, `NSArray` and `NSDictionary` are classes shared by reference, with mutable subclasses | `String`, `Array` and `Dictionary` are structs with value semantics | | Enums | C enumerations of integer constants | enums with methods, raw values and associated values, switched exhaustively | | Generics | lightweight generics such as `NSArray<NSString *> *`, not checked at run time | generic types and functions with constraints, for any type including structs | | Method calls | method calls are message sends resolved at run time through `objc_msgSend` | direct calls for structs, enums and `final` members; dynamic dispatch where overriding or protocols require it | | Errors | `NSError **` out-parameters | `throws`, `try` and `do`-`catch` | | Code layout | class-name prefixes, separate `.h` and `.m` files | modules as namespaces, no header files | A few of these deserve a sentence more: - **Absence.** The Objective-C behaviour is not a crash; it is silence. A `nil` object flows on as zero or `nil` until something far away misbehaves. Swift moves absence into the type: `String` and `String?` are different types, so the compiler can tell you where a missing value must be handled. - **Value types.** A Swift `Array` assigned to a second variable is an independent value; changing one does not change the other. Two `NSMutableArray` pointers assigned from one another share one object. - **Generics.** Objective-C's `<NSString *>` parameter guides the compiler and the Swift importer, but the array itself stores any object: an `NSNumber` added through an `id` variable sits in an `NSMutableArray<NSString *>` without complaint. A Swift `[String]` cannot hold anything else. - **Dispatch.** Direct calls let the optimizer inline; the mechanics belong to the lesson on method call mechanisms. - **Errors.** An Objective-C method signals failure by returning `NO` or `nil` and filling an `NSError **` argument, and nothing forces the caller to look. A Swift `throws` function cannot be called without `try`, so the compiler marks every place an error can leave. - **Memory management is not the headline difference.** Modern Objective-C also uses Automatic Reference Counting, so retain cycles and `weak` references work alike in both languages. ## What mixing the two costs 1. **Swift calling Objective-C** goes through a bridging header (in an app target) or a module import. Nullability annotations decide the imported type: a pointer marked `nullable` imports as `String?`, while an unannotated one imports as an implicitly unwrapped `String!` that traps if Objective-C returns `nil`. 2. **Objective-C calling Swift** goes only through the generated `ModuleName-Swift.h` header, and only for declarations Objective-C can represent, such as classes that inherit from `NSObject` with their `@objc` members, `@objc` protocols and `@objc` enums with integer raw values. 3. Everything Swift-only stays invisible on that side: a method taking a struct cannot be marked `@objc` at all, and a generic class is left out of the generated header. 4. So the boundary pulls Swift code toward classes and `@objc` APIs. The interface a Swift module offers Objective-C callers is a deliberately designed subset, and the rest of the module can use structs, enums and generics freely. The bridging rules themselves, such as toll-free bridged types and how Objective-C generics import, are covered in the lesson on Objective-C and C bridging. ## How to frame the choice in an interview - Swift's gains are **compile-time**: absence, types and errors that Objective-C leaves to convention are checked by the compiler. - The cost is the **boundary**: each type Objective-C must see has to be representable there, which in practice means an `NSObject` subclass, an `@objc` protocol or an `@objc` integer enum. - A new module that only Swift code calls pays none of that cost; a module Objective-C code must call designs its `@objc` surface on purpose.

  • Objective-C has ARC too, so is memory management a reason to choose Swift?
    Not really. Modern Objective-C and Swift both use Automatic Reference Counting for class instances, so retain cycles, `weak` and `strong` work alike. What differs is how much of the data is objects at all: Swift's `String`, `Array` and `Dictionary` are structs, so a copy is an independent value instead of a second reference to one shared object.
  • What type does Swift see for an Objective-C method returning `NSString *` with no nullability annotation?
    An implicitly unwrapped optional, `String!`. Swift lets the result be used as a `String`, but if Objective-C really returns `nil`, that use traps. Annotating the header with `nullable` or `nonnull`, or wrapping it in `NS_ASSUME_NONNULL_BEGIN` and `NS_ASSUME_NONNULL_END`, gives Swift a real `String?` or `String`.

  • Swift's main advantage is automatic memory management, which Objective-C lacks.
  • Sending a message to nil in Objective-C crashes, like force-unwrapping nil in Swift.
  • Objective-C can call any Swift type once the module is imported.
  • Swift's Array is just NSArray under a different name.
  • Every Swift method call goes through objc_msgSend.