Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.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
Objective c 在if条件下初始化对象_Objective C_Ios - Fatal编程技术网

Objective c 在if条件下初始化对象

Objective c 在if条件下初始化对象,objective-c,ios,Objective C,Ios,我想创建一个对象,但类型取决于if条件的结果: if ([type isEqualToString:@"day"]) { GraphDayView *graphv = [[GraphDayView alloc] initWithFrame:rect]; } else { GraphMonthView *graphv = [[GraphMonthView alloc] initWithFrame:rect]; } 问题是graphv超出了范围,所以我不能在if语句之后使用它。 所

我想创建一个对象,但类型取决于if条件的结果:

if ([type isEqualToString:@"day"]) {
    GraphDayView *graphv = [[GraphDayView alloc] initWithFrame:rect];
} else {
    GraphMonthView *graphv = [[GraphMonthView alloc] initWithFrame:rect];
}
问题是graphv超出了范围,所以我不能在if语句之后使用它。 所以我试着把它声明为一个id:

id graphv;

if ([type isEqualToString:@"day"]) {
    graphv = [[GraphDayView alloc] initWithFrame:rect];
} else {
    graphv = [[GraphMonthView alloc] initWithFrame:rect];
}
但现在的问题是,编译器不知道grapv是什么类型的对象。因此:

graphv.backgroundColor = [UIColor whiteColor];

给出了一个错误。有人知道如何解决这个问题吗

如果它们共享同一超类,则使用该超类而不是id。否则,创建两个变量并将其设置为nil:

GraphDayView *gdv = nil;
GraphMonthView *gmv = nil;

然后在if语句之后进行测试,以查看哪个已初始化。

如果它们共享相同的超类,则使用该超类而不是id。否则,创建两个变量并将其设置为nil:

GraphDayView *gdv = nil;
GraphMonthView *gmv = nil;

然后在if语句之后进行测试,看看哪个语句被初始化。

使GraphDayView和GraphMonthView都成为GraphCalendarView的子类。然后将您的
backgroundColor
设置为GraphCalendarView的属性

(或者,如果您的两个类已经是实现了
backgroundColor
的UI类的子类,那么您就可以在家自由了。)


将变量声明为
GraphCalendarView graphv,然后像前面一样继续。如果需要使用对两个派生类中的一个或另一个唯一的属性/方法,请首先强制转换到该类。

使GraphDayView和GraphMonthView都成为GraphCalendarView的子类。然后将您的
backgroundColor
设置为GraphCalendarView的属性

(或者,如果您的两个类已经是实现了
backgroundColor
的UI类的子类,那么您就可以在家自由了。)

将变量声明为
GraphCalendarView graphv,然后像前面一样继续。如果您需要使用两个派生类中的一个或另一个唯一的属性/方法,请首先强制转换到该类。

尝试此操作

id graphv;

if ([type isEqualoString:@"day"]) {
    (GraphDayView *)graphv = [[GraphDayView alloc] initWithFrame:rect];
} else {
    (GraphMonthView*)graphv = [[GraphMonthView alloc] initWithFrame:rect];
}
试试这个

id graphv;

if ([type isEqualoString:@"day"]) {
    (GraphDayView *)graphv = [[GraphDayView alloc] initWithFrame:rect];
} else {
    (GraphMonthView*)graphv = [[GraphMonthView alloc] initWithFrame:rect];
}

对于一个常见的超类,已经提出了很多最合适的建议

如果它们不是,则可以使用类型转换。但是,类型转换可能会导致异常,如未知selecor或bad_exec。若您想节省使用类型转换,那个么您应该始终检查isKindOfClass或respondsToSelector

样本:

id someClassObject//或任何其他通用超类*而不是id

如果([某事是真的]) someClassObject=[[AClass alloc]init]//假设弧。如果没有,那么您可能也希望在此处保留/自动释放。 其他的 someClassObject=[[AClass alloc]init]

//一些代码

如果([someClassObject isKindOfClass:[AClass class]]) [(AClass*)someClassObject类的方法]

// 如果([someClassObject isKindOfClass:[AClass class]]){ AClass*aclassttemp=(AClass*)someClassObject; [aClassTemp MethodofClass]; aClassTemp.propertyOfAClass=someValue; }

if([someClassObject respondsToSelector:@selector(MethodOfClass:)) [someClassObject PerformFormSelector:@selector(MethodOfClass:)withObject:[UIColor clearColor]]

请注意:选择器名称后面的数量与方法的参数数量相关。对于每个可能的方法调用,您可能找不到合适的performSelector变量。尤其是当您只能传递或返回对对象的引用时


同样,我只会在公共子类不适合您的情况下建议这两种方法。

对于公共超类,已经提出了许多非常适合的建议

如果不是,则可以使用类型转换。但是,类型转换可能会导致异常,如未知selecor或bad_exec。如果要保存使用类型转换,则应始终选中isKindOfClass或respondsToSelector

样本:

id someClassObject;//或任何其他公共超类*而不是id

如果([某事是真的]) someClassObject=[[AClass alloc]init];//假设为ARC。如果不是,那么您可能也希望在此处保留/autorelease。 其他的 someClassObject=[[AClass alloc]init]

…//一些代码

如果([someClassObject isKindOfClass:[AClass class]]) [(AClass*)someClassObject类的方法]

// 如果([someClassObject isKindOfClass:[AClass class]]){ AClass*aclassttemp=(AClass*)someClassObject; [aClassTemp MethodofClass]; aClassTemp.propertyOfAClass=someValue; }

if([someClassObject respondsToSelector:@selector(MethodOfClass:)) [someClassObject PerformFormSelector:@selector(MethodOfClass:)withObject:[UIColor clearColor]]

请注意:选择器名称后面的数量与方法的参数数量相关。对于每个可能的方法调用,您可能找不到合适的performSelector变量。尤其是当您只能传递或返回对对象的引用时


同样,如果公共子类不适合您的情况,我只建议使用这两个类。

通常在这种情况下使用公共基类,在这里可能吗?通常在这种情况下使用公共基类,在这里可能吗?假设两者都是UIView子类,那么您可以简单地将对象定义为/参考UIView。这可以作为背景色。根据子类的其他方法,您可能希望在UIView和您的子类之间引入一个通用的超类,正如Hot Clicks所建议的。谢谢,这很有效,很好的解决方案!不过有一个问题。我在这两个子类中都有一个名为
setmeasures
的方法。我是否应该解释一下它是抽象方法还是最好的方法?要么让它们从继承自
UIView
的公共基类继承,例如
@interface-GraphCalendarView:UIView
然后
@interface-GraphMonthView:GraphCalendarView
@interface-GraphDayView:GraphCalendarView
,或者如果它们没有ally共享任何其他共同点,如果您不想强加这种关系,您可以创建一个他们都遵守的协议,他们可以继续使用