- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个 NSInputStream 的子类。简单地说,我尝试编写如下代码,
@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
并且,我使用了如下所示的类。
SeekableInputStream *stm = [[SeekableInputStream alloc] initWithURL:url];
然后,在运行时,我可能会遇到以下错误消息。
-[SeekableInputStream initWithURL:]:无法识别的选择器发送到实例 0x10018ff30
我没有故意重写 initWithURL 来使用父方法。据我所知,派生类可以使用父类的方法,不是吗?
像initWithURL这样的扩展方法不能被继承吗?
有人告诉我如何对 NSInputStream 进行子类化吗?
最佳答案
来自 NSStream.h
// 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 所说,它是一个类别类。它的定义是:
// 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);
所以,我认为如果你在你的子类中使用它,它就可以工作。
#import <Foundation/NSStream.h>
您需要导入类别。请记住,您不能对类别进行子类化,只需覆盖它,然后就无法调用(或者如果可以,我不知道如何)
关于objective-c - 如何创建 NSInputStream 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008902/
我正在尝试将输入流转换为字符串。我尝试转换的输入流是 NSURLRequest.HTTPBodyStream,显然 httpbody 设置为 null 并在您发出请求后替换为流。我该怎么做呢?这是我目
我在使用 NSInputStream 时遇到问题。我有连接到服务器的客户端应用程序,然后服务器将开始通过 TCP 每秒大约 1 条消息重复向我的客户端应用程序发送消息。服务器只是向客户端广播消息,消息
我正在尝试创建一个简单的iPhone应用程序,它可以与服务器通信(目前正在我的计算机上运行并且工作正常)。我一直在尝试使用 NSStream 类,但遇到了很多问题。我真的只是想使用流来模拟 telne
是否有办法查看 nsinputstream 中的值,例如将其转换为字符串? 最佳答案 我假设您指的是从 NSInputStream 返回的数据: - (NSInteger)read:(uint8_t
我的应用程序使用 NSInputStream,如下所示: inputStream.delegate = self; [inputStream scheduleInRunLoop:[NSRunLoop
在我的应用程序中,我使用 NSStreams 进行客户端服务器通信。在事件 hasbytesAvailable 的 delegate 方法中,当我读取数据时返回 null 案例:当长度为4096时读取
当我从服务器接收数据时出现错误(数据只是字符串)。 例如 服务器发送数据(15000字节) 我的 iPhone 收到数据(7878 字节) 所以我尝试搜索,我知道方法是答案。 但我无法应用我的代码..
我正在尝试使用 NSInputStream 通过 TCP 套接字连接接收数据。在服务器端,我先发送数据大小,然后再发送数据本身。在 iOS 客户端,我需要从 NSInputStream 中提取前 4
请帮助我,我快疯了。我需要创建一个 NSInputStream,这样我就可以通过 wifi 从摄像头读取实时预览。liveViewStream 是一个 NSInputStream-Instance-V
我正在将一个 java 客户端移植到 iphone,我一直在想是否有一个围绕 NSInputStream 的包装器来从停止在指定分隔符处的流中读取字符串?我正在考虑类似 Javas Scanner 类
我想向 NSInputStream 添加三个“部分”:一个 NSString,另一个流的输出,然后是另一个 NSString。思路如下: 第一个和最后一个 NSString 代表 SOAP 请求的开始
全部, 我有一台服务器,它有一个用于通信的 TCP 套接字流。我需要访问该流并读取它需要发送给我的初始数据。 我目前的代码如下。老实说,我对此完全视而不见。我不确定这是否正确,更不用说适合这项工作了。
我正在尝试通过 NSInputStream 从我的文档目录中读取自定义文件,以将其上传到 FTP 服务器。我使用的代码与 SimpleFTPSample 中演示的代码完全相同由苹果公司提供。只要文件为
嘿伙计们,我正在从网络服务器上下载一个 yaml 文件。我的 NSInputStream 与本地 yaml 文件配合得很好(使用 file://filename.yaml url 方案) 以下是相关代
我正在尝试打断 [NSOutputstream write] 当连接丢失时。 但是,即使我使用 关闭流,它似乎仍然继续阻塞 [NSOutputstream close]. 我想要实现的只是在服务器连接
我需要使用 Windows 1252 编码将图像流式传输到 .NET 后端服务。所以我相信我需要创建一个 NSURLRequest 并将其 HTTPBodyStream 设置为使用图像数据创建的 NS
我有 openssl 服务器和 Objective-C 客户端。我这样发消息 uint32_t testD = 161; err = SSL_write(ssl_, &testD, sizeof(ui
我正在从服务器按顺序获取 NSData block ,超过大约。 4096 字节,按顺序。每个接收到的 block 的大小可能不同。 我想做的是将所有这些字节附加到某处,同时从数据的开头开始读取,顺序
我在一个文件中有 100 MB 的数据。这 100 mb 数据将被虚拟地划分。即,我需要创建一个指向不同 5MB block 的 NSInputStream。这可以通过使用 NSData 创建流来实现
我想将 NSInputStream 传递给 MPMoviePlayerController、MPMoviePlayerViewController 或其他任何东西。 输入流利用了 Apple 框架不支
我是一名优秀的程序员,十分优秀!