2012-01-09 64 views
0

我使用ASIFormDataRequest從我的iphone應用程序發送一個json字符串到服務器。有時候,當字符串太大時,iphone不會發送它。請求setTimeOutSeconds是60秒。我想在我的json字符串大的時候把它做成塊,然後把這些塊發送給服務器。我不能使用componentsSepratedByString:方法。我怎樣才能使json字符串的子串的數組。可以說每個子字符串的長度應該是200.提前致謝。如何在objective-c中創建json字符串的塊?

回答

1

嘗試這樣:

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; 
} 
相關問題