Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 问:如何尽可能多地重构具有属性的块代码?_Ios_Objective C - Fatal编程技术网

Ios 问:如何尽可能多地重构具有属性的块代码?

Ios 问:如何尽可能多地重构具有属性的块代码?,ios,objective-c,Ios,Objective C,我希望将以下具有属性访问权限的重复代码尽可能压缩到for循环或使用函数中: if (sender == self.section1SegmentedControl) { switch (sender.selectedSegmentIndex) { case 0: //YES self.DSProtokoll.section1YesNo = TRUE; break;

我希望将以下具有属性访问权限的重复代码尽可能压缩到for循环或使用函数中:

    if (sender == self.section1SegmentedControl) {
        switch (sender.selectedSegmentIndex) {
            case 0: //YES
                self.DSProtokoll.section1YesNo = TRUE;
                break;
            case 1: //NO
                self.DSProtokoll.section1YesNo = FALSE;
                break;
        }
    }
    if (sender == self.section2SegmentedControl) {
        switch (sender.selectedSegmentIndex) {
            case 0: //YES
                self.DSProtokoll.section2YesNo = TRUE;
                break;
            case 1: //NO
                self.DSProtokoll.section2YesNo = FALSE;
                break;
        }
    }
    if (sender == self.section3SegmentedControl) {
        switch (sender.selectedSegmentIndex) {
            case 0: //YES
                self.DSProtokoll.section3YesNo = TRUE;
                break;
            case 1: //NO
                self.DSProtokoll.section3YesNo = FALSE;
                break;
        }
    }
我试着把它放在一个函数中:

- (void)processYesNoIfMatch:(UISegmentedControl *)sender source:(UISegmentedControl *) a dest:(BOOL *) b {
    if (sender == a) {
        switch (sender.selectedSegmentIndex) {
            case 0: //YES
                *b = TRUE;
                break;
            case 1: //NO
                *b = FALSE;
                break;
        }
    }
}
但我不能使用该函数并使用不同的参数多次调用它:

[self processYesNoIfMatch:sender source:self.section1SegmentedControl dest:&self.DSProtokoll.section1YesNo];
[self processYesNoIfMatch:sender source:self.section2SegmentedControl dest:&self.DSProtokoll.section2YesNo];
因为不允许使用指向属性的指针

请求的属性表达式的地址


如何在objective-c中尽可能多地使用函数或for循环来简化这个重复的原始代码?

objective-c方法调用使用动态绑定,如果您愿意在编译时检查可能的运行时错误,您可以大大缩短代码。将以下内容视为伪代码(因为它已直接输入到答案中,并且只是一段代码):


for(int-controlNumber=1;controlNumberObjective-C方法调用使用动态绑定,如果您愿意在编译时检查可能的运行时错误,则可以大大缩短代码。将以下代码视为伪代码(因为它已直接键入答案中,并且只是一段代码):


for(int controlNumber=1;controlNumber)由于此问题(错误,IMO)已作为“基于意见”关闭,因此
switch
语句可以替换为
self.DSProtokoll.section1YesNo=(sender.selectedSegmentIndex==0);
这里有一个合理的重构,假设
self.DSProtokoll
属于
DSProtokoll
类型:
BOOL setting=sender.selectedSegmentIndex==0;DSProtokoll*p=self.DSProtokoll;if(sender==self.section1SegmentedControl){p.section1YesNo=setting;}else if(sender==self.section2SegmentedControl){p.section2YesNo=setting;}否则如果(sender==self.section3SegmentedControl){p.section3YesNo=setting;}
switch
语句可以替换为
self.DSProtokoll.section1YesNo=(sender.selectedSegmentIndex==0);
,因为这个问题已经(不正确,IMO)被关闭为“基于意见”,我只能在评论中作出回应。这里是一个合理的重构,假设
self.DSProtokoll
属于
DSProtokoll
BOOL设置=sender.selectedSegmentIndex==0;DSProtokoll*p=self.DSProtokoll;if(sender==self.section1SegmentedControl){p.section1YesNo=setting;}else if(发送方==self.section2SegmentedControl){p.section2YesNo=setting;}else if(发送方==self.section3SegmentedControl){p.section3YesNo=setting;}
for(int controlNumber = 1; controlNumber <= MAX_CONTROL_NUMBER; controlNumber++)
{
   // construct the property name as a *string*
   NSString *controlProperty = [NSString stringWithFormat:@"section%dSegmentedControl", controlNumber);
   // get the property value - if the property does not exist this is a runtime error
   NSSegmentedControl *controlN = [self valueForKey:controlProperty];
   if (sender == controlN)
   {
      // construct the property path as a *string*
      NSString *yesNoProperty = [NSString stringWithFormat:@"DSProtokoll.section%dYesNo", controlNumber;
      // set the property value, must pass the BOOL as an NSNumber object
      [self setValue:@(sender.selectedSegmentIndex == 0) forKeyPath:yesNoProperty];
      // all done, exit loop
      break;
   }
}