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 ;)
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.
Recent versions of XCode doesn't create Localizable. strings file by default. To add Localizable. strings file, go to File->New->File , choose Strings File under Resource tab of iOS, name it Localizable.
NSLocalizedString is a Foundation macro that returns a localized version of a string. It has two arguments: key , which uniquely identifies the string to be localized, and comment , a string that is used to provide sufficient context for accurate translation.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With