Swift Loops & Animations: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Loops über verschiedene Objekte == Swift kann nicht nur über Arrays Loopen sondern auch über Strings <syntaxhighlight lang="swift"> let titleText…“)
 
Zeile 1: Zeile 1:
== Loops über verschiedene Objekte ==
+
== Einführung - Loops über verschiedene Objekte ==
 
Swift kann nicht nur über Arrays Loopen sondern auch über Strings
 
Swift kann nicht nur über Arrays Loopen sondern auch über Strings
 
<syntaxhighlight lang="swift">
 
<syntaxhighlight lang="swift">
Zeile 6: Zeile 6:
 
             print(letter)
 
             print(letter)
 
         }
 
         }
 +
</syntaxhighlight>
 +
 +
== For in Loop ==
 +
 +
<syntaxhighlight lang="swift">
 +
let names = ["Stephan", "Yvonne", "Finn", "Damon"]
 +
let fruits: Set = ["Apple", "Pear", "Orange", "Banana"]
 +
let contacts = ["John":123, "Eva":456, "Adam":789]
 +
let word = "Donaudampfschifffahrtsgesellschaftskapitän"
 +
let halfOpenRange = 1..<5
 +
let closedRange = 1...5
 +
 +
// for in loop
 +
 +
// loop through an array
 +
for name in names { print("Hello, \(name)") }
 +
// range
 +
for number in 1...5{ print(number) }
 +
// look no var
 +
for _ in 1...5 { print ("hello") }
 +
// Sets doesn't guarantee order. But they are fast
 +
for fruit in fruits{ print("I like \(fruit)") }
 +
// Collections have keys and values
 +
for person in contacts{ print("\(person.key)'s number is \(person.value)") }
 +
// Strings can be looped as characters
 +
for char in word { print(char) }
 +
</syntaxhighlight>
 +
 +
=== Loop mit Effekt ===
 +
Mit einem Timer kann man sogar eine Animation in einem Textfeld erzeugen:
 +
<syntaxhighlight lang="swift">
 +
        titleLabel.text = ""
 +
        let titleText = "⚡️FlashChat"
 +
        var charIndex = 0.0
 +
        for letter in titleText {
 +
            Timer.scheduledTimer(withTimeInterval: 0.1 * charIndex, repeats: false){(myTimer) in
 +
                self.titleLabel.text?.append(letter)
 +
            }
 +
            charIndex += 1
 +
        }
 +
</syntaxhighlight>
 +
 +
== while loop ==
 +
<syntaxhighlight lang="swift">
 +
while somethingIsTrue {
 +
  // do something
 +
}
 +
</syntaxhighlight>
 +
 +
or do while - which will do the loop at least one time
 +
<syntaxhighlight lang="swift">
 +
do {
 +
  // do something
 +
} while somethingIsTrue
 +
 +
 +
<syntaxhighlight lang="swift">
 +
// while loop - careful - do not create infitinte loops ;)
 +
 +
var now = Date().timeIntervalSince1970
 +
let oneSecondFromNow = Date().timeIntervalSince1970 + 1
 +
while now < oneSecondFromNow {
 +
    now = Date().timeIntervalSince1970
 +
    print("waiting...")
 +
}
 +
 +
// fibonacci row
 +
func fibonacci(n: Int) {
 +
    if n < 2 { print("n has to be 2 at least"); return; }
 +
    var last = 0
 +
    var current = 1
 +
    var fibonaccis: [Int] = [last,current]
 +
 +
    for _ in 2..<n {
 +
        let next = last + current
 +
        fibonaccis.append(last + current)
 +
        last = current
 +
        current = next
 +
    }
 +
    print (fibonaccis)
 +
}
 +
fibonacci(n: 10)
 
</syntaxhighlight>
 
</syntaxhighlight>

Version vom 22. Januar 2023, 19:49 Uhr

Einführung - Loops über verschiedene Objekte

Swift kann nicht nur über Arrays Loopen sondern auch über Strings

        let titleText = "⚡️FlashChat"
        for letter in titleText {
            print(letter)
        }

For in Loop

let names = ["Stephan", "Yvonne", "Finn", "Damon"]
let fruits: Set = ["Apple", "Pear", "Orange", "Banana"]
let contacts = ["John":123, "Eva":456, "Adam":789]
let word = "Donaudampfschifffahrtsgesellschaftskapitän"
let halfOpenRange = 1..<5
let closedRange = 1...5

// for in loop

// loop through an array
for name in names { print("Hello, \(name)") }
// range
for number in 1...5{ print(number) }
// look no var
for _ in 1...5 { print ("hello") }
// Sets doesn't guarantee order. But they are fast
for fruit in fruits{ print("I like \(fruit)") }
// Collections have keys and values
for person in contacts{ print("\(person.key)'s number is \(person.value)") }
// Strings can be looped as characters
for char in word { print(char) }

Loop mit Effekt

Mit einem Timer kann man sogar eine Animation in einem Textfeld erzeugen:

        titleLabel.text = ""
        let titleText = "⚡️FlashChat"
        var charIndex = 0.0
        for letter in titleText {
            Timer.scheduledTimer(withTimeInterval: 0.1 * charIndex, repeats: false){(myTimer) in
                self.titleLabel.text?.append(letter)
            }
            charIndex += 1
        }

while loop

while somethingIsTrue {
  // do something
}

or do while - which will do the loop at least one time

do {
  // do something
} while somethingIsTrue


<syntaxhighlight lang="swift">
// while loop - careful - do not create infitinte loops ;)

var now = Date().timeIntervalSince1970
let oneSecondFromNow = Date().timeIntervalSince1970 + 1
while now < oneSecondFromNow {
    now = Date().timeIntervalSince1970
    print("waiting...")
}

// fibonacci row
func fibonacci(n: Int) {
    if n < 2 { print("n has to be 2 at least"); return; }
    var last = 0
    var current = 1
    var fibonaccis: [Int] = [last,current]

    for _ in 2..<n {
        let next = last + current
        fibonaccis.append(last + current)
        last = current
        current = next
    }
    print (fibonaccis)
}
fibonacci(n: 10)