iOS Class,实现多个字符串的正确方法?

时间:2013-10-05 17:32:35

标签: ios objective-c string class properties

我希望这是一个相当简单的问题。我在课堂上使用下面的所有字符串。这些字符串与其他数据一起附加到单个字符串以创建可读输出字符串。

目前我有一个方法,我只是初始化方法本身底部列出的所有字符串。我打算制作多个其他方法,在类中使用这些相同的字符串,所以我的问题是..简单地将它们作为所有属性并初始化它们以便它们可以在整个类中使用,如下例所示(i)知道没有工作代码)。或者有更好的方法我应该这样做,我知道它会像我提议的那样工作,但作为一个新手,我想在某些时候学习正确的做事方式。提前为您提供帮助。

@interface

@property(强,非原子)NSString * lbLabel;

... .. .. ..每一个.. @end

@implementation

- (ID)INIT {     self.lbLabel = NSLocalizedString(@“lb”,@“lb”);     ....等等所有其他...... }

@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:");

2 个答案:

答案 0 :(得分:1)

我能想到使这个更简单的唯一方法是使用字典而不是做

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

你这样做

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

然后在方法文件中执行

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

这样做的原因是,所有的字符串数据都将存储在字典中,并且没有20个不同的字符串声明,因此这简化了您的头文件,并且所有属性都是从1个字符对象中访问的。

接下来,我们将其定义为普通NSDictionary而不是NSMutableDictionary。 Mutable意味着它可以被改变,但我个人不喜欢我传递的对象能够如此轻松地操作,所以我将nonMutable obejcts转换为mutable然后返回到non mutable,这确保了对象中的数组只能在我来回转换的地方进行操作。

这种方式是我的偏好,并且不再是另一种设计实践的正确方法,但我更喜欢这种方法而不是那么多的字符串变量。

答案 1 :(得分:1)

如果这些不应该在课外访问,你不需要创建它们的属性,也不应该调用self。[ANYTHING]在你的类中,你应该使用_ [variableName]访问ivars的方法。

所以这样做的最好方法就是做你做过的事,但要把它们变成私人的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

在类的.m文件中执行上述操作,这些变量可以通过类访问,但不能在外部访问。如果您决定要将它们设置为外部,则只需将它们设为@properties并且因为您使用了_ [VARNAME]命名约定,您已经设置使用它们,也就是说没有更改名称,因为@properties是自动合成的并且您应该使用他们的ivar名称访问它们而不是自己。[VARNAME]。要评论字典解决方案,是的,如果你想一次性传递所有字符串将它们放在一个容器中将是一个更好的方法,但是如果你单独访问它们将它们放在容器内是过度的和不必要的,它会更容易直接访问它们并且速度更快。