Reputation: 739
As the title described below , A superclass ,the name is 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
A subclass , the name is ZpIOFileObject
@interface ZpIOFileObject : ZpIOObject
- (void)fileDataWithAsynchronize:(void(^)(NSData *fileData))result ;
@end
Another subclass , the name is 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
You can see this code :
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 ;
}
I want to convert ZpIOObject to ZpIOFileObject/ZpIODocumentObject .
I think you will think is so easy : create ZpIOFileObject/ZpIODocumentObject and assignment . for example:
ZpIOFileObject *fileObject = [[ZpIOFileObject alloc] init] ;
fileObject.property = ioobject.property
... do something ...
No ! I don't want this .
I want a superclass convert to subclass , why ? In my opinion , ioObject is a existing object , create(alloc) a new object(for example [[ZpIOFileObject alloc] init]) is Waste memory resources !
I can't bear to waste memory resources !
So , My question is :
If superclass is an existing object , How to convert superclass' object to subclass' object ?
Upvotes: 0
Views: 654
Reputation: 739
Tested for a long time, I am sure, can't do that 。
subclass' object can convert to superclass' object , but superclass' object cannot convert to subclass' object .
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 !
Upvotes: 1