首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建NSInputStream的子类?

如何创建NSInputStream的子类?
EN

Stack Overflow用户
提问于 2012-04-04 11:07:04
回答 2查看 2.4K关注 0票数 4

我想做一个NSInputStream的子类。简单地说,我试着编写如下代码,

代码语言:javascript
复制
@interface SeekableInputStream : NSInputStream
{
    NSUInteger startOffset;
    NSUInteger totalReadLen;
}

- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len;
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len;
- (BOOL)hasBytesAvailable;
- (void)open:(NSUInteger)offset;

@end

我用这门课就像下面这样。

代码语言:javascript
复制
SeekableInputStream *stm = [[SeekableInputStream alloc] initWithURL:url];

然后,在运行时,我可能会遇到以下错误消息。

-SeekableInputStream initWithURL::未识别的选择器发送到实例0x10018ff30

我没有因为故意使用父方法而重写initWithURL。根据我的知识,派生类可以使用父类的方法,不是吗?

不能继承像initWithURL这样的扩展方法吗?

有没有人告诉我如何对NSInputStream进行子类化?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-04 13:12:11

从NSStream.h

代码语言:javascript
复制
// NSStream is an abstract class encapsulating the common API to NSInputStream and NSOutputStream.
// Subclassers of NSInputStream and NSOutputStream must also implement these methods.
@interface NSStream : NSObject
- (void)open;
- (void)close;

- (id <NSStreamDelegate>)delegate;
- (void)setDelegate:(id <NSStreamDelegate>)delegate;
 // By default, a stream is its own delegate, and subclassers of NSInputStream and NSOutputStream must maintain this contract. [someStream setDelegate:nil] must restore this behavior. As usual, delegates are not retained.

- (id)propertyForKey:(NSString *)key;
- (BOOL)setProperty:(id)property forKey:(NSString *)key;

- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode;

- (NSStreamStatus)streamStatus;
- (NSError *)streamError;
@end

// NSInputStream is an abstract class representing the base functionality of a read stream.
// Subclassers are required to implement these methods.
@interface NSInputStream : NSStream
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len;
// reads up to length bytes into the supplied buffer, which must be at least of size len. Returns the actual number of bytes read.

- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len;
// returns in O(1) a pointer to the buffer in 'buffer' and by reference in 'len' how many bytes are available. This buffer is only valid until the next stream operation. Subclassers may return NO for this if it is not appropriate for the stream type. This may return NO if the buffer is not available.

- (BOOL)hasBytesAvailable;
// returns YES if the stream has bytes available or if it impossible to tell without actually doing the read.
@end

如您所见,没有initWithURL函数。因此,您的super无法工作,因为它实际上并不存在。就像MrTJ说的,它是一个类别类。它的定义如下:

代码语言:javascript
复制
// The NSInputStreamExtensions category contains additional initializers and convenience routines for dealing with NSInputStreams.
@interface NSInputStream (NSInputStreamExtensions)
- (id)initWithURL:(NSURL *)url NS_AVAILABLE(10_6, 4_0);

所以,我认为如果你在你的子类中使用它,它就能工作。

代码语言:javascript
复制
#import <Foundation/NSStream.h>

您需要导入该类别。记住,你不能子类一个类别,只是覆盖它,不能调用然后(如果可以,我不知道如何)

票数 2
EN

Stack Overflow用户

发布于 2012-04-04 13:03:20

如果您在SDK中签出NSStream.h,那么initWithURL不是在核心类NSInputStream中定义的,而是在一个名为NSInputStreamExtensions的类别中定义的。对于从继承类调用基类类别中定义的方法,我不太了解,但这肯定是您遇到的可见性问题的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10009867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档