好的,所以我对 Objective C 还很陌生,我正在构建一个索引类,在该类中我有一个方法可以将书中的单词添加到 _ListofPtrsToUniqueWords 中,uniqueWord 是我为存储单词而构建的另一个类正在编目,在 C++ 中,我的方法将 UniqueWord 添加到一个唯一单词数组,如果它已经存在,它会跳过这个单词,但它会在 _CurrentLineArray 列表中添加一个行号,这是我在 C++ 上使用的方法
/* Attempts to add a word to the concordance. If it already exists, the existing entry is updated with the new line. */
void Concordance::add(const string word, const int line)
{
int insertion, index;
UniqueWord *uw = new UniqueWord(word, line);//creates the uniqueWord object
insertion = newIndex(*uw, index);//figures out where my word belongs in my array to be alphabetized
if (insertion == -1)
{
// The word already exists - add a line number.
delete uw;
ListOfUniqueWordsPtrs[index]->addLine(line);//I'm trying to do this in Objective C.
//My friend recommended i do this but i never asks what exactly does it do, addLine is a Unique word method
}
else
{
ListOfUniqueWordsPtrs.insert(ListOfUniqueWordsPtrs.begin() + insertion, uw);
}
}
现在我正尝试在 Objective C 中做同样的事情,但我不明白这个符号“->”的作用,我的 friend 只是建议我这样做,但我不明白它的作用以及我该如何做在 Objective C 中实现这一点
-(void) add:(NSString *)currentWordBeingCatalog and:(NSNumber*)CurrentLineNumber{
NSInteger insertion, index=0;
UniqueWord *CurrentWord=[[UniqueWord alloc] initWithString:currentWordBeingCatalog andline:CurrentLineNumber];
insertion=[self NewIndexToFindOutIf:CurrentWord is:[NSNumber numberWithLong:index]];
if(insertion==-1){
/*If the word already exist, it would delete the word and add the line number to the _linenumber NSMutableArray*/
CurrentWord=NULL;
[_ArrayOfPtrsToUniqueWords objectAtIndex:index]->[CurrentWord addALineNumberToCurrentLineNumberArray:CurrentLineNumber]];//This is where i'm trying to figure out what to do
}else{
[_ArrayOfPtrsToUniqueWords insertObject:CurrentWord atIndex:(0+insertion)];
}
}
我希望我能给你们足够的关于这段代码的信息,谢谢
最佳答案
这是您正在寻找的语法:
[[_ArrayOfPtrsToUniqueWords objectAtIndex:index] addALineNumberToCurrentLineNumberArray:CurrentLineNumber];
...也就是说,您要求数组返回其元素之一,然后要求该元素添加行号。
如果你愿意,你可以这样分解它:
UniqueWord *existingWord = [_ArrayOfPtrsToUniqueWords objectAtIndex:index];
[existingWord addALineNumberToCurrentLineNumberArray:CurrentLineNumber];
您还可以直接在 NSArray
和 NSMutableArray
对象上使用数组下标,所以如果 _ArrayOfPtrsToUniqueWords
是一个 NSMutableArray
,你可以这样做:
UniqueWord *existingWord = _ArrayOfPtrsToUniqueWords[index];
[existingWord addALineNumberToCurrentLineNumberArray:CurrentLineNumber];
或者这个:
[_ArrayOfPtrsToUniqueWords[index] addALineNumberToCurrentLineNumberArray:CurrentLineNumber];
关于c++ - 在 Objective C 中添加 "->",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987104/