如何在一行上对多个变量使用方法

发布于 2025-01-02 00:59:56 字数 165 浏览 2 评论 0原文

我该如何做到这一点:

[wage setText:@""];
[hours setText:@""];
[grossPay setText:@""];
[taxes setText:@""];
[netPay setText:@""];

在一行中?

How can i do this:

[wage setText:@""];
[hours setText:@""];
[grossPay setText:@""];
[taxes setText:@""];
[netPay setText:@""];

in one line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

硬不硬你别怂 2025-01-09 00:59:56

不要在一行中完成。这样做要容易理解。引用布赖恩·科尼汉的话:

每个人都知道调试比最初编写程序困难两倍。因此,如果您在编写它时尽可能聪明,那么您将如何调试它?

Don't do it in one line. Do it so that it's easy to understand. To quote Brian Kernighan:

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?

又爬满兰若 2025-01-09 00:59:56

如果所有这些都符合 KVC 并且您想要输入相同的值,则可以将所有这些放入一个数组中,然后设置文本。

[[NSArray arrayWithObjects:wage, hours, grossPay, taxes, netPay, nil] setValue:@"" forKey:@"text"];

或者hoshi的想法也不错。 (同样需要 KVC 合规性)

非 KVC 解决方案:

[[NSArray arrayWithObjects:wage, hours, grossPay, taxes, netPay, nil] makeObjectsPerformSelector:@selector(setText:) withObject:@""];

If all of these are KVC compliant and you want to put the same value, you could put all of these in an array then set the text.

[[NSArray arrayWithObjects:wage, hours, grossPay, taxes, netPay, nil] setValue:@"" forKey:@"text"];

Or hoshi's idea is good too. (again it requires KVC compliance)

A non KVC solution :

[[NSArray arrayWithObjects:wage, hours, grossPay, taxes, netPay, nil] makeObjectsPerformSelector:@selector(setText:) withObject:@""];
尬尬 2025-01-09 00:59:56

使用模型类,如:

@interface DbInfos : NSObject{
    NSString *wage;
    NSString *hours;
    NSString *grossPay;
    NSString *taxes;
    NSString *netPay;
}

@property(nonatomic,strong)NSString *wage;
@property(nonatomic,strong)NSString *hours;
@property(nonatomic,strong)NSString *grossPay;
@property(nonatomic,strong)NSString *taxes;
@property(nonatomic,strong)NSString *netPay;

-(id)initWithWage:(NSString *)newWage Hours:(NSString *)newHours GrossPay:(NSString *)newTaxes NetPay:(NSString *)newNetPay;

Use a model class,as:

@interface DbInfos : NSObject{
    NSString *wage;
    NSString *hours;
    NSString *grossPay;
    NSString *taxes;
    NSString *netPay;
}

@property(nonatomic,strong)NSString *wage;
@property(nonatomic,strong)NSString *hours;
@property(nonatomic,strong)NSString *grossPay;
@property(nonatomic,strong)NSString *taxes;
@property(nonatomic,strong)NSString *netPay;

-(id)initWithWage:(NSString *)newWage Hours:(NSString *)newHours GrossPay:(NSString *)newTaxes NetPay:(NSString *)newNetPay;
笨笨の傻瓜 2025-01-09 00:59:56

同意 Rob (+1) 的观点,但回答是为了有趣

for (UILabel * at in [NSArray arrayWithObjects:wage, hours, grossPay, taxes, netPay, nil]) at.text = @"";

请注意,如果工资小时,这将会失败grossPay,或nil

agreeing with Rob (+1) on this one, but answering for fun:

for (UILabel * at in [NSArray arrayWithObjects:wage, hours, grossPay, taxes, netPay, nil]) at.text = @"";

note that this would fail if wage, hours, grossPay, or taxes were nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文