[英]Extending NSMutableData class
My question is simple. 我的问题很简单。 I want to extend the NSMutableData class in iOS and override some of the methods defined in NSMutableData.
我想在iOS中扩展NSMutableData类,并覆盖NSMutableData中定义的一些方法。 For eg I want to override writeToFile function with my own wrapper implementation for it.
例如,我想用自己的包装器实现重写writeToFile函数。 Is it possible to do this?
是否有可能做到这一点?
My TestNSMutableData.h 我的TestNSMutableData.h
@interface TestNSMutableData : NSMutableData
-(BOOL)writeToFile:(NSString*)path atomically:(BOOL)useAuxiliaryFile;
@end
My TestNSMutableData.m 我的TestNSMutableData.m
@implementation TestNSMutableData
-(BOOL)writeToFile:(NSString*)path atomically:(BOOL)useAuxiliaryFile{
//Some activity on the data and modifying it
return [self writeToFile:path atomically:useAuxiliaryFile];
}
When I try to use the TestNSMutableData in one of my project like 当我尝试在我的项目之一中使用TestNSMutableData时
TestNSMutableData myData;
myData = [TestNSMutableData alloc]init];
[myData writeToFile:path atomically:YES]
I get an error as follows 我收到如下错误
NSInvalidArgumentException'- reason ' * - length only defined for abstract class.
NSInvalidArgumentException'-原因' * -仅为抽象类定义的长度。 Define -[TestNSMutableData length] !
定义-[TestNSMutableData length]!
Is it possible at all to override the methods defined in Native classed for eg NSMutableData ? 是否有可能完全覆盖在Native中为例如NSMutableData定义的方法?
UPDATE I create NSString class category method for writeTofile 更新我为writeTofile创建NSString类类别方法
My implementation is 我的实现是
-(BOOL)writeToFile:(NSString*)path atomically:(BOOL)useAuxiliary encoding:(NSStringEncoding)enc error:(NSError**)error{
//Manipulating NSString
self = manipulatedString;
return [super writeToFile....];
}
I get a warning "NSObject may not respond to 'writeToFile' function. Which is technically correct as NSString is derived from NSObject and NSObject does not have writeToFile function. 我收到警告“ NSObject可能不响应'writeToFile'函数。从技术上讲,这是正确的,因为NSString是从NSObject派生的,并且NSObject没有writeToFile函数。
What I want is to write the manipulated string in the file and not the actual string. 我想要的是在文件中写入操作后的字符串,而不是实际的字符串。
Is there a way to call the NSString writeToFile method from the above function. 有没有一种方法可以从上述函数中调用NSString writeToFile方法。
Regards, 问候,
Nirav 尼拉夫
NSMutableData
is probably a class cluster , making it a bit hard to subclass . NSMutableData
可能是一个类集群 ,因此很难进行子类化 。 Why not use a category to add a custom -writeToFile
method instead? 为什么不使用类别来添加自定义
-writeToFile
方法呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.