如何在 Objective-C 中制作 json 字符串的块?

发布于 2024-12-25 17:46:38 字数 230 浏览 5 评论 0原文

我正在使用 ASIFormDataRequest 将 json 字符串从我的 iphone 应用程序发送到服务器。有时,当该字符串太大时,iPhone 不会发送它。请求setTimeOutSeconds为60秒。我想在 json 字符串很大时对其进行分块,然后将这些块发送到服务器。我无法使用 ComponentsSeperatedByString: 方法。我如何制作 json 字符串的子字符串数组。假设每个子字符串长度应为 200。提前致谢。

I am sending a json string from my iphone application to server using ASIFormDataRequest. Sometimes when that string is too big, iphone doesn't send it. request setTimeOutSeconds is 60 seconds. I want to make chunks of my json string when its big and then send those chunks to server. i can not use componentsSepratedByString: method. How can i make an array of substrings of json string. lets say that every substring length should be 200. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2025-01-01 17:46:39

尝试这样的事情:

NSString *longString = <SOME_LONG_STRING>;
NSUInteger chunkSize = 200;

NSMutableArray *chunks = [[NSMutableArray alloc] initWithCapacity:0];

NSUInteger length = [longString length];
NSUInteger currentPosition = 0;

while (currentPosition < length) {
    NSUInteger thisLength = MIN(chunkSize, length - currentPosition);
    NSString *thisChunk = [longString substringWithRange:NSMakeRange(currentPosition, thisLength)];
    [chunks addObject:thisChunk];
    currentPosition += thisLength;
}

Try something like this:

NSString *longString = <SOME_LONG_STRING>;
NSUInteger chunkSize = 200;

NSMutableArray *chunks = [[NSMutableArray alloc] initWithCapacity:0];

NSUInteger length = [longString length];
NSUInteger currentPosition = 0;

while (currentPosition < length) {
    NSUInteger thisLength = MIN(chunkSize, length - currentPosition);
    NSString *thisChunk = [longString substringWithRange:NSMakeRange(currentPosition, thisLength)];
    [chunks addObject:thisChunk];
    currentPosition += thisLength;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文