如果這個工具幫到了你,可以請作者喝杯咖啡 ☕
Objective-C運算子優先順序與結合性速查對照表
[]tools.level-oc-query.categories.primarytools.level-oc-query.associativity.left用于数组或集合下标访问;在 Objective-C 中方括号也用于向对象发送消息。
items[index] / [view setNeedsLayout]()tools.level-oc-query.categories.primarytools.level-oc-query.associativity.left改变表达式求值顺序,或调用 C 函数、Block、方法返回的函数指针。
(a + b) * c / NSLog(@"%@", text).tools.level-oc-query.categories.primarytools.level-oc-query.associativity.left访问结构体成员,也常用于 Objective-C 属性的点语法。
point.x / object.title->tools.level-oc-query.categories.primarytools.level-oc-query.associativity.left通过结构体指针访问成员。
node->next+atools.level-oc-query.categories.unarytools.level-oc-query.associativity.right保留数值本身,通常用于明确表达正值。
+count-atools.level-oc-query.categories.unarytools.level-oc-query.associativity.right返回操作数的相反数。
-offset++a / a++tools.level-oc-query.categories.unarytools.level-oc-query.associativity.right将变量加 1;前置形式先递增再取值,后置形式先取值再递增。
++index / index++--a / a--tools.level-oc-query.categories.unarytools.level-oc-query.associativity.right将变量减 1;前置形式先递减再取值,后置形式先取值再递减。
--index / index--(type)tools.level-oc-query.categories.unarytools.level-oc-query.associativity.right把表达式显式转换为指定 C 或 Objective-C 指针类型。
(UIView *)sender*atools.level-oc-query.categories.unarytools.level-oc-query.associativity.right读取指针指向位置的值。
*buffer&atools.level-oc-query.categories.unarytools.level-oc-query.associativity.right取得变量、结构体成员或对象指针变量的地址。
&error!tools.level-oc-query.categories.unarytools.level-oc-query.associativity.right对布尔或条件表达式取反。
!isHidden~tools.level-oc-query.categories.unarytools.level-oc-query.associativity.right对整数每一位取反。
~masksizeoftools.level-oc-query.categories.unarytools.level-oc-query.associativity.right返回类型或表达式占用的字节数,结果类型为 size_t。
sizeof(CGPoint)*tools.level-oc-query.categories.multiplicativetools.level-oc-query.associativity.left两个数值相乘。
width * scale/tools.level-oc-query.categories.multiplicativetools.level-oc-query.associativity.left执行数值除法;整数除法会截断小数部分。
total / count%tools.level-oc-query.categories.multiplicativetools.level-oc-query.associativity.left返回整数相除后的余数。
index % 2+tools.level-oc-query.categories.additivetools.level-oc-query.associativity.left数值相加,也可用于指针与整数的偏移。
x + y-tools.level-oc-query.categories.additivetools.level-oc-query.associativity.left数值相减,或两个指针相减得到元素距离。
max - min<<tools.level-oc-query.categories.shifttools.level-oc-query.associativity.left将整数二进制位向左移动指定数量。
1 << bit>>tools.level-oc-query.categories.shifttools.level-oc-query.associativity.left将整数二进制位向右移动指定数量。
flags >> 1>tools.level-oc-query.categories.relationaltools.level-oc-query.associativity.left判断左操作数是否大于右操作数。
count > limit>=tools.level-oc-query.categories.relationaltools.level-oc-query.associativity.left判断左操作数是否大于或等于右操作数。
count >= minimum<tools.level-oc-query.categories.relationaltools.level-oc-query.associativity.left判断左操作数是否小于右操作数。
index < total<=tools.level-oc-query.categories.relationaltools.level-oc-query.associativity.left判断左操作数是否小于或等于右操作数。
index <= lastIndex==tools.level-oc-query.categories.equalitytools.level-oc-query.associativity.left比较两个值是否相等;对象内容比较通常应使用 isEqual: 等方法。
status == 200!=tools.level-oc-query.categories.equalitytools.level-oc-query.associativity.left比较两个值是否不相等。
object != nil&tools.level-oc-query.categories.bitwisetools.level-oc-query.associativity.left对两个整数逐位执行与运算。
options & NSCaseInsensitiveSearch^tools.level-oc-query.categories.bitwisetools.level-oc-query.associativity.left对两个整数逐位执行异或运算。
mask ^ flag|tools.level-oc-query.categories.bitwisetools.level-oc-query.associativity.left对两个整数逐位执行或运算,常用于组合选项位。
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight&&tools.level-oc-query.categories.logicaltools.level-oc-query.associativity.left短路与;左侧为假时不再计算右侧表达式。
view != nil && !view.hidden||tools.level-oc-query.categories.logicaltools.level-oc-query.associativity.left短路或;左侧为真时不再计算右侧表达式。
cached || [self reloadData]?:tools.level-oc-query.categories.conditionaltools.level-oc-query.associativity.right根据条件在两个表达式之间选择一个结果。
title.length ? title : @"Untitled"=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right把右侧表达式结果写入左侧变量或属性。
self.title = title/=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right将左值除以右侧表达式后再赋回左值。
scale /= 2*=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right将左值乘以右侧表达式后再赋回左值。
height *= ratio%=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right将左值取余后再赋回左值。
index %= count+=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right将右侧表达式加到左值上并赋回。
x += delta-=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right从左值中减去右侧表达式并赋回。
remaining -= used<<=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right左移指定数量后赋回左值。
flags <<= 1>>=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right右移指定数量后赋回左值。
flags >>= 1&=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right执行按位与后赋回左值。
options &= allowed^=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right执行按位异或后赋回左值。
mask ^= flag|=tools.level-oc-query.categories.assignmenttools.level-oc-query.associativity.right执行按位或后赋回左值。
options |= UIViewAnimationOptionCurveEaseInOut,tools.level-oc-query.categories.commatools.level-oc-query.associativity.left从左到右依次求值多个表达式,整个表达式结果为最后一个表达式的值。
i++, j--在Objective-C開發中,運算子的優先順序和結合性直接影響表示式求值結果,稍有疏忽便會導致難以排查的邏輯錯誤。本工具提供完整的Objective-C運算子執行順序對照表,清晰列出從最高到最低優先順序的所有運算子及其結合性,幫助開發者快速查閱、準確理解求值規則,避免因優先順序混淆引發的編碼問題。
* 優先順序高於加法 +,因此表示式 2 + 3 * 4 的結果為 14 而非 20;賦值運算子 = 為右結合,a = b = c 等價於 a = (b = c)。() 具有最高優先順序,可以強制改變表示式預設的求值順序,是明確計算意圖的最直接方式。+ 是左結合,因此 a + b + c 按 (a + b) + c 計算;而賦值運算子 = 是右結合,a = b = 0 會先將 0 賦給 b,再將結果賦給 a。本對照表僅涵蓋標準Objective-C運算子,可能不包含編譯器擴充套件或特殊平臺下的自定義運算子。在實際編碼中,即使熟悉優先順序規則,仍強烈建議在混合使用多種運算子時使用括號明確求值順序,提升程式碼可讀性和可維護性。本工具為純靜態內容展示,不收集任何用戶數據,無隱私風險。
熟練運用運算子優先順序是編寫無歧義程式碼的基礎。常見易錯點包括:邏輯與 && 優先順序高於邏輯或 ||;位與 & 優先順序低於相等比較 ==;條件運算子 ?: 優先順序較低且為右結合。在複合表示式中,編寫單元測試或使用靜態分析工具驗證求值結果,可以有效避免潛在的優先順序陷阱。