Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How to create a global extension to use everywhere for a Double or Int?

Here's my problem: I am trying to create a global extension for Double (or Int) which can be used in every ViewController without repeating the code at the top of every ViewController file.

for instance:

extension Double {
    var mileToFeet: Double {return self * 5280}
}

Of course I can call it like this:

let feet = 26.mileToFeet

Obviously this works when I put the extension at the top of the ViewController and use it withing the code, but where can I place extension so that I can use it everywhere?

I tried to put it into AppDelegate, but that doesn't work? Am I on the right track there?

Thanks

like image 595
RjC Avatar asked Sep 13 '25 07:09

RjC


1 Answers

You need to make your extension public if you're defining it in another module:

public extension Double {
    var mileToFeet: Double { return self * 5280 }
}
like image 187
Mo Abdul-Hameed Avatar answered Sep 15 '25 01:09

Mo Abdul-Hameed