2011-06-03 42 views
0
NSString *locations=[[NSString alloc] init]; 
    locations=[mainView getLocationText]; 
    NSMutableArray *cs=[NSMutableArray arrayWithArray:[locations componentsSeparatedByString:@","]]; 
    NSString *city=[[cs objectAtIndex:0] replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[[cs objectAtIndex:0] length])]; 
    NSString *state=[[cs objectAtIndex:1] replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[[cs objectAtIndex:0] length])]; 
    NSMutableString *otherParams = [[[NSMutableString alloc] initWithFormat:@"city=%@&state=%@&beds=%@&baths=%@&minprice=%@&maxprice=%@&searchType=%@&page=1",city,state,bedsVal,bathsVal,minPriceVal,maxPriceVal,propTypeVal] autorelease];  

它给人的错误取代:在字与字之间除去空间和“+”

Attempt to mutate immutable object with replaceOccurrencesOfString:withString:options:range:'

和空格不与“+”

回答

3

我最初的答案是不正确的更换 ,你应该(根据Jasarien的回答)使用stringByReplacingOccurrencesOfString:withString:options:range。 而不是replaceOccurrencesOfString:withString:options:range

+2

这是无关紧要的 - 提问者使用'[NSMutableArray arrayWithArray];'创建可变数组。将不可变数组传递给此方法会返回一个新的可变数组,其中包含传入数组的内容。 – Jasarien 2011-06-03 09:38:26

+0

+1 Jasarien,你是对的 - 我得到了一个错误没有我:)我已经改变了我的答案,以反映你的正确答案 – theiOSDude 2011-06-03 09:58:54

2

NSString是不可变的。 replaceOccurrencesOfString:withString:options:range:仅适用于NSMutableString

,你应该使用的方法是:

stringByReplacingOccurrencesOfString:withString:options:range:

这是不可变的字符串来实现,即NSString将返回与您指定的更换一个新的字符串。