判断字符串是否相等 isEqualToString:

// if((btn.currentTitle == answerBtn.currentTitle) && btn.hidden == YES)
// 字符串相等比较 不要直接比,这样比的是指针,不是指针指向的数据
if([btn.currentTitle isEqualToString:answerBtn.currentTitle] && btn.hidden == YES)

 

 

 

 

NSString *strA = [NSString stringWithFormat:@"a"];

NSString *strB = [NSString stringWithFormat:@"b"];

if( strA == strB)

    NSLog(@"A is equal to B");

    else

    NSLog(@"A is not equal to B");

 

运行这段code, 在console 上的输出是: A is not equal to B

 

代码做些改动, 将 strA 与strB 设为相等。

NSString *strA = [NSString stringWithFormat:@"a"];

NSString *strB = [NSString stringWithFormat:@"a"];

if( strA == strB)

    NSLog(@"A is equal to B");

    else

    NSLog(@"A is not equal to B");

 

运行这段code ,在console上的输出仍然是 A is not equal to B 。

这是为什么呢 ?问题出在 字符串对比的语句上。

 

if ( strA == strB) // 这个strA, strB 是指针, 虽然字符串的内容是相同的, 但指向字符串的 指针肯定是不同的, 也不能相同啊。 (为了更好地理解字符串,需要弄清楚 指针的概念。 内存的分配。 )

 

    // 错误:if( strA == strB)

    // 正确: if ([strA isEqualToString:strB]) 

iOS SDK 本身 也提供了 字符串对比的方法: isEqualToString:

转载于:https://www.cnblogs.com/ERICSUN12/p/5344449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值