1

誰かがこのコードを説明できますか

- (IBAction)backspacePressed {
   self.display.text =[self.display.text substringToIndex:
                  [self.display.text length] - 1]; 

   if ( [self.display.text isEqualToString:@""]
      || [self.display.text isEqualToString:@"-"]) {

      self.display.text = @"0";
      self.userIsInTheMiddleOfEnteringNumber = NO;
   }
}

目的 c の 2 行の意味がわかりません。|| また、substringToIndex の意味がわかりません。プログラマーは、substringFromIndex などを見たドキュメントのさまざまなメソッドすべてから substringToIndex を使用することをどのように知っているのでしょうか。非常に多くあります。これは、インデックス内の文字列がカウントされ、-1 は文字列を削除することを意味するということですか? リンゴのドキュメントの意味は、文字の削除にどのように関連していますか?

4

2 に答える 2

1

コードの説明とともに提供されるコメント...

- (IBAction)backspacePressed
{
   // This is setting the contents of self.display (a UITextField I expect) to
   // its former string, less the last character.  It has a bug, in that what
   // happens if the field is empty and length == 0?  I don't think substringToIndex
   // will like being passed -1...
   self.display.text =[self.display.text substringToIndex:
                  [self.display.text length] - 1]; 

   // This tests if the (now modified) text is empty (better is to use the length
   // method) or just contains "-", and if so sets the text to "0", and sets some
   // other instance variable, the meaning of which is unknown without further code.
   if ( [self.display.text isEqualToString:@""]
      || [self.display.text isEqualToString:@"-"]) {

      self.display.text = @"0";
      self.userIsInTheMiddleOfEnteringNumber = NO;
   }
}
于 2012-10-11T11:29:17.573 に答える
0

|| is an OR operator. At least one of the statements has to be true.

Look at Apple's documentation for the substringToIndex: method

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

This is stuff you could find easily with a google search.

于 2012-10-11T11:24:38.733 に答える