20

I am new in iOS developing and in Swift language. And I try to make simple iOS application, and I need to have some string resources (for labels and text fields) in app. Of course, I can put this strings to my *.swift files as constants, but I think, it is a bad way. How can I do it? I need something as string resources in Android. I use Xcode 6 beta with swift.

TY for answers ;)

mr_ivan777
  • 401
  • 1
  • 10
  • 17
  • 3
    Did you have a look at the [Resource Programming Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html) ? There is a section about "String Resources". – Martin R Aug 11 '14 at 18:08
  • Yes, it's nice, thanks! – mr_ivan777 Aug 11 '14 at 18:17

1 Answers1

17

Your best bet is creating a file called Localizable.strings. In this file you can make localised string variables you can use throughout your application. Define strings like this;

"hello_world" = "Hello World!";

Use them in your Obj/C code like so;

NSLocalizedString(@"hello_world", nil);

Or in Swift;

NSLocalizedString("hello_world", comment: "")

p.s., I haven't tested this. Swift code might be faulty because of the fact I can't test this atm.

bmeulmeester
  • 1,117
  • 12
  • 26
  • 2
    See (for example) http://stackoverflow.com/questions/25081757/whats-nslocalizedstring-equivalent-in-swift for the Swift translation. – Martin R Aug 11 '14 at 18:21
  • Can I have more than 1 Localizable file? I'm trying to create a Text based game and I want to separate the text from the logic, would this be a viable solution? – Abdou023 Feb 04 '17 at 06:05
  • How I can use this in IB? – Manish Pathak Mar 31 '20 at 07:04