import UIKit
let encrouragement = "You can do it!"
var personalizedEncouragement = "You can do it, Lauren!"
personalizedEncouragement = personalizedEncouragement.stringByReplacingOccurrencesOfString("Lauren", withString:"Cameron")
let birthYear = 2008
var currentYear == 2017
var age = currentYear - birthYear
let buildingCapacity = 300
var attendance = 220
attendance += 2
var goat = UIImage(named:"Chinese-New-Year-3.jpg")!
let yearsOfTheGoat = [1967, 1979, 1991, 2003, 2015]
let yearsOfTheSheep = [1967, 1979, 1991, 2003, 2015]
yearsOfGoat.append(2027)
Category: Swift / SwiftUI
String
import UIKit import Foundation var funWithStrings = UIImage(named:"kittenWithOrangeString.jpg"): let myFirstString = "no " let mySecondString = "no problems" let theTruth = myFirstString + ", " + mySecondString var doggyDiet = "Lulu eats 25lbs of dog food per month" var dogName = "Ferris" var ferrisPic = UIImage(named:"SpringerdoodleFerris.jpg")
var lbsPerDay = 0.75 var daysPerMonth:Double = 30.0 doggyDiet = "\(dogName) eats \(lbsPerDay * daysPerMonth) of dog food per month" var frankiePic = UIImage(named:"frankie.jpeg")! lbsPerDay = 0.25 dogName = "Lil Frankie" doggyDiet = "\(dogName) eats \(lbsPerDay * daysPerMonth) of dog food per month"
access an array of characters
var password = "Meet me in St. Louis"
for character in password.characters {
if character == "e" {
print("found an e!")
} else {
}
}
if else
if hungry && !vegetarian {
println("Let's eat steak!")
} else if hungry && vegetarian {
println("How about pumpkin curry?")
} else {
println("nevermind")
}
var thereIsPie = true
if hungry || thereIsPie {
println("Let's eat!")
} else {
println("Let's wait.")
}
Ternary conditional
if question {
answer1
} else {
answer2
}
hugry ? println("Let's eat!") : println("Let's wait.")
hugry || thereIsPie ? println("Let's eat!") : println("Let's wait.")
Swift types
import UIKit
import Foundation
class LightSwitch {
var on: Bool = true
}
var livingRoomSwitch = LightSwitch()
livingRoomSwitch.on
var dollarSign: Character = "$" var myFirstSwiftString String = "no' money"
Optional and Tuples
let kitchenSwitch = LightSwitch()
let ticketPrice = 13.75
let swiftIsFast = true
let encouragement = “You can do it!”
let ticketPrice = 7.5
let allowance = 10.0
var iceCreamPrice = 3.0
var pic UIImage(named:"Choe.png")!
if allowance >= ticketPrice + iceCreamPrice {
println("Let's go to the movies!")
} else {
println("Let's watch a movie at home and eat ice cream")
}
var hungry = true
var vegetarian = false
if hungry {
println("Let's eat!")
} else {
println("Let's wait.")
}
Anatomy of a Constrain
Y = ax + b
stackView seen same screen
Compression Resistance
Label, TextField, Button, Slider, ImageView
View Properties
Intrinsic Content Size
Compression Resistance
Content Hugging
-stack view
axis, spacing, alignment, distribution
happily modifies size of view
-> fill(default), fill equally, fill proportionally
works hard not to modify size of views
->equal spacing, equal centering
alignment -horizontal stack views
text-> ascender, baseline, descender
guiding principles of autolayout
-stackView first, constraints later
-Start small, or not at all
-Work from the inside out
-Trust the simulator only
-Don’t panic
Seven Commandments of AutoLayout
1. Tweak the properties of the StackView
2. Use another StackView
3. Tweak the compression resistance or the hugging priority
4. Add constraints to the StackView
5. Add constraints to the views
6. To connect views within different StackViews, use V.
7. If everything else fails, use a filler view
AutoLayout
-Laying out the views
-Deciding how these views will adapt when the available screen size changes
import UIKit
class CodeViewController:
UIViewController {
override func loadView(){
code
}
}
-screen size changes
iphone se, iphone 6+
rotaion
code, xibs, storyboards
-Swift Code(NSLayoutConstraint)
-Visual Format Code
-Creating constraints in IB
UIStackView
building an App with x-code
create a project
add views
define layouts
integrate functions
connect ui to code
respond to events
function practice
func firstCharacter(of word: String) -> Character {
return word[word.startIndex]
}
firstCharacter(of: "Mom")
import Fundation
func placeFirstLetterLast(myString: String){
}
func placeFirstLetterLast (myString: String) -> String {
var myString = myString
myString.append(firstCharacter(of: myString))
return myString
}
func shortNameFromName(name: String) -> String {
let lowercaseName = name.lowercaseString
let vowelSet = CharacterSet(charactersIn: "aeiou")
}
shortNameFromName(name: "Nate")
//
shortNameFromName(name: "Talia")
//
func lyricsForName(lyricsTemplate: String, fullName: String) -> String {
let shortName = shortNameForName(name: fullName)
let lyrics = lyricsTemplate
.replacingOccurrences(of: "<FULL_NAME>", with: fullName)
.replacingOccurrences(of: "<SHORT_NAME>", with: shortName)
return lyrics
}
functions
The function print, min, max, and abs are a few examples of global functions. Check out a complete list of Swift’s global functions.
print("I'm a global function!")
var initialPrice = 50
var bestOffer = 45
var finalPrice = min(bestOffer, initialPrice)
Anatomy of a function
func functionName (_ parameterName: parameterType) -> returnType {
statements to execute
return object
}
func sumOfStrings(_ aBunchOfStrings: [String]) -> Int {
let array = aBunchOfStrings
var sum = 0
for string in array {
if Int(string) != nil {
let int intToAdd = Int(string)!
sum += intToAdd
}
}
return sum
}
func reverse(_ string: String) -> String {
var reversedString = ""
for character in string.characters {
reversedString = "\(character)" + reversedString
}
return reversedString
}
func warmUp(temperature: Int) -> Int {
return temperature + 10
}
func firstChar(word: String) -> Character {
return word[word.startIndex]
}
func concatenate(firstString: String, secondString secondString){
return firstString + secondString
}
func warmUp(temperature: Int) -> Int {
return temperature + 10
}
func firstCharacter(word: String) -> Character {
return word[word.startIndex]
}
func concatenate(firstString: String, secondString: String)-> String {
return firstString + secondString
}