Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering macOS Programming

You're reading from   Mastering macOS Programming Hands-on guide to macOS Sierra Application Development

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781786461698
Length 626 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Stuart Grimshaw Stuart Grimshaw
Author Profile Icon Stuart Grimshaw
Stuart Grimshaw
Gregory Casamento Gregory Casamento
Author Profile Icon Gregory Casamento
Gregory Casamento
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Hello macOS 2. Basic Swift FREE CHAPTER 3. Checking Out the Power of Xcode 4. MVC and Other Design Patterns 5. Advanced Swift 6. Cocoa Frameworks - The Backbone of Your Apps 7. Creating Views Programmatically 8. Strings and Text 9. Getting More from Interface Builder 10. Drawing on the Strength of Core Graphics 11. Core Animation 12. Handling Errors Gracefully 13. Persistent Storage 14. The Benefits of Core Data 15. Connect to the World - Networking 16. Concurrency and Asynchronous Programming 17. Understanding Xcodes Debugging Tools 18. LLDB and the Command Line 19. Deploying Third - Party Code 20. Wrapping It Up

Variables and types


We can declare variables as follows:

var a: Int = 1

In the preceding line of code, a is declared to be of type Int, with a value of 1. Since only an Int can be assigned the value 1, Swift can automatically infer the type of a, and it is not necessary to explicitly include the type information in the variable declaration:

var a = 1

In the preceding code, it is equally clear to both Swift and the reader what type a belongs to.

Note

What, no semicolons?You can add them if you want to, and you'll have to if you want to put two statements on the same line (why would you do that?). But no, semicolons belong to C and its descendants, and despite its many similarities to that particular family of languages, Swift has left the nest.

The value of a var can be changed by simply assigning to it a new value:

var a = 1 
a = 2 

However, in Swift, we can also declare a constant, which is immutable, using the let keyword:

let b = 2 

The value of b is now set permanently. The following attempt to...

You have been reading a chapter from
Mastering macOS Programming
Published in: May 2017
Publisher: Packt
ISBN-13: 9781786461698
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image