gpt4 book ai didi

ios - Objective-C - InputStream 到 NSString

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:20 24 4
gpt4 key购买 nike

我是 Objective C 的新手,所以我正在尝试做我通常在 Java 中做的事情,比如将 InputStream 转换为 String。基本上,我想在 Objective C 中编写这段 Java 代码...

InputStream in = … //An inputStream i got from http request

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
str.append(line);
}

String myString = str.toString();
in.close();

希望我的问题描述的足够清楚! :)

谢谢!

最佳答案

好吧,Caleb 是对的,但要回答这个问题:

NSInputStream *inputStream = [[NSInputStream alloc] init];
uint8_t buffer[1024];
int len;
NSMutableString *total = [[NSMutableString alloc] init];
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
[total appendString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]];
}
}

您的编码参数可能不同。

关于ios - Objective-C - InputStream 到 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14846118/

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