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

最佳答案

write:maxLength:方法文档中:


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


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

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

10-14 19:59