我知道可以通过预处理 Info.plist 文件将变量存储在默认的 Info.plist 文件中。我想知道是否可以用自定义 plist 文件做类似的事情。
我通过以下代码加载 plist 文件:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *customPlistPath = [path stringByAppendingPathComponent:@"custom.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:customPlistPath];
是否可以输入 ${PRODUCT_NAME} 或 ${MY_OWN_VARIABLE_NAME} 等值,并在我读入后在自定义 plist 文件中使用它们?目前,当我将自定义 plist 文件读入 NSDictionary 时,变量值不会被替换
最佳答案
Cocoa Touch 框架中没有内置任何东西可以为您完成这项工作。您必须编写自己的代码来在运行时处理这些值。我已经为我自己的应用程序完成了这个。我在实用程序类中使用了这两个方法。
+ (NSString *)processVariables:(NSString *)string {
if (string.length == 0) {
return string;
}
NSRange range = [string rangeOfString:@"${" options:NSLiteralSearch];
if (range.location != NSNotFound) {
NSMutableString *res = [string mutableCopy];
do {
NSRange close = [res rangeOfString:@"}" options:NSLiteralSearch];
if (close.location == NSNotFound) {
break; // Uh-oh - bad
}
NSRange rngVar = NSMakeRange(range.location, close.location - range.location + 1);
NSString *var = [res substringWithRange:NSMakeRange(rngVar.location + 2, rngVar.length - 3)];
NSString *val = [variables objectForKey:var];
if (val) {
[res replaceCharactersInRange:rngVar withString:val];
} else {
[res replaceCharactersInRange:rngVar withString:var];
}
range = [res rangeOfString:@"${" options:NSLiteralSearch];
} while (range.location != NSNotFound);
return res;
} else {
return string;
}
}
+ (id)expandPlistVariables:(id)root {
if ([root isKindOfClass:[NSArray class]]) {
NSArray *array = (NSArray *)root;
NSMutableArray *res = [[NSMutableArray alloc] initWithCapacity:array.count];
for (id obj in array) {
if ([obj isKindOfClass:[NSString class]]) {
[res addObject:[self processVariables:(NSString *)obj]];
} else {
id newObj = [self expandPlistVariables:obj];
[res addObject:newObj];
}
}
return res;
} else if ([root isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = (NSDictionary *)root;
NSMutableDictionary *res = [[NSMutableDictionary alloc] initWithCapacity:dict.count];
for (NSString *key in [dict allKeys]) {
id obj = dict[key];
if ([obj isKindOfClass:[NSString class]]) {
res[key] = [self processVariables:(NSString *)obj];
} else {
id newObj = [self expandPlistVariables:obj];
res[key] = newObj;
}
}
return res;
} else {
// oops
return root;
}
}
您将传入从 plist 加载的字典(或数组)并返回一个新字典(或数组),其中包含变量引用的所有字符串值都已更新。
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *customPlistPath = [path stringByAppendingPathComponent:@"custom.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:customPlistPath];
plistData = [UtilityClass expandPlistVariables:plistData];
在代码中,variables 是一个包含变量值的 NSDictionary。此字典中的键是您的 plist 中 ${ 和 } 之间的值。示例:
variables = @{ @"PRODUCT_NAME" : @"MyProduct", @"MY_OWN_VARIABLE_NAME" : @"Some Value" };
关于ios - 自定义 plist 文件中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18708278/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta