Use of constant (#define) in Swift Language – iOS

Swift Constant Test
Swift Constant Test

In Objective-C we are using a header file to create constant variables like as

// Objective-C
#define APP_ALERT_TITLE "Objective-C Constant"

Swift has new syntax to define the constant (#define)

// Swift
let APP_ALERT_TITLE = "Swift Constants"

Lets try

Create a swift file with the constants

import Foundation

class Constants {
    
    // MARK: List of Constants
    
    static let APP_ALERT_TITLE = "Swift Constants"
    static let SAMPLE_MESSAGE = "The alert is working !!"
}

Note : Here the MARK statement is also changed.

// Objective-C
#pragma mark -
#pragma mark List of Constants
// Swift
// MARK: List of Constants

Continue reading