++
operators and --
. At first glance, this angered me, but, in fact, in modern Swift, they are simply not needed. The need for increments is eliminated thanks to a new for
loop and higher order functions like map
or filter
. Of course, you can still implement the increment yourself, but in a different way. func plusPlus() { var i = 0 i++ // : '++' : Swift 3 i += 1 // '+=' '-=' // succesor() predecessor() let alphabet = "abcdefghijklmnopqrstuvwxyz" let index = alphabet.characters.indexOf("u") if let index = index { alphabet.characters[index.successor()] } }
for init; comparison; increment {}
for init; comparison; increment {}
for init; comparison; increment {}
, we can also easily remove ++
and --
. But do not worry - there is an excellent and very convenient for-in
loop in Swift. func forLoop() { // : C- for // Swift for var i = 1; i <= 10; i += 1 { print("I'm number \(i)") } // Swift : for i in 1...10 { print("I'm number \(i)") } }
removeFirst()
function removeFirst()
. You could easily write it yourself as an extension, but now it is provided out of the box. The function is very smart, and, even though your array may be filled with options, it simply deletes the first element, and does not leave it as nil
nil
. Love. func slices() { var array = ["First", "Second", "Third", "Fourth"] array.removeLast() // , … array.removeFirst() // ( n- n-1) }
Equatable
elements Equatable
Equatable
. func tuples() { let tuple1 = (1, true, 3, 4, 5, "a") let tuple2 = (1, false, 3, 4, 5, "a") let equalTuples = tuple1 == tuple2 print(equalTuples) // false }
// : // Swift; func noMoreCurryingThisWay(a: Int)(b: Int) -> Int { return a + b } func curryThisWay(a: Int) -> (Int) -> Int { return { (b: Int) -> Int in return a + b } }
func swiftVer() { var cuteNumber = 7 #if swift(>=2.2) print("Good morning, I'm brand new Swift") cuteNumber += 1 #else print("Hi, I'm elder than my brother, but can ++") cuteNumber++ #endif // , '#if swift' Obj-C, // - '#available'. '#'. // , , : /* if #swift(>=2.2) { } else { } */ }
#selector
was selected #selector
#selector
and not Selector()
Selector()
that reminds instantiation (and also undesirable). In fact, this option is still better than just a string, thanks to autocompletion; the probability of a typo is minimal. func selector() { let button = UIButton() // : Objective-C // ; '#selector' button.addTarget(self, action: "forLoop", forControlEvents: .TouchUpInside) // '#selector' button.addTarget(self, action: #selector(forLoop), forControlEvents: .TouchUpInside) }
Source: https://habr.com/ru/post/277857/
All Articles