简体   繁体   中英

Objective-C Casting to Custom Class in conditional statement

I have two classes of UICollectionViewCell - ComicStripViewCellA & ComicStripViewCellB.

Instead of

if (condition) {

     ComicStripViewCellA *comicStripViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"comicStripCell" forIndexPath:indexPath];
     comicStripViewCell.imageView.image = image;

     // Common Code Section
     comicStripViewCell.pageNumber.text = @"Something ...";
     .
     .

} else {

     ComicStripViewCellB *comicStripViewCell = [collectionView dequeueReusableCellWithReuseIdentifer:@"comicStripCell" forIndexPath:indexPath];
     comicStripViewCell.title.text = @"Something ...";

     // Common Code Section
     comicStripViewCell.pageNumber.text = @"Something ...";
     .
     .
}

I want to optimize the code since a lot of the code in "Common Code Section" is the same. This is what I want to achieve:

Class myClass;

if (condition) {
     myClass = NSClassFromString(@"ComicStripViewCellA");
} else {
     myClass = NSClassFromString(@"ComicStripViewCellB");
}

myClass *comicStripViewCell = [collectionView dequeueReusableCellWithReuseIdentifer:@"comicStripCell" forIndexPath:indexPath];
comicStripViewCell.title.text = @"Something ...";

// Common Code Section
comicStripViewCell.pageNumber.text = @"Something ...";
.
.

I know the above code will not compile. This is just to illustrate what I want to achieve. Is there any way of getting the comicStripViewCell casted to a class dynamically?

Thanks in advance!


Thank you all for your suggestions! I have used the @protocol and everything worked fine.

Make both cells subclasses of a common parent which defines the common methods. Then:

ComicStripViewCellB *comicCell = ...;

Or define an @protocol which lists the common methods, have the cell classes publicly declare that they implement the protocols ( @interface ComicStripViewCellB < ComicStripViewCell > ). Then:

UICollectionViewCell < ComicStripViewCellB > *comicCell = ...;

If they have the same methods and properties, you could create a protocol that both of them implements. Or you could create a class that inherits from UICollectionViewCell with the common stuff, and make your cells inherit this class.

With protocol:

UICollectionViewCell<MyCellProtocol> *cell;

if (condition) {
    cell = ...
} else {
    cell = ...
}

With subclass:

MyCommonCollectionViewCell *cell;
if (condition) {
    cell = ...
} else {
    cell = ...
}

If the two custom subclasses have common properties that are not in UICollectionViewCell , then you should create a new superclass for them to share that does have those. Then, the comicStripViewCell variable should be a pointer to that shared superclass.

Use a protocol in which you declare the necessary properties and methods both classes should implement, then you'll be able to refactor your common code section.

@protocol Foo <NSObject>

@property (strong, nonatomic) NSString *identifier;

@end

- (void)bar
{
    id <Foo> var = [someObject callMethod];
    var.identifier = @"quirk";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM