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

Objective-C Swift

17 Sep 2023 . 3 min read       @gurjitpt


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


Generic placeholder image

WRITTEN BY

Gurjit Singh

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


Next Posts

How to use enum in Swift Empty space for void

Enumerations (enum) enables you to write code in a type safe way. Enum is very useful while defining common type of values. You don't have to provide...

Mar 7, 2023 . 2 min read     Swift Enum

Read More »

Optional unwrapping syntax in Swift 5.7 Empty space for void

Optional unwrapping is one of the most used patterns in iOS development. Swift 5.7 introduces new features included a new way to unwrap optional values...

Jun 14, 2022 . 2 min read     Optional unwrapping

Read More »

Property observers in Swift Empty space for void

It's a common pattern or technique in various programming languages when we want to perform some action when a value is changed. Property ...

May 24, 2021 . 2 min read     Property Observers

Read More »