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 Introduction

Custom init() methods

How would I create a custom init method for a realm object? I have an enum for different unit of measures and would like to initiate the object by passing the enum to the object and in the init method, use a computed property

1 Answer

enum Measures {
    case firstMeasure, secondMeasure
}

class Distance {
    let measure1: Int
    let measure2: Int

    init(measure1: Int, measure2: Int) {
        self.measure1 = measure1
        self.measure2 = measure2
    }

    func passMesureEnum(measure: Measures) -> Distance {
        switch mesure {
            case .firstMeasure: return Distance(measure1: 0, measure2: 0)
            case .secondMeasure: return Distance(measure1: 0, measure2: 0)
        }
    }
}

something like that