2

I am using Xcode 10.3 and Swift 5

I have a long struct with my Localizable Strings as follows:

import Foundation
struct Strings {
    static let string1 = NSLocalizedString("String 1", comment:"")
    ....
}

I wanted to get rid of all the repetitive text (, comment:"" at the end of each struct item) so I wrote a one line extension as follows, in the same file, but I get the 'Use of undeclared type' error in the extension

import Foundation
extension NSLocalizedString {  // 'Use of undeclared type'
}     
struct Strings {
    static let string1 = NSLocalizedString("String 1", comment:"")
    ...
}

I tried to do the same with a String extension and a new class LString, and creating a class method that returns the NSLocalizedString object, with the same result

Why is NSLocalizedString recognized inside the struct but not in the extension?. (They are in the same file)

Am I missing something?

eharo2
  • 2,553
  • 1
  • 29
  • 39
  • 3
    `NSLocalizedString` is a function, not a type – dan Oct 14 '19 at 17:58
  • Thx @dan for the clarification.... Basic stuff... I will review my approach... thx – eharo2 Oct 14 '19 at 19:54
  • 2
    Don't use `static` with NSLocalizedString. If you switch languages while the app is open, the string will not update due to the static declaration. – Adrian Oct 14 '19 at 20:01
  • @dan. If you put this in an answer I will be happy to select it as the valid one – eharo2 Oct 15 '19 at 16:59

0 Answers0