IOS SWIFT: the declaration is only valid in the file area

Usually in C # I used to implement extension methods in a separate class (named "ExtensionMethods") and used in the project.

Here in my first quick iphone app I need to implement some extension methods of the String class but giving me this error

enter image description here

This works great with a fast playground, but not sure how to use in a real project. really appreciate if someone can guide me with this. Thank you.

+3


source to share


1 answer


The extension must be at the root level - not inject them into a class or whatever. So just write:

import UIKit

extension String {
    var doubleValue: Double {
        ...
    }
}

extension String {
    func doubleValueT() -> Double {
        ...
    }
}

      



Note that you can also combine them into one extension:

import UIKit

extension String {
    var doubleValue: Double {
        ...
    }

    func doubleValueT() -> Double {
        ...
    }
}

      

+9


source







All Articles