Reputation: 134
I would like to cast to a class type by string value.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
Class class = [self class];
NSString *className = NSStringFromClass(class);
NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:className owner:self options:NULL];
NSEnumerator* nibEnumerator = [nibContents objectEnumerator];
NSObject* nibObject = nil;
while ((nibObject = [nibEnumerator nextObject]) != nil) {
if ([nibObject isKindOfClass:class]) {
self = nibObject; // <<< compiler warning
break;
}
}
}
return self;
}
This code is part of a UITableViewCell superclass. I have a whole bunch of UITableViewCell subclasses that all wants to load their own nibs using the above snippet. This is working really well but I have a compiler warning on the line self = nibObject.
I would like to suppress the compiler warning but that would require casting, along the lines of
self = (className *)nibObject;
So how can I do this in my super class so I don't have to essentially repeat the above snippet in every subclass and cast per usual?
Upvotes: 0
Views: 136
Reputation: 3000
If you are completely sure that classes are compatible, then cast it as id
:
self = (id)nibObject;
(self is returned as id
as well)
Upvotes: 2