2017-02-15 4 views
1

値を置き換える必要があるURLストリングがあります。メインストリング内の複数の異なるサブストリングを置き換えます。

例:http:

\\ www.domain.com ID = [USER_ID] &レコード= [CALL_KEY] &他の何らかの= [OTHER_STUFF]

\ []を探して文字列をループする必要があります。

今のところ、私は個々のキーをハードコードし、値で置き換える必要があります。

- (NSString*) injectValuesIntoURL:(NSString*) url{ 


    NSString * injected = @""; 

    //[CALL KEY] 
    injected = [url stringByReplacingOccurrencesOfString:@"[CALL_KEY]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kCallKey"]]; 

    //[USER_ID] 
    injected = [injected stringByReplacingOccurrencesOfString:@"[USER_ID]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kUsername"]]; 

     //[OTHER_STUFF] 
     injected = [injected stringByReplacingOccurrencesOfString:@"[OTHER_STUFF]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kOtherStuff"]]; 


    return injected; 

} 

Iは、電源に接続することができる異なる値の束を持って、その代わりにハードコーディング[通話キー]、私は動的にそこにあるものを読み、値を注入したいです。だから、最初のURLがかもしれ

ます。http:\\ www.domain.com ID = [kUsername] &記録= [kCallKey] &他の何らかの\ = [kOtherStuff]

ので、私は[]を見つける文字列をループし、内部にあるものに置き換えることができます。私は、文字列を検索し、見つける[文字を、次にアップにその文字列をコピーする]にはどうすればよい

し、[そうとなど次に進んで?

+0

をお試しください。 http://stackoverflow.com/questions/27880650/swift-extract-regex-matches –

+2

[Regex in Objective-C:マッチをダイナミックテンプレートに置き換える方法は?]を参照してください。 /stackoverflow.com/a/22458626/3832970)。あなたの場合の正規表現は '@" \\((。+?)] "'または '@" \\((^ [\\] \\ [] +)] "'です。 –

答えて

0

私はこのようにして同様の問題を解決しました。辞書にはキーワードをキーとして保存し、置換するキーワードと値を置換する文字列を使用しました。

NSDictionary * stringsReplacedByDictionary = NSDictionary *dictionary = @{ 
@"kUserName" : Username, 
@"kCallKey" : CallKey, 
@"kOtherStuff" : otherStuff 
}; 

// Enumerate the Dictionary containg all Strings to be replaced. 
for (NSString *keys in stringsReplacedByDictionary) { 

// Here I builded the search string in your case would be [kUsername] 
NSString *keysWithParenthesis = [NSString stringWithFormat:@"[$%@]", keys]; 
NSString *value = [stringsReplacedByDictionary objectForKey:keys]; 


//Check if there is a string to replace! 
if ([stringToScan containsString:keysWithParenthesis]) { 
NSRange replacingStringRange = [[textStorage string] rangeOfString:keysWithParenthesis]; 
[textStorage replaceCharactersInRange:replacingStringRange withString:value]; 
     } 
0

私は正規表現を使用することになり、この

NSString *url = @"http:\\www.domain.com\\id=[USER_ID]&record=[CALL_KEY]&someother=[OTHER_STUFF]"; 
    NSArray *values = @[@"one", @"two", @"three"]; 
    NSString *pattern = @"\\[([^\\]\\[]+)]"; 

    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil]; 
    NSInteger count = [regex matchesInString:url options:0 range:NSMakeRange(0, url.length)].count; 

    if (count == 0 || count != values.count) { 
     // handle error .. 
    } 

    NSTextCheckingResult *result = [regex firstMatchInString:url options:0 range:NSMakeRange(0, url.length)]; 

    for (int i = 0; i < values.count; i++) { 
     url = [url stringByReplacingCharactersInRange:result.range withString:values[i]]; 
     result = [regex firstMatchInString:url options:0 range:NSMakeRange(0, url.length)]; 

     if (result.range.location == NSNotFound) { // optional 
      break; 
     } 
    } 

    NSLog(@"%@", url); 
関連する問題