Book Image

Learning Xcode 8

By : Jak Tiano
Book Image

Learning Xcode 8

By: Jak Tiano

Overview of this book

Over the last few years, we’ve seen a breakthrough in mobile computing and the birth of world-changing mobile apps. With a reputation as one of the most user-centric and developer-friendly platforms, iOS is the best place to launch your next great app idea. As the official tool to create iOS applications, Xcode is chock full of features aimed at making a developer’s job easier, faster, and more fun. This book will take you from complete novice to a published app developer, and covers every step in between. You’ll learn the basics of iOS application development by taking a guided tour through the Xcode software and Swift programming language, before putting that knowledge to use by building your first app called “Snippets.” Over the course of the book, you will continue to explore the many facets of iOS development in Xcode by adding new features to your app, integrating gestures and sensors, and even creating an Apple Watch companion app. You’ll also learn how to use the debugging tools, write unit tests, and optimize and distribute your app. By the time you make it to the end of this book, you will have successfully built and published your first iOS application.
Table of Contents (23 chapters)
Learning Xcode 8
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating classes, structs, and enums


So now we've covered the building blocks of programming in Swift. Our next step is to understand how to put these pieces together in an object-oriented programming environment. To do that, we're going to need to learn about classes, structs, and enumerations in Swift.

Classes

Classes in Swift are composed of properties and methods (functions). Let's jump right into an example:

class MyClass {
   
    var myInt: Int
    var myFloat: Float
   
    private var myOptString: String?
   
    init () {
        myInt = 0
        myFloat = 0
    }
   
    func generateString() -> String {
        myOptString = "\(myInt) \(myFloat)"
        return myOptString!
    }
   
}

On the first line, you see the beginning of the class declaration, beginning with the class keyword, followed by the class name. Class names in Swift should always be capitalized. The rest of the class declaration is inside a set of curly braces.

At the top of the class, we declare our properties...