【问题标题】:iOS - PLIST - Access other plist values from within plistiOS - PLIST - 从 plist 中访问其他 plist 值
【发布时间】:2013-04-15 19:57:00
【问题描述】:

我的应用中有一个 PLIST 文件,其中包含各种配置数据。一些数据是用于访问服务器的 URL。该服务器托管我们代码的几个不同版本的 JSON 文件。我想要做的是在具有版本的 PLIST 文件中有一个值,然后能够从其他值中引用它。所以 plist 中的 url 值可能是 https://www.company.com/${VERSION}/jsonfile.svc (其中 ${VERSION} 是同一个 plist 文件中的不同键)。

【问题讨论】:

    标签: ios plist


    【解决方案1】:

    正如 bshirley 所说,没有什么是自动的,但 Objective-C 可以帮助您。下面是一个名为 VariableExpansionNSDictionary 类别的简单实现,它演示了如何实现这一点(请注意,这尚未经过全面测试,但主要用于演示您可以使其自动化的方式。此外,expandedObjectForKey 假设您正在处理NSStrings,所以你可能需要稍微调整一下。

    // In file NSDictionary+VariableExpansion.h
    @interface NSDictionary (VariableExpansion)
    
    - (NSString*)expandedObjectForKey:(id)aKey;
    
    @end
    
    // In file NSDictionary+VariableExpansion.m
    #import "NSDictionary+VariableExpansion.h"
    
    @implementation NSDictionary (VariableExpansion)
    
    - (NSString*)expandedObjectForKey:(id)aKey
    {
        NSString* value = [self objectForKey:aKey];
    
        NSError *error = NULL;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\$\\{([^\\{\\}]*)\\}"
                      options:NSRegularExpressionCaseInsensitive
                      error:&error];
    
        __block NSMutableString *mutableValue = [value mutableCopy];
        __block int offset = 0;
    
        [regex enumerateMatchesInString:value options:0
                      range:NSMakeRange(0, [value length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
        {
        NSRange matchRange = [match range];
        matchRange.location += offset;
    
        NSString* varName = [regex replacementStringForResult:match
                               inString:mutableValue
                                 offset:offset
                               template:@"$1"];
    
        NSString *varValue = [self objectForKey:varName];
        if (varValue)
        {
            [mutableValue replaceCharactersInRange:matchRange
                        withString:varValue];
            // update the offset based on the replacement
            offset += ([varValue length] - matchRange.length);
        }
        }];
    
        return mutableValue;
    }
    
    @end
    
    
    // To test the code, first import this category:
    #import "NSDictionary+VariableExpansion.h"
    
    // Sample NSDictionary.
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"http://${HOST}/${VERSION}/bla", @"URL",
            @"1.0", @"VERSION",
            @"example.com", @"HOST", nil];
    
    // And the new method that expands any variables (if it finds them in the PLIST as well).
    NSLog(@"%@", [dict expandedObjectForKey:@"URL"]);
    

    最后一步的结果是http://example.com/1.0/bla,表明您可以在单个值中使用多个变量。如果未找到变量,则不会在您的原始字符串中触及它。

    由于您使用 PLIST 作为源,因此请使用 dictionaryWithContentsOfFile

        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    

    【讨论】:

      【解决方案2】:

      你尝试过什么吗?这个比较简单,按你说的转换一下,具体使用方法:stringByReplacingOccurrencesOfString:

      【讨论】:

      • 现在我正在提取值并从代码中构造字符串。我希望这在 plist 中自动完成,所以当我提取值时,版本号会根据 plist 文件中的值插入。
      • 没有什么“自动”可供您利用,从文件加载字典,使其可变,迭代值并替换您感兴趣的值。如果这是您想要的以后喜欢用,把它设为NSMutableDictionary上的一个类别,这样你就可以在别处使用它了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多