0
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

4

1 回答 1

3

write:maxLength:方法文档:

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

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

于 2011-07-22T05:21:51.917 回答