0

Fairly straightforward question I hope. I have all the strings below which I am using in a class. These strings are appended to a single string along with other data to create a readable output string.

Currently I have a single method and I just initialize all the strings exactly as listed at the bottom in the method itself. I intend to make multiple other methods that will use these same strings within the class, so my question is.. Is it fine practice to simply make them all properties and initialize them so they can be used throughout the class as in below example (i know not working code). Or is there a better way I should approach this, I know it would work as I propose but as a newb I would like at some point learn the proper way to do things. Cheers in advance for any help.

@interface

@property (strong, nonatomic) NSString *lbLabel;

... .. .. ..for every single one.. @end

@implementation

-(id)init { self.lbLabel = NSLocalizedString(@"lb", @"lb"); .... and so on for all the rest...... }

@end

// English
NSString *lbLabel = NSLocalizedString(@"lb", @"lb");
NSString *mLabel = NSLocalizedString(@"m", @"m");
NSString *mphLabel = NSLocalizedString(@"mph", @"mph");
NSString *inchLabel = NSLocalizedString(@"in", @"in");


// Metric
NSString *kgLabel = NSLocalizedString(@"kg", @"kg");
NSString *kLabel = NSLocalizedString(@"k", @"k");
NSString *kphLabel = NSLocalizedString(@"kp/h", @"kp/h");
NSString *cmLabel = NSLocalizedString(@"cm", @"cm");


//Both
NSString *liftLabel = NSLocalizedString(@"Lift:", @"Lift:");
NSString *calisthenicLabel = NSLocalizedString(@"Calisthenic:", @"Calisthenic:");
NSString *cardioLabel = NSLocalizedString(@"Cardio:", @"Cardio:");
NSString *metricsLabel = NSLocalizedString(@"Metrics:", @"Metrics:");
NSString *commentLabel = NSLocalizedString(@"Comment:", @"Comment");
NSString *setLabel = NSLocalizedString(@"Set:", @"Set:");
NSString *repsLabel = NSLocalizedString(@"Reps:", @"Reps:");
NSString *weightLabel = NSLocalizedString(@"Weight:", @"Weight:");

//Cardio Labels
NSString *timeLabel = NSLocalizedString(@"Time:", @"Time:");
NSString *distanceLabel = NSLocalizedString(@"Distance:", @"Distance:");
NSString *rateLabel = NSLocalizedString(@"Rate:", @"Rate:");
NSString *settingOneLabel = NSLocalizedString(@"Setting 1:", @"Setting 1:");
NSString *settingOneValueLabel = NSLocalizedString(@"Setting 1 Value:", @"Setting 1 Value:");
NSString *settingTwoLabel = NSLocalizedString(@"Setting 2:", @"Setting 2:");
NSString *settingTwoValueLabel = NSLocalizedString(@"Setting 2 Value:", @"Setting 2 Value:");
NSString *caloriesLabel = NSLocalizedString(@"Calories:", @"Calories:");

//Metrics Labels
NSString *heightLabel = NSLocalizedString(@"Height:", @"Height:");
NSString *neckLabel = NSLocalizedString(@"Neck:", @"Neck:");
NSString *chestLabel = NSLocalizedString(@"Chest:", @"Chest:");
NSString *waistLabel = NSLocalizedString(@"Waist:", @"Waist:");
NSString *hipsLabel = NSLocalizedString(@"Hips:", @"Hips:");
NSString *bicepsLabel = NSLocalizedString(@"Biceps:", @"Biceps:");
NSString *ForearmsLabel = NSLocalizedString(@"Forearms:", @"Forearms:");
NSString *wristsLabel = NSLocalizedString(@"Wrists:", @"Wrists:");
NSString *quadsLabel = NSLocalizedString(@"Quads/Thighs:", @"Quads/Thighs:");
NSString *calvesLabel = NSLocalizedString(@"Calves:", @"Calves:");
NSString *anlklesLabel = NSLocalizedString(@"Ankles:", @"Ankles:");
NSString *feetLabel = NSLocalizedString(@"Feet:", @"Feet:");
NSString *bmiLabel = NSLocalizedString(@"BMI:", @"BMI:");
NSString *bmrLabel = NSLocalizedString(@"BMR:", @"BMR:");
NSString *waistHeightLabel = NSLocalizedString(@"Waist/Height:", @"Waist/Height:");
NSString *waistHipsLabel = NSLocalizedString(@"Waist/Hips:", @"Waist/Hips:");
// English
NSString *lbLabel = NSLocalizedString(@"lb", @"lb");
NSString *mLabel = NSLocalizedString(@"m", @"m");
NSString *mphLabel = NSLocalizedString(@"mph", @"mph");
NSString *inchLabel = NSLocalizedString(@"in", @"in");


// Metric
NSString *kgLabel = NSLocalizedString(@"kg", @"kg");
NSString *kLabel = NSLocalizedString(@"k", @"k");
NSString *kphLabel = NSLocalizedString(@"kp/h", @"kp/h");
NSString *cmLabel = NSLocalizedString(@"cm", @"cm");


//Both
NSString *liftLabel = NSLocalizedString(@"Lift:", @"Lift:");
NSString *calisthenicLabel = NSLocalizedString(@"Calisthenic:", @"Calisthenic:");
NSString *cardioLabel = NSLocalizedString(@"Cardio:", @"Cardio:");
NSString *metricsLabel = NSLocalizedString(@"Metrics:", @"Metrics:");
NSString *commentLabel = NSLocalizedString(@"Comment:", @"Comment");
NSString *setLabel = NSLocalizedString(@"Set:", @"Set:");
NSString *repsLabel = NSLocalizedString(@"Reps:", @"Reps:");
NSString *weightLabel = NSLocalizedString(@"Weight:", @"Weight:");

//Cardio Labels
NSString *timeLabel = NSLocalizedString(@"Time:", @"Time:");
NSString *distanceLabel = NSLocalizedString(@"Distance:", @"Distance:");
NSString *rateLabel = NSLocalizedString(@"Rate:", @"Rate:");
NSString *settingOneLabel = NSLocalizedString(@"Setting 1:", @"Setting 1:");
NSString *settingOneValueLabel = NSLocalizedString(@"Setting 1 Value:", @"Setting 1 Value:");
NSString *settingTwoLabel = NSLocalizedString(@"Setting 2:", @"Setting 2:");
NSString *settingTwoValueLabel = NSLocalizedString(@"Setting 2 Value:", @"Setting 2 Value:");
NSString *caloriesLabel = NSLocalizedString(@"Calories:", @"Calories:");

//Metrics Labels
NSString *heightLabel = NSLocalizedString(@"Height:", @"Height:");
NSString *neckLabel = NSLocalizedString(@"Neck:", @"Neck:");
NSString *chestLabel = NSLocalizedString(@"Chest:", @"Chest:");
NSString *waistLabel = NSLocalizedString(@"Waist:", @"Waist:");
NSString *hipsLabel = NSLocalizedString(@"Hips:", @"Hips:");
NSString *bicepsLabel = NSLocalizedString(@"Biceps:", @"Biceps:");
NSString *ForearmsLabel = NSLocalizedString(@"Forearms:", @"Forearms:");
NSString *wristsLabel = NSLocalizedString(@"Wrists:", @"Wrists:");
NSString *quadsLabel = NSLocalizedString(@"Quads/Thighs:", @"Quads/Thighs:");
NSString *calvesLabel = NSLocalizedString(@"Calves:", @"Calves:");
NSString *anlklesLabel = NSLocalizedString(@"Ankles:", @"Ankles:");
NSString *feetLabel = NSLocalizedString(@"Feet:", @"Feet:");
NSString *bmiLabel = NSLocalizedString(@"BMI:", @"BMI:");
NSString *bmrLabel = NSLocalizedString(@"BMR:", @"BMR:");
NSString *waistHeightLabel = NSLocalizedString(@"Waist/Height:", @"Waist/Height:");
NSString *waistHipsLabel = NSLocalizedString(@"Waist/Hips:", @"Waist/Hips:");
3
  • Curious why you are using this setup. Why not just use NSLocalizedString(@"timeLabel", @"time label") everywhere for example?
    – nielsbot
    Oct 6, 2013 at 3:24
  • Hi nielsbot, if your simply referring to my naming convention within the strings.. Well honestly if its wrong its probably just because I don't know any better and I'm a noob :). I've never fully localized a product before and have just been using this convention everywhere so if/when I go to localize i figure ill be in better shape. Any input or advice is always appreciated. Cheers
    – Narsil
    Oct 6, 2013 at 4:30
  • Well, I think you can just use NSLocalizedString() wherever you need a localized string. The problem becomes, how do you make sure your key string is consistent. You can just do your best, or you can use macros or constant strings. Check out stackoverflow.com/a/10196327/210171
    – nielsbot
    Oct 6, 2013 at 5:37

2 Answers 2

1

If these are not supposed to be accessed outside of the class you have no need to make them properties, as well as you shouldn't be calling self.[ANYTHING] inside your class, you should be using the _[variableName] method of accessing ivars.

So the best way to do this is to do what you have done but make them private ivars.

@interface CLASSNAME ()
{
    // English
    NSString *_lbLabel = NSLocalizedString(@"lb", @"lb");
    NSString *_mLabel = NSLocalizedString(@"m", @"m");
    NSString *_mphLabel = NSLocalizedString(@"mph", @"mph");
    NSString *_inchLabel = NSLocalizedString(@"in", @"in");
    ...
}

@end

Do the above in the .m file of your class and those variables will be accessible through the class but not externally. If you ever decide that you want them to be external you simply make them @properties and because you used the _[VARNAME] naming convention you are already setup to use them, aka no name changing because the @properties are auto-synthsized for you and you should be accessing them with their ivar name not with self.[VARNAME]. To comment on the dictionary solution, yes if you wanted to pass all the strings at once holding them in a container would be a better way to go but if you're accessing them individually putting them inside a container is overkill and unnecessary, it's easier to just access them directly and it's faster.

1
  • 1
    Thank you very much for this and elaborating on where and why I should place them as you stated this is very helpful as a new guy, will probably help me to clean up some of my other code as well where I have properties that shouldn't be properties and so on. Thanks again!
    – Narsil
    Oct 5, 2013 at 18:23
1

The only way i could think of making this simplier, is to use a dictionary and instead of doing

NSString *liftLabel = NSLocalizedString(@"Lift:", @"Lift:");

you do this

NSDictionary* dictionaryOfStrings = [[NSDictionary alloc] init];

then in the method file do

NSMutableDictionary* mutableDictionary = [Dictionary dictionaryWithDictionary:self.dictionaryOfStrings];
[mutableDictionary setObject:NSLocalizedString(@"Lift:", @"Lift:") forKey:@"lift"];
self.dictionaryOfStrings = [NSDictionary dictionaryWithDictionary:mutablDictionary];

the reasoning for this is that, all of the string data will be stored with in the dictionary and not have 20 different strings declarations so this simplifies your header file, and all the properties are accessed from 1 object the dictionary.

Next we define it as a a normal NSDictionary not NSMutableDictionary. Mutable means that it can be changed, but i personally don't like the objects i pass around to be able to be manipulated so easily, so i convert nonMutable obejcts to mutable and then back to non mutable, this ensures that the objects in the array can only be manipulated where i convert it back and forth.

This way is a preference of mine and is no more the right way than another design practice, but i prefer this method over having that many string variables.

1
  • Thanks for this, with your and apperley's input below I think should be good to go! Cheers
    – Narsil
    Oct 5, 2013 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.