if语句Objective-C内部的评估(NSIntegers) [英] Evaluation (NSIntegers) inside if-statement Objective-C

查看:118
本文介绍了if语句Objective-C内部的评估(NSIntegers)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有疑问,为什么它可以正常工作:

I am in doubt, why this work correctly:

NSInteger row = indexPath.row;
NSInteger preloadTrigger = self.nodes.count - 20;
if (row >= preloadTrigger) {
    [self.loader loadNextposts];
}

这不是(只是跳过if语句):

And this does not (just skips the if-statement):

if (indexPath.row >= self.nodes.count - 20) {
    [self.loader loadNextposts];
}

self.nodes.count - 20的值为负时.

但是,当表达式的值为正数时,它总是可以正常工作.

However, when the value of the expression is positive, it works fine always.

一个非常奇怪的行为,因为我看不到两个表达式的语义差异.

A very strange behavior, as I cannot see semantic difference in two expressions.

更新: 因此,我决定对其进行测试:

Update: So, I decided to test it:

(lldb) po self.nodes.count - 20
18446744073709551601

(lldb) po preloadTrigger
-15

推荐答案

根据

According to Apple Docs, count property is a NSUIntegerin objective-C.

写时:

 NSInteger preloadTrigger = self.nodes.count - 20;

实际上,您正在将count强制转换为NSInteger对象,如果count不大于20,则其值可以为负.

In fact you are casting count to a NSInteger object, and can have a negative value if count isn't greater than 20.

但是当你写的时候:

(indexPath.row >= self.nodes.count - 20)

count是一个NSUInteger对象,从中减去20总是得出一个正数(顺便说一句,是一个很大的数).

count is a NSUInteger object, and subtract 20 from it will always lead to a positive number (a huge one by the way).

这篇关于if语句Objective-C内部的评估(NSIntegers)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆