Tutorial by Examples

Swift supports the creation of custom operators. New operators are declared at a global level using the operator keyword. The operator's structure is defined by three parts: operand placement, precedence, and associativity. The prefix, infix and postfix modifiers are used to start an custom op...
As there is currently no simple way of combining dictionaries in Swift, it can be useful to overload the + and += operators in order to add this functionality using generics. // Combines two dictionaries together. If both dictionaries contain // the same key, the value of the right hand side dicti...
Let's add a custom operator to multiply a CGSize func *(lhs: CGFloat, rhs: CGSize) -> CGSize{ let height = lhs*rhs.height let width = lhs*rhs.width return CGSize(width: width, height: height) } Now this works let sizeA = CGSize(height:100, width:200) let sizeB = 1.1 * si...
Swift Bitwise operators allow you to perform operations on the binary form of numbers. You can specify a binary literal by prefixing the number with 0b, so for example 0b110 is equivalent to the binary number 110 (the decimal number 6). Each 1 or 0 is a bit in the number. Bitwise NOT ~: var number...
Overflow refers to what happens when an operation would result in a number that is either larger or smaller than the designated amount of bits for that number may hold. Due to the way binary arithmetic works, after a number becomes too large for its bits, the number overflows down to the smallest p...
Operators that bound tighter (higher precedence) are listed first. OperatorsPrecedence group (≥3.0)PrecedenceAssociativity.∞left?, !, ++, --, [], (), {}(postfix)!, ~, +, -, ++, --(prefix)~> (swift ≤2.3)255left<<, >>BitwiseShiftPrecedence160none*, /, %, &, &*MultiplicationPrec...

Page 1 of 1