"Interactive Web Pages with JavaScript" was retired on March 17, 2017. You are now viewing the recommended replacement.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Structs in swift

Hi

I'm really struggling on structs and are currently on the course Object oriented Swift 2.0. The first 4 steps were easy and pretty straightforward and I understood them as it was mostly based on the syntax.

However the next video tutorial was methods and from there on wards I basically lost track and am thinking of giving up. especially the example of the game and the points nothing makes sense. I've been on Structs for 4/5 days now and only completed it after looking at what everyone else had done.

Can anyone explain structs and methods? so we use constants and loops and everything inside them?

What are Initializers that also completely goes above my head?

Thank you

When I look back what a silly question to ask. How did I not understand this!

2 Answers

Hey Alia Khan,

In Swift there are 3 basic object types - Structures, Classes, and Enumerations. Each one of them has it's own set of capabilities and therefore use cases.

Object oriented programming is all about interacting with the objects that make up your application. A simple way to grasp this is to view everything as an object. The Swift language or Apple's frameworks provide a variety of predefined objects for you to use. For example, a String is an object, an Int is an object, and a UIButton is an object. These have all been provided for you and can be used to make your applications.

However, sometimes we need to create our own objects when developing our applications. This is where Classes/Structures come into play. These are blueprints to create your own objects!

For example, let's say for our application we need to create a Person type:

struct Person {
    // These are properties, think of it as things your object HAS
    let name: String
    let age: Int

   // These are methods (functions associated with a type), they are things your object can DO
   func speak() {
       print("I am speaking!")
   }

   func brushTeeth() {
      print("Brushing Teeth")
   }

   // This is the initializer (its a special type of function)
   // You call this function to create an instance of your object
   // It's job is to make sure all of the properties of that object have values
   init(name: String, age: Int) {
       self.name = name
       self.age = age
   } 
}
//Remember Classes/Structures/Enumerations are BLUEPRINTS that define your own data types!
//Now that we have defined what a properties a person has 
// and what a person can do (it's methods), we can create one!

// We do this by calling our initializer function and passing it the values it requires
// To give its properties values

let aPerson: Person = Person(name: "Alia", age: 20)

I hope this clears some things up, Good Luck!

Thank you

  • Initializers:

Q: What is an initializer? A: An Initializer is a method / function

Q: What does an initializer do? A: It creates an instance of the class.

Q: What is an instance? A: An instance is a copy of a Class or Struct in a variable or constant for you to use.

Q: What is the difference between a normal method and an initializer method? A:
1. Instead of doing func name(args) -> return {}, you do init(args) {} 2. The return type does not need to set. 3. An initializer does not need a name. 4. The init keyword is used instead of func. 5. The Initializer is specifically designed to create an instance of a Struct or Class. Normal Methods don't do that.