如何使用 for 循环优化此代码

Posted

技术标签:

【中文标题】如何使用 for 循环优化此代码【英文标题】:how to optimize this code using for loop 【发布时间】:2013-08-12 09:46:08 【问题描述】:

请帮我优化这段代码:

- (NSMutableDictionary *) parseResponse:(NSData *) data 

NSString *myData = [[NSString alloc] initWithData:data
                                         encoding:NSUTF8StringEncoding];
NSLog(@"JSON data = %@", myData);
NSError *jsonParsingError = nil;
//parsing the JSON response
id jsonObject = [NSJSONSerialization
                 JSONObjectWithData:data
                 options:NSJSONReadingAllowFragments
                 error:&jsonParsingError];
if (jsonObject != nil && jsonParsingError == nil)

    //NSLog(@"Successfully deserialized...");
        NSLog(@"json object is %@",jsonObject);
    if([jsonObject isKindOfClass:[NSDictionary class]])
    
        NSLog(@"It has only  dictionary so simply read it");

    
    else if([jsonObject isKindOfClass:[NSArray class]])
    
        NSLog(@"It has only  NSArray so simply read it");

    
    else if([jsonObject isKindOfClass:[NSString class]])
    
       NSString *stringWithoutOpenCurly = [jsonObject stringByReplacingOccurrencesOfString:@"" withString:@""];
        NSString *stringWithoutOpenAndCloseCurly = [stringWithoutOpenCurly stringByReplacingOccurrencesOfString:@"" withString:@""];
        NSArray *arrayOfKeyValue = [stringWithoutOpenAndCloseCurly componentsSeparatedByString:@","];

        NSString *statusString = [arrayOfKeyValue objectAtIndex:0];
        NSArray *statusKeyValueArray = [statusString componentsSeparatedByString:@":"];
        NSString *statusKey, *statusValue;
        statusKey = [[statusKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        statusValue = [[statusKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSString *messageString = [arrayOfKeyValue objectAtIndex:1];
        NSArray *messageKeyValueArray = [messageString componentsSeparatedByString:@":"];
        NSString *messageKey, *messageValue;
        messageKey = [[messageKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        messageValue = [[messageKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];


        NSString *dataIdString = [arrayOfKeyValue objectAtIndex:2];
        NSArray *dataIdKeyValueArray = [dataIdString componentsSeparatedByString:@":"];
        NSString *dataIdKey, *dataIdValue;
        dataIdKey = [[dataIdKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        dataIdValue = [[dataIdKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSString *nextString = [arrayOfKeyValue objectAtIndex:3];
        NSArray *nextKeyValueArray = [nextString componentsSeparatedByString:@":"];
        NSString *nextKey, *nextValue;
        nextKey = [[nextKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        nextValue = [[nextKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSString *b64String = [arrayOfKeyValue objectAtIndex:4];
        NSArray *b64KeyValueArray = [b64String componentsSeparatedByString:@":"];
        NSString *b64Key, *b64Value;
        b64Key = [[b64KeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        b64Value = [[b64KeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];


        NSString *fileNameString = [arrayOfKeyValue objectAtIndex:5];
        NSArray *fileNameKeyValueArray = [fileNameString componentsSeparatedByString:@":"];
        NSString *fileNameKey, *fileNameValue;
        fileNameKey = [[fileNameKeyValueArray objectAtIndex:0] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        fileNameValue = [[fileNameKeyValueArray objectAtIndex:1] stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        NSMutableDictionary *responseDictionary = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:statusValue, messageValue, dataIdValue, nextValue, b64Value, fileNameValue, nil] forKeys:[NSArray arrayWithObjects:statusKey, messageKey, dataIdKey, nextKey, b64Key, fileNameKey, nil]];
        NSLog(@"manually created dictionary is ++++++++++++++++++++++++++++++%@",responseDictionary);
        NSLog(@"statusValue is ++++++++++++++++++++++++++++++%@",[responseDictionary valueForKey:statusKey]);
        return responseDictionary;
    


else //previoucly no else handler so put it here

// mErrorMessage = @"服务器错误"; // [self stopIndicatorProgressWithError];

jsonObject 仅适用于 NSString 类????不知道什么问题。 有什么帮助吗?

感谢和问候。

【问题讨论】:

【参考方案1】:

您可以使用适当的 Foundation 类“优化”您的代码:

NSString *jsonObject = ...; // your JSON data as string

NSData *jsonData = [jsonObject dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary * responseDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
              options:0 error:&error];

或者,如果你真的需要一个可变字典:

NSMutableDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:jsonData
             options:NSJSONReadingMutableContainers error:&error];

【讨论】:

感谢您的回复,我使用的代码与您提供的代码相同,请参阅***.com/questions/18127363/… 这就是我手动创建键/值的原因 我无法以这种方式获取字典/数组。请查看我的编辑,以前我使用 NSJsonSerialization 类,但我得到 NSString 作为响应,这就是我手动创建 NSDictionary 的原因,请参阅任何编辑。 . @Maddy:我假设您的响应是“嵌套 JSON”:jsonObject 是一个字符串,它本身就是字典的 JSON 编码形式。在这种情况下,我的代码也可以将 jsonObject 字符串转换为正确的字典。比较 ***.com/a/16948785/1187415 的类似问题。

以上是关于如何使用 for 循环优化此代码的主要内容,如果未能解决你的问题,请参考以下文章

如何优化此代码以仅显示圆形数组的轮廓?

如何优化不同长度的嵌套for循环?

你将如何优化这个简短但非常慢的 Python 循环?

如何将此代码转换为 for 循环格式?

如何使用循环转换优化 C 代码? [关闭]

如何优化js代码