ios - 如何在 iOS 中将父类(super class)转换为子类

标签 ios objective-c subclass superclass

正如下面的标题所描述的,一个父类(super class),名字是ZpIOObject

typedef NS_OPTIONS(NSUInteger, ZpIOObjectType) {
      ZpIOObjectType_NONE = 0 , // is no exist file or no judgment object type
      ZpIOObjectType_File ,     // it's a file object
      ZpIOObjectType_Document   // it's a document object
};
@interface ZpIOObject:NSObject
      @property (nonatomic , assign ) ZpIOObjectType fileType ; 
      @property (nonatomic , strong ) NSString *absolutePathString ;

      /**create an object or subobject*/
      +(instancetype) ioObjectWithAbsolutePathString:(NSString *)absolutePathString ;
@end

一个子类,名字是ZpIOFileObject

@interface ZpIOFileObject : ZpIOObject
      - (void)fileDataWithAsynchronize:(void(^)(NSData *fileData))result ;
@end

另一个子类,名字是ZpIODocumentObject

@interface ZpIODocumentObject : ZpIOObject
      @property (nonatomic , strong , readonly) NSArray<ZpIOObject*> *subIOObjects ;
@end

@implementation ZpIODocumentObject
@synthesize subIOObjects = _subIOObjects ;

- (NSArray<ZpIOObject *>*)subIOObjects
{
    if( nil == self.absolutePathString || YES == [[self.absolutePathString trim] isEqualToString:emptyString] ){
        return nil ;
    }
    if( nil != self.error ){
        return nil ;
    }
    // 进行IO操作,获取最新的文件当前层级的子文件、子文件夹,一定是最新数据,但对IO开销大
    NSFileManager *fileManager = [NSFileManager defaultManager] ;
    NSError *error ;
    NSArray<NSString*>* subDirsNameArr = [fileManager contentsOfDirectoryAtPath:self.absolutePathString error:&error] ;
    if( nil == subDirsNameArr || 0 == [subDirsNameArr count] ){
        return nil ;
    }
    for( NSString *subFilePathString in subDirsNameArr ){
        NSMutableString *subFileAbsolutePathString = [[NSMutableString alloc] initWithString:subFilePathString] ;
        [subFileAbsolutePathString appendString:[NSString stringWithFormat:@"/%@",subFilePathString]] ;
        ZpIOObject * ioobject = [ZpIOObject ioObjectWithAbsolutePathString:subFileAbsolutePathString] ;
        switch ( ioobject.fileType ) {
            case ZpIOObjectType_File:{
            ; // How to convert ZpIOObject to ZpIOFileObject ??????
            }
            case ZpIOObjectType_Document:{
            ; // How to convert ZpIOObject to ZpIODocumentObject ??????
            }
            break;
        default:{/*do nothing*/}break ;
        }
    }
    return nil ;
}
@end

你可以看到这段代码:

        switch ( ioobject.fileType ) {
            case ZpIOObjectType_File:{
            ; // How to convert ZpIOObject to ZpIOFileObject ??????
            }
            case ZpIOObjectType_Document:{
            ; // How to convert ZpIOObject to ZpIODocumentObject ??????
            }
            break;
        default:{/*do nothing*/}break ;
        }

我想将 ZpIOObject 转换为 ZpIOFileObject/ZpIODocumentObject 。

我想你会觉得很简单: 创建 ZpIOFileObject/ZpIODocumentObject 并赋值。 例如:

        ZpIOFileObject *fileObject = [[ZpIOFileObject alloc] init] ;
        fileObject.property = ioobject.property
... do something ...

不!我不想要这个。

我想要将父类(super class)转换为子类,为什么? 在我看来,ioObject是一个现有的对象,创建(分配)一个新的对象(例如[[ZpIOFileObject alloc] init])是浪费内存资源!

我不忍心浪费内存资源!

所以,我的问题是:

如果父类(super class)是一个现有的对象,如何将父类(super class)的对象转换为子类的对象?

最佳答案

测试了很久,我确定,不能那样做。

子类的对象可以转换为父类(super class)的对象,但父类(super class)的对象不能转换为子类的对象。

for example :

A inheritance ASuper

A *a = [[A alloc] init] ;
ASuper * aSuper = a ; // right 
A *aAnother = (A *) aSuper ; // right 

ASuper * aSuperAnother = [[ASpuer alloc]init] ;
A *a_other = aSuperAnother ; // Error !

关于ios - 如何在 iOS 中将父类(super class)转换为子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35642665/

相关文章:

ios - 无法为版本 14.2 的设备 MacFamily20,1 获取特征集

ios - UITableView和UISegmentedControl

ios - 有没有更好的方法来排除 tvOS 的 navigationBarTitle?

ios - 使用 FB iOS SDK 发布到 friend 提要时出错

objective-c - 使用 FB Request 从 Facebook 获取好友列表

ios - OCUnit的测试用例类中是否需要在public接口(interface)中定义测试方法

ios - 在 Parse iOS 中为每个对象抓取第一张图像

python - 我应该将列表子类化还是使用列表作为属性创建类?

c++ - 是否可以将非模板类子类化为模板类?

objective-c - 如何通过类别扩展向 NSMutableArray 添加属性?