0
我試圖將環境變量替換爲一個字符串,但是我被regularExpresionWithPattern
停滯了。將環境變量替換爲NSString
我發現了另一個問題,一些代碼,似乎回答別人的問題,但因爲種種原因,並沒有爲我工作:
NSString *substituteVariables(NSString *input) {
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSError *error = nil;
// I know this pattern isn't good enough, but it's a start!
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"$([A-Z_]+)"
options:NSRegularExpressionCaseInsensitive error:&error];
return [regex stringByReplacingMatchesInString:input options:0
range:NSMakeRange(0, input.length)
usingBlock:^NSString *(NSTextCheckingResult *result,
NSMatchingFlags flags, BOOL *stop) {
NSString *envKey = [input substringWithRange:[result rangeAtIndex:1]];
return [environment objectForKey:envKey];
}];
};
這會產生一個編譯器警告這在很大程度上表明,碼贏沒有工作:
appbundler/native/main.m:64:19: warning: instance method '-stringByReplacingMatchesInString:options:range:usingBlock:' not found (return type defaults to 'id') [-Wobjc-method-access]
return [regex stringByReplacingMatchesInString:input options:0 range:NSMakeRange(0, input.length) usingBlock:^NSString *(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSRegularExpression.h:25:12: note: receiver is instance of class declared here
@interface NSRegularExpression : NSObject <NSCopying, NSCoding> {
^
1 warning generated.
雖然代碼有望與10.7 SDK一起工作,但我正在針對OS 10.8 SDK進行構建。
我檢查了文檔,並且確實在文檔中不存在此方法。然而,它以某種方式爲別人工作,我不明白爲什麼。
orz所以它畢竟是一個自定義的方法。我不是特別想要這個代碼庫膨脹,所以我可能只是內聯。 – Trejkaz