iOS compare button title to string

后端 未结 4 1850
渐次进展
渐次进展 2020-12-11 18:18

I\'m just learning how to code so thanks for your patience on this simple question.

Here\'s my code:

- (IBAction)buttonWasPressed:(id)sender {  
            


        
相关标签:
4条回答
  • 2020-12-11 18:48

    The better way to compare string is:

    NSString *string1 = <your string>;
    NSString *string2 = <your string>;
    
    if ([string1 caseInsensitiveCompare:string2] == NSOrderedSame) {
        //strings are same
    } else {
        //strings are not same
    }
    
    0 讨论(0)
  • 2020-12-11 18:51

    I've found that with Xcode 8.3.1, it is necessary to do:

    if([self.myButton isEqual: @"My text"]) {
    //do this
    }
    
    0 讨论(0)
  • 2020-12-11 19:08

    Use -isEqualToString method:

    if ([buttonName isEqualToString:@"Button 1"])
       ...
    

    using == you compare ponters, not the actual string values they contain

    0 讨论(0)
  • 2020-12-11 19:10

    in objective-c you can't compare strings using "==", instead you should use the method isEqualToString from the NSString class to compare a string with another.

    if ([buttonName isEqualToString: @"Button 1"]) {
      // do something
    }
    
    0 讨论(0)
提交回复
热议问题