NSOutputStream给出-1个字节?

时间:2011-07-22 05:17:19

标签: 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);

我得到了:Wrote -1 bytes

1 个答案:

答案 0 :(得分:3)

来自write:maxLength:方法文档:

  

返回值
  实际写入的字节数,如果发生错误,则为-1。可以使用streamError获取有关错误的更多信息。如果接收器是固定长度的流并且已达到其容量,则返回0。

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