where should I write the function?

Discussion in 'iOS Programming' started by grandM, Jan 26, 2017.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    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!
     
  2. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #2
    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.
     

Share This Page