Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,553
309
Hi guys

I have a question which I always stumble upon. Say you have two structures as follows:
Code:
struct Occupation {

    let name: String

}


struct Person {

    let firstName: String

    let lastName: String

    var occupations: [Occupation]

    func hasAnOccupationOfKind(occupation occupationToMatch: String) -> Bool{

        var match = false

        for occupation in occupations {

            if occupation.name == occupationToMatch { match = true}

        }

        return match

    }
}
Should the function be coded in Person or within Occupation? Of course it would be named something like isOfKind(_ occupation: String)

Tx!
 
Sure. I would stick it in the Person struct since you are comparing "Person" with Occupation. I personally like to leave models function free and add it in via an extension. But it doesn't really matter.
 
  • Like
Reactions: grandM
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.