Gizmodo
Gizmodo

Reputation: 3222

iOS: Swift Constant File

Which is more efficient during runtime?

Constants.swift:

let CONSTANT1 = 2
let CONSTANT2 = 0.034
let CONSTANT_STRING = "String"
let CONSTANT_COLOR = UIColor.red

or

Constants.swift

struct Constants {
   static let CONSTANT1 = 2
   static let CONSTANT2 = 0.034
   static let CONSTANT_STRING = "String"
   static let CONSTANT_COLOR = UIColor.red
}

or is there a difference at all during runtime?

Upvotes: 1

Views: 157

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

Compiler optimization is sophisticated enough that it's extremely unlikely there will be any performance difference. The compiler can tell that they're all constants, so the compiled code will likely be identical or nearly so. You could try both ways and attempt to profile the difference (if any). It would almost certainly be a waste of time unless you're very interested in compilers.

Upvotes: 1

Related Questions