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/