所以我剛剛發現可以像這樣在 Applescript 中將字串相互比較:
"hello world" > "abc"
回傳:
true
但是,您也可以將字串與數字進行比較。然而,字串總是更大:
"a" = 10 ^ 308 -- Close to infinity
回傳:
false
有沒有辦法只比較數字而不使用try- 和 -on error陳述句?
uj5u.com熱心網友回復:
你的練習的意義是不可理解的。不清楚有什么用?AppleScript 完全按照開發人員希望的方式撰寫。例如,他們想:為什么不將字串與數字進行比較。您不能(并且不能)更改 AppleScript 邏輯操作的行為。但是您可以為它們撰寫“智能等價物”。并使用它們。
on isNumbersEqual(x, y)
tell {real, integer}
if x's class is not in it then error "LEFT OPERAND ISN'T NUMBER"
if y's class is not in it then error "RIGHT OPERAND ISN'T NUMBER"
end tell
x = y
end isNumbersEqual
-- isNumbersEqual("1", 10 ^ 308) --> ERROR!!
isNumbersEqual(1, 10 ^ 308) --> false
注意:與其他編程語言不同,AppleScript 在將字串與數字進行比較時不會拋出錯誤。相反,它隱式地將數字強制轉換為字串以執行合法的比較。你可以查看我的宣告:
("1" > 10 ^ 308) is ("1" > (10 ^ 308 as string)) --> true
uj5u.com熱心網友回復:
解決此問題的一個簡單方法是檢查您傳遞的變數或字串的類:
set x to "This is my innocent string"
if x > 0 and class of x ≠ string then --do stuff here.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/321790.html
上一篇:“i”和“Love”的嚴格弱序
