在执行方法之前,更容易检查每个bool?(Easier way to check for each bool before executing a method?)

是否有更简单,更少的复制/粘贴方式来实现与下面的代码相同的东西? 什么是最好的方法?

if (self.title.selectedSegmentIndex !=-1) { [self.form setValue:[self.title titleForSegmentAtIndex:self.title.selectedSegmentIndex] forKey:@"titleKey"]; } if (self.author.selectedSegmentIndex !=-1) { [self.form setValue:[self.author titleForSegmentAtIndex:self.author.selectedSegmentIndex] forKey:@"authorKey"]; } if (self.description.selectedSegmentIndex !=-1) { [self.form setValue:[self.description titleForSegmentAtIndex:self.description.selectedSegmentIndex] forKey:@"descriptionKey"]; } etc....

编辑 :抱歉它不是bool if语句(我相信我需要这个检查,否则当尝试从索引中获取标题时没有选择任何段时它会崩溃)

我觉得我对这些if语句进行了大量的重复检查,并且不知道更好的方法,任何建议都表示赞赏。

Is there an easier, less copy/paste way of achieving the same thing as the code below? What is the best way?

if (self.title.selectedSegmentIndex !=-1) { [self.form setValue:[self.title titleForSegmentAtIndex:self.title.selectedSegmentIndex] forKey:@"titleKey"]; } if (self.author.selectedSegmentIndex !=-1) { [self.form setValue:[self.author titleForSegmentAtIndex:self.author.selectedSegmentIndex] forKey:@"authorKey"]; } if (self.description.selectedSegmentIndex !=-1) { [self.form setValue:[self.description titleForSegmentAtIndex:self.description.selectedSegmentIndex] forKey:@"descriptionKey"]; } etc....

Edit: sorry its not a bool if statement (I believe I need this check though, otherwise it will crash when no segment is selected when trying to get the title from the index)

I feel like I have a lot of repetitive checking with these if statements and dont know a better way, any suggestions are appreciated.

最满意答案

这种事情有几种方法。 (这里的代码都没有经过测试甚至编译过。)

最明显的是一种简单的方法:

- (void)setFormValueFromSegmentedControl:(UISegmentedControl *)seg forKey:(NSString *)key { if (seg.selectedSegmentIndex !=-1) { [self.form setValue:[seg titleForSegmentAtIndex:seg.selectedSegmentIndex] forKey:key]; } }

现在您的代码简化为:

[self setFormValueFromSegmentedControl:self.title forKey:@"titleKey"]; [self setFormValueFromSegmentedControl:self.author forKey:@"authorKey"]; ...

就我个人而言,我喜欢这个,因为它是如此简单明了,用最少的魔法来阅读。 但是,即使那个街区变得笨拙,也有其他解决方案。

你可以做像@Cyrille和@ H2CO3这样的建议:

for (NSString *key in [@"title", @"author", ...]) { UISegmentedControl *seg = [self valueForKey:key]; if (seg.selectedSegmentIndex != -1) { NSString *formKey = [key stringByAppendingString:@"Key"]; [self.form setValue:[seg titleForSegmentAtIndex:seg.selectedSegmentIndex] forKey:formKey]; } }

您还可以使用控件上的tag来指示它应用于哪个表单键(您可以在IB中配置标记或在代码中使用setTag: 。 所以title的标签为0, author的标签为1,等等。

NSArray *formKeyForTag = @[@"titleKey", @"authorKey", ...]; for (UISegmentedControl *seg = [... IBOutletCollection of controls ...]) { if (seg.selectedSegmentIndex != -1) { NSString *formKey = formKeyForTag[seg.tag]; [self.form setValue:[seg titleForSegmentAtIndex:seg.selectedSegmentIndex] forKey:formKey]; }

或者,您可以将相关对象添加到分段控件,指向表单键。 请参阅什么是objc_setAssociatedObject()以及在什么情况下应该使用它? 有关更多信息。 (我碰巧喜欢相关物品......)

或者您可以使用NSDictionary来保留映射。

所以有很多选择。 但我有点像顶部的简单方法。 这是最明显的。

There are several approaches to this kind of thing. (None of the code here has been tested or even compiled.)

The most obvious is a simple method:

- (void)setFormValueFromSegmentedControl:(UISegmentedControl *)seg forKey:(NSString *)key { if (seg.selectedSegmentIndex !=-1) { [self.form setValue:[seg titleForSegmentAtIndex:seg.selectedSegmentIndex] forKey:key]; } }

Now your code simplifies to:

[self setFormValueFromSegmentedControl:self.title forKey:@"titleKey"]; [self setFormValueFromSegmentedControl:self.author forKey:@"authorKey"]; ...

Personally, I like this because it's so simple and obvious to read with a minimum of magic. But there are other solutions if even that block got unwieldy.

You can do something like @Cyrille and @H2CO3 are suggesting:

for (NSString *key in [@"title", @"author", ...]) { UISegmentedControl *seg = [self valueForKey:key]; if (seg.selectedSegmentIndex != -1) { NSString *formKey = [key stringByAppendingString:@"Key"]; [self.form setValue:[seg titleForSegmentAtIndex:seg.selectedSegmentIndex] forKey:formKey]; } }

You could also use a tag on the control to indicate which form key it applies to (you can configure tags in IB or using setTag: in code). So title would have a tag of 0, author would have a tag of 1, etc.

NSArray *formKeyForTag = @[@"titleKey", @"authorKey", ...]; for (UISegmentedControl *seg = [... IBOutletCollection of controls ...]) { if (seg.selectedSegmentIndex != -1) { NSString *formKey = formKeyForTag[seg.tag]; [self.form setValue:[seg titleForSegmentAtIndex:seg.selectedSegmentIndex] forKey:formKey]; }

Or you could add an associated object to the segmented control, pointing to the form key. See What is objc_setAssociatedObject() and in what cases should it be used? for more on that. (I happen to love associated objects…)

Or you could use an NSDictionary to keep the mappings.

So lots of options. But I kind of like the simple method at the top. It's the most obvious.

更多推荐