ios - 有效地在 Objective C iOS 中解析字符串

标签 ios objective-c xcode algorithm performance

我有一个包含 ATTR1、ATTR2、ATTR3 键的 (NSDictionary * record)。

我有一个需要应用于 sql lite 数据库的动态 where 子句。但在构建获取请求之前,谓词必须采用“ATTR1=%@ AND ATTR2=%@ AND ATTR3=%@”格式。我还有一个以这种格式定义的静态字符串 whereClause "ATTR1=:ATTR1 AND ATTR2=:ATTR2 AND ATTR3=:ATTR3"(注意:这种格式是不可更改的。它是在客户的生产服务器上定义的)。我所说的动态是指“:ATTR1”、“:ATTR2”、“:ATTR3”应该被上面提到的字典变量记录中的值替换。

因此,要创建谓词,我需要创建以下字符串和属性数组,以便执行获取请求。

“ATTR1=%@ 和 ATTR2=%@ 和 ATTR3=%@”

["ATTR1","ATTR2","ATTR3"]

我找到的解决方案如下:

 NSString * whereClause = @"ATTR1=:ATTR1 AND ATTR2=:ATTR2 AND ATTR3=:ATTR3";
 NSArray * primaryTokens = [whereClause componentsSeparatedByString: @":"];
 NSUInteger  primaryTokenCount = [primaryTokens count];
 NSMutableString * finalWhereClause = [[NSMutableString alloc] initWithString:primaryTokens[0]];
 NSMutableArray * attributes = [[NSMutableArray alloc]init];
 if(primaryTokenCount >1)
 {
    for(int i=0;i<(primaryTokenCount-1);i++){
        NSArray * secondaryTokens = [primaryTokens[i+1] componentsSeparatedByString: @" "];
        NSUInteger secondaryTokenCount = [secondaryTokens count];
        if(secondaryTokenCount > 1)
        {
            [finalWhereClause appendString:@"%@"];
            NSArray * newSecondaryArray = [secondaryTokens subarrayWithRange:NSMakeRange(1,[secondaryTokens count]-1)];
            [finalWhereClause appendString:@" "];
            [finalWhereClause appendString:[newSecondaryArray componentsJoinedByString:@" "]];
        }
    [attributes addObject:secondaryTokens[0]];
    }
[finalWhereClause appendString:@"%@"];
}
NSLog(@"THE FINAL PARSED STRING IS : %@",finalWhereClause);
NSLog(@"THE FINAL ATTRIBUTES IS : %@",[attributes componentsJoinedByString:@","]);

上面的代码有效。但它的开销是使用 3 个额外的数组并在主循环的每次运行中按组件分隔。 Objective C 中是否有一种有效的方法来实现这种解析,以避免这种额外存储的开销。此方法将被广泛调用。因此,我们将不胜感激。

最佳答案

NSString *whereClause = @"ATTR1=:ATTR1 AND ATTR2=:ATTR2 AND ATTR3=:ATTR3";
NSString *finalWhereClause = whereClause;

NSArray *clauseComponents = [whereClause componentsSeparatedByString: @" "];
NSMutableArray *attributes = [NSMutableArray new];

for (NSString *component in clauseComponents) {

    if ([component containsString:@":"]) {

        NSString *paramName = [component substringFromIndex:[component rangeOfString:@":"].location + 1];
        [attributes addObject:paramName];


        NSString *separatedParamName = [@":" stringByAppendingString:paramName];
        finalWhereClause = [finalWhereClause stringByReplacingOccurrencesOfString:separatedParamName withString:@"%@"];

    }

}

NSLog(@"THE FINAL PARSED STRING IS : %@",finalWhereClause);
NSLog(@"THE FINAL ATTRIBUTES IS : %@",[attributes componentsJoinedByString:@","]);

关于ios - 有效地在 Objective C iOS 中解析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556355/

相关文章:

ios - Swift - 更改幻灯片的大小以删除 tableView 中的按钮

ios - TestFlight Beta 测试 iOS 应用程序

iphone - iOS:将 URL 解析为段

objective-c - 如何在不重复的情况下插入与另一个相关的核心数据记录

ios - 如何从命令行在模拟器上构建和运行 xcodeproj?

android - iOS 版 GCM 的优势?

ios - Instagram API 获取带有特定标签的图像

objective-c - 帮助填充数字字符串

ios - 从父屏幕中删除 ViewController 保持黑色

ios - 使用 AVAudioPLayer 播放音频片段时出现故障