Operators in Objective C and Swift

Common for Objective C and Swift
  • Assignment Operator ( = )
  • Arithmetic Operator
    • addition +,
    • Subtraction -, 
    • Multiplication *,  
    • Division /
  • Remainder (Modulo) Operator %
  • Unary plus operator (+), Unary minus operator (-), unary operator is prepended directly on the value it operates on.
  • compound assignment/short hand operators (x+=y, x-=y, x*=y, x/=y, x%=y, x&=y, x|=y, x^=y)
  • Increment (++) & decrement operator (—) {int x=9, int y: y= ++x , y = - - x } (Removed in Swift 3)
  • Relational/Comparison operators 
    • Equal to (a == b)
    • Not equal to (a != b)
    • Greater than (a > b)
    • Less than (a < b)
    • Greater than or equal to (a >= b)
    • Less than or equal to (a <=  b)
  • Logical Operator 
    • Logical Not (!a)
    • Logical AND (a && b)
    • Logical OR (a || b)
  • Ternary Conditional Operator ( if condition? expression 1 : expression 2 )
Swift Only
  • Range Operator 
    • Closed Range Operator '1…10', includes 1 and 10 in this range.
    • Half Open Range Operator '1..~10 , includes 1 but not 10
  • Nil-Coalescing Operator (x??y)
The nil-coalescing operator (x ?? y) unwraps an optional x if it contains a value, or returns a default value y if x is nil. The expression a is always of an optional type. The expression y must match the type that is stored inside x.

The nil Coalescing operator is shorthand for the below code:

x != nil ? x! : y

The code above uses the ternary conditional operator and forced unwrapping (x!) to access the value wrapped inside a when x is not nil, and to return x otherwise. The nil-coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form.

1 comment:

  1. Right,Good to see these helpful information here iPhone Apps Development Company.Thanks lots for sharing them with us.

    ReplyDelete