gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:25:12 25 4
gpt4 key购买 nike

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

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

@界面

@property (strong, nonatomic) NSString *lbLabel;

.........对于每一个..@结束

@实现

-(id)初始化{ 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:");

最佳答案

如果这些不应该在类之外访问,则无需为它们设置属性,也不应调用 self.[ANYTHING] 在类中,您应该使用 _[variableName ] 访问ivars的方法。

所以最好的方法是做你已经做过的事情,但将它们设为私有(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

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

关于iOS 类,实现多个字符串的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19200673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com