0

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

目前我有一个方法,我只是按照方法本身底部列出的完全初始化所有字符串。我打算在类中创建多个其他方法来使用这些相同的字符串,所以我的问题是.. 简单地将它们设为所有属性并初始化它们是否是一种很好的做法,以便它们可以在整个类中使用,如下例所示(我知道不工作的代码)。或者有没有更好的方法我应该解决这个问题,我知道它会按照我的建议工作,但作为一个新手,我想在某个时候学习正确的做事方式。提前欢呼任何帮助。

@界面

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

... .. .. ..对于每一个.. @end

@执行

-(id)init { self.lbLabel = NSLocalizedString(@"lb", @"lb"); .... 其余的以此类推...... }

@结尾

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

2 回答 2

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 个对象访问。

接下来我们将其定义为正常的NSDictionarynot NSMutableDictionary。可变意味着它可以更改,但我个人不喜欢我传递的对象能够如此容易地被操纵,所以我将非可变对象转换为可变然后再转换回不可变,这样可以确保对象中的数组只能在我来回转换它的地方进行操作。

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

于 2013-10-05T17:45:16.490 回答
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 名称而不是 self.[VARNAME] 来访问它们。要评论字典解决方案,是的,如果您想一次传递所有字符串,将它们保存在容器中将是一种更好的方法,但是如果您单独访问它们,将它们放入容器中是过分且不必要的,这更容易直接访问它们,速度更快。

于 2013-10-05T18:16:55.220 回答