SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Downloaden Sie, um offline zu lesen
Unit 3—Lesson 3:
Guard
func singHappyBirthday() {
if birthdayIsToday {
if invitedGuests > 0 {
if cakeCandlesLit {
print("Happy Birthday to you!")
} else {
print("The cake candle's haven't been lit.")
}
} else {
print("It's just a family party.")
}
} else {
print("No one has a birthday today.")
}
}
func singHappyBirthday() {
guard birthdayIsToday else {
print("No one has a birthday today.")
return
}
guard invitedGuests > 0 else {
print("It's just a family party.")
return
}
guard cakeCandlesLit else {
print("The cake's candles haven't been lit.")
return
}
print("Happy Birthday to you!")
}
guard
guard condition else {
//false: execute some code
}
//true: execute some code
guard
func divide(_ number: Double, by divisor: Double) {
if divisor != 0.0 {
let result = number / divisor
print(result)
}
}
func divide(_ number: Double, by divisor: Double) {
guard divisor != 0.0 else { return }
let result = number / divisor
print(result)
}
guard with optionals
if let eggs = goose.eggs {
print("The goose laid (eggs.count) eggs.")
}
//`eggs` is not accessible here
guard let eggs = goose.eggs else { return }
//`eggs` is accessible hereafter
print("The goose laid (eggs.count) eggs.")
guard with optionals
func processBook(title: String?, price: Double?, pages: Int?) {
if let theTitle = title, let thePrice = price, let thePages = pages {
print("(theTitle) costs $(thePrice) and has (thePages) pages.")
}
}
func processBook(title: String?, price: Double?, pages: Int?) {
guard let theTitle = title, let thePrice = price, let thePages = pages else { return }
print("(theTitle) costs $(thePrice) and has (thePages) pages.")
}
Lab: Guard
Unit 3—Lesson 3
Open and complete the exercises in Lab - Guard.playground
© 2017 Apple Inc. 

This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

Weitere ähnliche Inhalte

Mehr von SV.CO (20)

Segues and navigation controllers
Segues and navigation controllersSegues and navigation controllers
Segues and navigation controllers
 
Camera And Email
Camera And EmailCamera And Email
Camera And Email
 
Scroll views
Scroll viewsScroll views
Scroll views
 
Intermediate table views
Intermediate table viewsIntermediate table views
Intermediate table views
 
Table views
Table viewsTable views
Table views
 
Closures
ClosuresClosures
Closures
 
Protocols
ProtocolsProtocols
Protocols
 
App anatomy and life cycle
App anatomy and life cycleApp anatomy and life cycle
App anatomy and life cycle
 
Extensions
ExtensionsExtensions
Extensions
 
Gestures
GesturesGestures
Gestures
 
View controller life cycle
View controller life cycleView controller life cycle
View controller life cycle
 
Controls in action
Controls in actionControls in action
Controls in action
 
Auto layout and stack views
Auto layout and stack viewsAuto layout and stack views
Auto layout and stack views
 
Custom view
Custom viewCustom view
Custom view
 
Tab bar controllers
Tab bar controllersTab bar controllers
Tab bar controllers
 
Displaying data
Displaying dataDisplaying data
Displaying data
 
Loops
LoopsLoops
Loops
 
Enumerations
EnumerationsEnumerations
Enumerations
 
Collections
CollectionsCollections
Collections
 
Scope
ScopeScope
Scope
 

Kürzlich hochgeladen

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Kürzlich hochgeladen (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Guard

  • 2. func singHappyBirthday() { if birthdayIsToday { if invitedGuests > 0 { if cakeCandlesLit { print("Happy Birthday to you!") } else { print("The cake candle's haven't been lit.") } } else { print("It's just a family party.") } } else { print("No one has a birthday today.") } }
  • 3. func singHappyBirthday() { guard birthdayIsToday else { print("No one has a birthday today.") return } guard invitedGuests > 0 else { print("It's just a family party.") return } guard cakeCandlesLit else { print("The cake's candles haven't been lit.") return } print("Happy Birthday to you!") }
  • 4. guard guard condition else { //false: execute some code } //true: execute some code
  • 5. guard func divide(_ number: Double, by divisor: Double) { if divisor != 0.0 { let result = number / divisor print(result) } } func divide(_ number: Double, by divisor: Double) { guard divisor != 0.0 else { return } let result = number / divisor print(result) }
  • 6. guard with optionals if let eggs = goose.eggs { print("The goose laid (eggs.count) eggs.") } //`eggs` is not accessible here guard let eggs = goose.eggs else { return } //`eggs` is accessible hereafter print("The goose laid (eggs.count) eggs.")
  • 7. guard with optionals func processBook(title: String?, price: Double?, pages: Int?) { if let theTitle = title, let thePrice = price, let thePages = pages { print("(theTitle) costs $(thePrice) and has (thePages) pages.") } } func processBook(title: String?, price: Double?, pages: Int?) { guard let theTitle = title, let thePrice = price, let thePages = pages else { return } print("(theTitle) costs $(thePrice) and has (thePages) pages.") }
  • 8. Lab: Guard Unit 3—Lesson 3 Open and complete the exercises in Lab - Guard.playground
  • 9. © 2017 Apple Inc. This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.