@interface CustomClass : NSObject@property (nonatomic,strong) NSArray * nicestArrayEver;@end
我想创建一个CustomClass的子类,但这里是catch
@interface ASubClassCustomClass : CustomClass@property (nonatomic,strong) NSMutableArray * nicestArrayEver;@end
你可以想象的问题是,当我初始化ASubClassCustomClass并调用它的超级初始化器(因为还需要其他属性)时,会创建不可变的nicestArrayEver ..我怎样才能避免它的创建,所以我可以设置可变的?
注意:这只是一个例子,真正的实现调用了很多创建和真正定制的子类(不是NSArray).
解决方法 您可以通过使用不同的后备变量使其工作,在合成它时如下所示:@synthesize nicestArrayEver = nicestArrayEverSubClass_;#import <Foundation/Foundation.h>@interface CustomClass : NSObject@property (nonatomic,strong) NSArray * nicestArrayEver;@end@implementation CustomClass@synthesize nicestArrayEver ;-(ID)init{ if (self = [super init]) { nicestArrayEver = [[NSArray alloc] init]; } return self;}@end@interface ASubClassCustomClass : CustomClass@property (nonatomic,strong) NSMutableArray * nicestArrayEver;@end@implementation ASubClassCustomClass@synthesize nicestArrayEver = nicestArrayEverSubClass_;-(ID)init{ if (self = [super init]) { nicestArrayEverSubClass_ = [[NSMutableArray alloc] init]; } return self;}@endint main(int argc,const char * argv[]){ @autoreleasepool { CustomClass *c1 = [[[CustomClass alloc] init] autorelease]; ASubClassCustomClass *c2 = [[[ASubClassCustomClass alloc] init] autorelease]; NSLog(@"%@",NsstringFromClass([[c1 nicestArrayEver] class])); NSLog(@"%@",NsstringFromClass([[c2 nicestArrayEver] class])); } return 0;}
产量
2012-05-27 01:59:16.221 NicestArray[2312:403] __NSArrayI2012-05-27 01:59:16.225 NicestArray[2312:403] __NSArrayM
另一种方法可能是在基类中有两个init方法,一个用于实例化属性,另一个用于子类的任务 – 这将阻止你创建昂贵的对象只是为了抛出它们远.@H_502_40@现在,基类可以直接使用第二个init进行实例化,并进入false状态.您可以通过使用isMemberOfClass:检查自类类型来避免这种情况,如果类类型是基类,则抛出错误.
@interface CustomClass : NSObject@property (nonatomic,strong) NSArray * nicestArrayEver;-(ID)initWithoutArray;@end@implementation CustomClass@synthesize nicestArrayEver ;-(ID) initWithoutArray{ if (self = [super init]) { if ([self isMemberOfClass:[CustomClass class]]) { [NSException raise:@"AbstractMethodCall" format:@"%@ should be called only from Subclasses of %@",NsstringFromSelector(_cmd),NsstringFromClass([self class])]; } } return self;}-(ID)init{ if (self = [super init]) { nicestArrayEver = [[NSArray alloc] init]; } return self;}@end@interface ASubClassCustomClass : CustomClass@property (nonatomic,strong) NSMutableArray * nicestArrayEver;@end@implementation ASubClassCustomClass@synthesize nicestArrayEver = nicestArrayEverSubClass_;-(ID)init{ if (self = [super initWithoutArray]) { nicestArrayEverSubClass_ = [[NSMutableArray alloc] init]; } return self;}@endint main(int argc,NsstringFromClass([[c2 nicestArrayEver] class])); //this works,as it is the subclass ASubClassCustomClass *shoulDWork = [[[ASubClassCustomClass alloc] init] autorelease]; // ouch! CustomClass *shouldCrash = [[[CustomClass alloc] initWithoutArray] autorelease]; } return 0;}总结
以上是内存溢出为你收集整理的objective-c – 使用不同的子类属性创建子类全部内容,希望文章能够帮你解决objective-c – 使用不同的子类属性创建子类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)