2011-06-02 176 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:方法,因为这只适用于可变字符串。