gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:36:06 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com