objective-c - NSOutputStream 给出 -1 字节?

标签 objective-c cocoa macos nsoutputstream

NSDictionary *bundle = [NSDictionary dictionaryWithObjectsAndKeys:message,@"message", toUserName, @"receiver",fromUserName, @"sender", nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:bundle];           
NSOutputStream *outStream;
[toUser getInputStream:nil outputStream:&outStream];
[outStream open];
NSInteger bytes = [outStream write:[data bytes] maxLength: [data length]];
[outStream close];

success = YES;
NSLog(@"Wrote %ld bytes", bytes);

我得到:写了-1个字节

最佳答案

来自write:maxLength:方法文档:

Return Value
The number of bytes actually written, or -1 if an error occurs. More information about the error can be obtained with streamError. If the receiver is a fixed-length stream and has reached its capacity, 0 is returned.

返回值-1表示发生错误。您应该使用[outStream streamError]获取一个 NSError 对象,告诉你出了什么问题,以便你可以尝试修复它,或者为用户获取问题的描述。

关于objective-c - NSOutputStream 给出 -1 字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6785934/

相关文章:

ios - 在 Cocoa Touch 中,数据源可以称为模型吗?

iphone - UIControlEvents 和子类化 UIControl

iphone - 可以限制 NSArray FilteredArrayUsingPredicate 返回的结果

ios - 实现TripAdvisor api的示例示例-iOS应用程序

xcode - 当窗口具有焦点时更改 NSView 背景颜色

C++外部程序IO

iPhone RestKit 如何启用 RKLogDebug?

xcode - 核心数据 : bundle. main.url 意外(突然)返回 NIL

Mac系统上的mysql设置问题

macos - 如何在终端 mac OS X 中使用和安装 SystemC?