Abstract lines in objective-c (iPhone)

I am working on an iphone application, and I wonder what good practice is for abstracting strings. I'm used to creating a file with constant lines and referring to them in my application (for example, URLs, port numbers or even button shortcuts). I am wondering if this is considered good practice in Obj-C, and if so, what is the best way to do this? Should I create a class with strings? Or use the ".strings" file?

ps I can localize my application later. I didn’t think about how to do this, but I think that abstracting strings is a good idea while I am developing.

Thanks! Mga

+7
source share
1 answer

Typically, you interact with the NSBundle . You use a line to read a localized version of a line (which is loaded from a file with a localized line).

There are also some macros that some people use to facilitate the syntax, prefixed with NSLocalizedString . NSLocalizedString uses an NSBundle .

imo, you should use string constants to identify the localized string you are reading (for example, you should with dictionaries and object keys).

you can declare your constants using this form (suppose objc):

 extern NSString* const MONAppString_Download; 

and define it like this:

 NSString* const MONAppString_Download = @"Download"; 

then access it using:

 NSString * tableName = nil; // << using the default NSString * localized = [[NSBundle mainBundle] localizedStringForKey:MONAppString_Download value:MONAppString_Download // << return the string using the default localization if not found table:tableName]; 

sometimes it helps to create wrapper functions to reduce noise, especially when you use them in many places:

 // @return what set to the above variable named 'localized'. NSString * MONLocalized_Download(); 

then you customize your line files, such as a map, one for each localization you support.

therefore, whenever you need to read a line that is visible to the user, you use the form above. also think that there are other localization resources (nibs, images, pdfs, etc.) that you can link to your application. most of the work here is also abstracted from NSBundle, CFBundle, if you prefer.

luck

+5
source

All Articles