Book Image

Swift Cookbook

By : Cecil Costa, Cecil Costa
Book Image

Swift Cookbook

By: Cecil Costa, Cecil Costa

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Simulating a home automation


Technology is getting more and more popular every day; soon we will control even our door locks from our mobile phones. However, how does it work? Imagine when you walk into your living room, a sensor can detect that there is someone there and it will turn on the light. Also, if a barometer detects that it is going to rain, it can close the home windows. The examples mentioned here are good samples of objects that change their status and tell other objects about this change. For this case, we will use the pattern Observer, also known as the publisher-subscriber pattern.

In this recipe, we will create a sample of a home with only two windows, a door lock, and a clothes line. To make it simple, the application will read the sensor information from a file.

When we receive information from the radar, it may be that someone is approaching your home. In this case, the door lock must be locked and the windows should be closed, or when this person goes away, the windows...