【问题标题】:Adding "->" in Objective C在 Objective C 中添加“->”
【发布时间】: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


【解决方案1】:

这是您要查找的语法:

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

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

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

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

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

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

或者这个:

[_ArrayOfPtrsToUniqueWords[index] addALineNumberToCurrentLineNumberArray:CurrentLineNumber];

【讨论】:

  • 谢谢,这是我正在寻找的确切解释。
【解决方案2】:

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

【讨论】:

  • 我会在他的评论中寻找来自@crashmtr 的示例吗?对不起,我真的不知道这个符号是做什么的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多