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

Structure of iOS Projects & passing between classes

I still have big issues on deciding how to structure my app within Xcode. So far I basically put all my methods, variables, enums, etc. in the same ViewController within the same class that is created when I start a new project. This is mostly due to me not knowing how to pass the result of a method to a method of another class, as well as when it makes sense/is necessary to create a new class. Do I always need to be within a class? Putting it all in one class eliminates all passing issues.

Could someone please tell me how to well-structure an iOS project and how to pass results of functions between classes?

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Throwing everything into one class maybe convenient but it is not practical. Apple suggest that iOS projects follow the MVC design pattern or Model-View-Controller. The model layer is separated from the controller. As far as passing data, that's why you have properties. If you are going from one view controller A to view controller B make sure that B has a property that can be assigned a value by A.

Hi Amit, thank you for this pointer! I will look into the MVC design pattern. Could you give me a simple example of passing a property (see example below)? How can I use the final result of additions happening in 'ControllerA' and 'ControllerB' in 'ControllerC'? In the example below I can only assign an Int to 'resultFinal' when its outside of class 'ControllerC'.

import Foundation
import UIKit

class ControllerA {
     class func sending( a: Int, b: Int) -> Int {
        let result: Int = a + b
        return result
    }
}

class ControllerB {
    class func receiving( result: Int, c: Int) -> Int  {
        let furtherAddition = result + c
        return furtherAddition
    }
}

class ControllerC: UIViewController {
        let resultFinal: Int = ControllerB.receiving(result: ControllerA.sending(a: 2, b: 2), c: 2)
}