2011-06-02 44 views
0

我想我只是在iOS 4.3文檔中找到一個不準確的地方,更具體地說在NSString的頁面上。NSString文檔是否過時/錯誤?

我試圖使用stringByReplacingOccurrencesOfString:withString:方法,它是clearly in the documentation

error

錯誤的文本(因爲它是一個有點小)

然而,當我試圖使用一隻股票 NSString初始化這種方法,我用一個漂亮的錯誤迎接

'NSString' may not respond to '-replaceOccurrencesOfString:withString:'

warning: Semantic Issue: Method '-replaceOccurrencesOfString:withString:' not found (return type defaults to 'id')

出現上述錯誤的位置:

if ([elementName isEqualToString:@"name"]) { 
    NSString *temp = [[NSString alloc] initWithFormat:@"%@", currentString]; 
    NSString *newTemp = [temp replaceOccurrencesOfString:@"\n" withString:@""]; // the error occurs here 
    [self.group.names addObject:temp]; 
} 

所以,問題是,是我的錯還是文檔錯了?

回答

6

也許你想要的是

stringByReplacingOccurencesOfString:withString: 

而不是

replaceOccurencesOfString:withString: 

別急......你說!

+0

d'oh ........... – esqew 2011-06-02 02:13:48

1

stringByReplacingOccurrencesOfString:withString:不等於replaceOccurrencesOfString:withString:

有點驚訝,你沒有注意到,在打字的問題。 ;)

3

這是-stringByReplacingOccurrencesOfString:withString:而不是-replaceOccurrencesOfString:withString:正如你在你的代碼片段。

replaceOccurencesOfString:withString:在NSMutableString上的一個方法,它將替換字符串(因爲它是可變的)。也許你正在查看錯誤的文檔頁面,或者XCode給你一個錯誤的代碼完成建議。

0

的方法被稱爲 'stringByReplacingOccurrencesOfString:withString:',而不是 'replaceOccurrencesOfString:withString:'

0

你有replaceOccurrencesOfString:withString:在你的代碼,而不是您引用的函數,stringByReplacingOccurrencesOfString:withString:

(你錯過在stringBy部分)

2

您得到了這樣的回答已經5次,但在這裏就是爲什麼你得到錯誤:

replaceOccurrencesOfString:withString:是的方法。方法名稱的verbage表示它在原地運行(在接收器上)。

相反,stringByReplacingOccurrencesofString:withString:確實不是在接收器上運行,因此存在於NSString上。由於變量聲明爲NSString,所以不能使用replaceOccurrencesOfString:withString:方法,因爲這隻適用於可變字符串。