【发布时间】:2013-11-28 00:30:38
【问题描述】:
好的,所以我对目标 c 还很陌生,我正在构建一个索引类,在该类中,我有一个方法可以将书中的单词添加到 aa _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 中做同样的事情,但我不明白这个符号的作用“->”,我的朋友只是建议我这样做,但我不明白它的作用以及我该怎么做在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]]你不想要->。您通过objectAtIndex获取对象,然后通过在其周围添加另一组[]并添加消息名称来调用该对象。以[[UniqueWord alloc] initWithString...为例。
标签: c++ objective-c class object methods