The beginner's guide to Objective-C for Swift developers

Objective-C Swift

Posted on 17 Sep 2023 . 3 min read


The Swift programming language was created in 2014 with a particular focus on iOS and macOS development. It is an open source language with a simple syntax. On other hand, Objective-C was founded in 1984 and was influence from C language and small talk.


Swift soon replaced Objective C, but there is still a sizable Objective C code base. Therefore, it's essential to have a foundational understanding of Objective C as an iOS developer.


This article serves as a quick reference on Objective C for Swift programmers.


#Constants and Variables

Swift- You can declare constants using let keyword and variables using var keyword in Swift. For example,

let name = "John" //declare constant
var loginAttempt: Int //declare variable
loginAttempt = 1 //assign number to variable


Objective-C- There are several types of constants in Objective-C but commonly used constants are numeric, string and bool. For example,

const int MAX_ATTEMPT = 5; //numeric constant
NSString const *name = @"John"; //string constant
const BOOL IS_EMAIL_VALID = YES; //bool constant

NSNumber *loginAttempt; //declare variable
loginAttempt = 1; //assign number to variable


#Properties

Swift- In Swift, classes and structs can define there own constants and variables and its called properties. These properties can be either stored or computed.

struct Employee {
 let name: String //declare constant stored property
 var salary: Int //declare variable stored property
 lazy var login = Login() // declare lazy property
}


For computed property, it can be either get or set to make action happens. For example, we add bonus property which automatically returns salary with bonus.

struct Employee {
 let name: String //declare constant stored property
 var salary: Int //declare variable stored property
 lazy var login = Login() // declare lazy property
 //read only property
 var bonus: Int {
  get {
    return salary * 2000
  }
 }
}


Objective-C- In Objective-C property declaration starts with @property keyword.

@property NSString *name; // declare simple property
@property (readonly) NSString *lastName; //declare readonly property
@property(readwrite) NSInteger salary; //declare read write property


#Methods

Swift- Methods are functions which are associates with particular type. In Swift, we define methods as follow:

struct Vehicle {
  //declare method without parameter and return type
  func registration()

  //declare method with paramter but no return type
  func topSpeed(gear: Int)

  //declare method with two parameters and return type
  func typeOfVehicle(tyres: Int, number: String) -> [String]
  
  //declare method with default parameter
  func topSpeed(_ speed: Int = 30)
  //method implementation
  func typeOfVehicle(tyres: Int, number: String) -> [String] {
    return []
  }
}

//calling method
let vehicle = Vehicle()
var type = vehicle.typeOfVehicle(tyres: 4, number: "90 BV 21")


Objective C- In Objective C, we can define methods as follow:

//declare method without parameters and return type
-(void)registration;
//declare method with parameter but no return type
-(void)topSpeed:(NSNumber *)gear; 
//declare method with two parameters and return type
-(NSArray *)typeOfVehicle: (NSNumber *)tyres (NSString *)number; 
//method implementation
-(NSArray *)typeOfVehicle: (NSNumber *)tyres (NSString *)number {
  return[];
}

//calling method
NSArray *result = [typeOfVehicle:@4 category:@"90 BV 21"];


#Enumeration

Swift- You can define enum with the enum keyword. The values inside enum are enumeration cases.

enum Direction {
  case north
  case south
  case east
  case west
}

let whereToGo = Direction.north


Objective C-Enums in Objective C is defined by following syntax:

typedef NS_ENUM(NSInteger, Direction) {
 North,
 South,
 East, 
 West
};


The bottom line

As described earlier, this article is only a beginners guide for Swift developers who wants to learn Objective-C. Besides basics there are lot of topics to explore in Objective-C like protocols, extensions, generics etc.


Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.

Thanks!


Share this article



Written By

Generic placeholder image

Gurjit Singh

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me for more updates:


Discover articles by topics

SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView

Related Articles


String Concatenation in Swift: A Comprehensive Guide

In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......

2024-10-17 . 3 min read     String Concatenation

Read More »

Integrating SwiftUI with UIKit Using UIHostingController

With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....

2024-07-09 . 3 min read     UIHostingController

Read More »

Deep Dive into Autorelease Pools in Swift

In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...

2024-01-28 . 4 min read     Swift Autorelease

Read More »

Swift enum equatable: with or without associated values

Swift enums provide a powerful way to model a set of related values. Enums can be equipped with associated values, allowing them to represen...

2024-01-24 . 3 min read     Swift Enums

Read More »

How to create Date Picker in SwiftUI

Use a DatePicker when creating a view that enables users to choose both a calendar date and, if needed, a specific time.In SwiftUI, you can ...

2024-01-16 . 2 min read     SwiftUI DatePicker

Read More »

Getting started with Swiftlint to enforce Swift style

SwiftLint is a tool that ensures Swift code adheres to defined style guidelines. It automates code review by identifying and suggesting impr...

2023-12-29 . 4 min read     Swift SwiftLint

Read More »