swift extension in separate file

I want to extend the String class and have that extension availble to entire project. I create a .swift file "String+Extensions.swift"


import Swift
public extension String {

    func LogToConsole(){
        print(self)
    }

}


I then one to use this extension in a viewController or class. but I cant not as it does not exist.


let x: String+Extensions


The extension is only accesbile if accessed in the same file as where it is defined. Meaning I either have to repeat the extension declaration in each file. I though want it accessed across the entire project.


Is this possible? Makes sense that it would be.

Accepted Answer

just write let x: String

wow so simple. I thought I woud have had to create an instance of the extension. Many thanks

Take care, the file name is not a class name; in addition, you do not define any class here, just extend String.


Wish you success in your learning.

The thing to remember about extensions is that you are extending the type, not an instance. By creating the extension, all strings everywhere in your code base are extended.

Should add: though access to the methods & properties of the extension are still controlled by the declared access controls.

swift extension in separate file
 
 
Q