2012-10-10 46 views
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; 
    } 
} 

我没有得到什么,2号线的目标C的意思。 ||另外,我没有得到substringToIndex的含义。程序员如何知道在我看到的substringFromIndex等文档中的所有不同方法中使用substringToIndex。有这么多。这是说索引中的字符串被计数,-1意味着它删除了一个字符串?苹果文档中的含义如何与删除角色有关?

+0

-1在这里用来减一self.display.text的长度,然后将结果(一个简单的整数)被用作参数传递给函数substringToIndex。如果你不明白这一点,你应该转而研究更多基本的编程资料,然后再继续担心NSString的工作原理。 – Merk

回答

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; 
    } 
} 
0

||是一个OR操作符。至少有一个陈述必须是真实的。

看看苹果的文档,以substringToIndex:方法

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

这是东西,你可以用谷歌搜索很容易找到。用的代码的解释提供

+0

感谢您回答我的问题trojanfoe,至于NSAddict,我知道这可以很容易地查找,我已经看过文档之前,我问了一个关于在这个网站有效的docuemntation的东西的问题。 – user1295568