0

Ok so I'm fairly new to objective c and i'm building a concordance class, inside the class i got a method which adds a word from a book into a a _ListofPtrsToUniqueWords, uniqueWord is another class i built for storing the word being cataloged, well in c++ my method Adds the UniqueWord to an Array of Unique Words, if it already exist its skips the word but it add a line number to the _CurrentLineArray list, this is the method i used on 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);
    }
}

Now i'm trying to do the same in Objective C but i don't understand what this symbol does "->", my friend just recommended i do this but i don't understand what it does and how can i implament this in 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)];
 }

}

I hope i give you guys enough information on this code, thank you

4

2 回答 2

2

这是您正在寻找的语法:

[[_ArrayOfPtrsToUniqueWords objectAtIndex:index] addALineNumberToCurrentLineNumberArray:CurrentLineNumber];

...也就是说,您要求数组返回其元素之一,然后要求该元素添加行号。

如果您愿意,可以这样分解:

UniqueWord *existingWord = [_ArrayOfPtrsToUniqueWords objectAtIndex:index];
[existingWord addALineNumberToCurrentLineNumberArray:CurrentLineNumber];

你也可以直接在NSArrayNSMutableArray对象上使用数组下标,所以如果_ArrayOfPtrsToUniqueWords是一个NSMutableArray,你可以这样做:

UniqueWord *existingWord = _ArrayOfPtrsToUniqueWords[index];
[existingWord addALineNumberToCurrentLineNumberArray:CurrentLineNumber];

或这个:

[_ArrayOfPtrsToUniqueWords[index] addALineNumberToCurrentLineNumberArray:CurrentLineNumber];
于 2013-11-14T20:08:27.893 回答
2

它在 Objective-C 中的->作用类似于它在 C++ 中的作用,它被放置在指向对象的指针之后以访问它的实例变量(在 C++ 中它也用于调用方法,而在 Objective-C 中使用[instance methodName]语法) . 然而,属性通常比->Objective-C 中的符号更受欢迎。它在您的代码中的使用似乎是错误的,rob mayoff 在纠正它方面做得很好。

于 2013-11-14T20:04:34.980 回答