problems with MVC

Discussion in 'iOS Programming' started by grandM, Sep 30, 2015.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    Hi guys

    I'm stumbling over the MVC concept once more. I'm building something simple. I have several animals which I'm showing one by one.

    At the moment the array of animals is in the Controller. I suppose it shouldn't be in the Controller but in the Model? Is this so?

    Secondly. If the array ought to be in the Model what should I do? Should I use a different struct to define what an Animal is. Or should the array of animals be in the class containing the logic to add animals etc?

    So in code I'm in doubt if I should do this:

    Code:
    struct Animal {
    
        var name: String = ""
        var birthPlace: String = ""
        var breed: String = ""
        var age: Double = 0.00
    
       
    
        var animals: [Animal] = []
        func addAnimal(){}
    
    }
    
    or

    Code:
    struct Animal {
    
        var name: String = ""
        var birthPlace: String = ""
        var breed: String = ""
        var age: Double = 0.00
    }
    
    class animalLogic(){
     
        var animals: [Animal] = []  
    
        func addAnimal(){}
    }
    
    I presume the latter as with the first solution the array would contain for each animal the array animals once more.

    Thanks once again
     
  2. smirk, Sep 30, 2015
    Last edited by a moderator: Sep 30, 2015

    smirk macrumors 6502a

    smirk

    Joined:
    Jul 18, 2002
    Location:
    Orange County, CA
    #2
    Right, you would want to put the logic in your model class, and the display of that logic in the view controller. So if your program tracked animals, your model would contain the `animals` array plus methods to add, remove, and update `Animal` data. The view controller code would orchestrate the interface: take data from the model and place it on the screen, take data the user keyed in and hand it off to the model methods.

    So your model code could be something like this:

    Code:
    struct Animal {
        var name: String
        var birthPlace: String
        var breed: String
        var age: Double
    }
    
    class AnimalLogic {
        var animals = [Animal]()
    
        func addAnimal(name: String, birthPlace: String, breed: String, age: Double){
            animals += [Animal(name: name, birthPlace: birthPlace, breed: breed, age: age)]
        }
    }
    And then in your view controller you would do something like:

    Code:
    var animalLogic = AnimalLogic() // Instantiate the model
    
    . . .
    
    animalLogic.addAnimal("Rex", birthPlace: "Home", breed: "Dalmation", age: 3.4)
     
  3. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #3
    Thanks this is truly helpful
    If I want to go from animal to animal, would I store the number from the animal in the Controller or in the Model? I presume the Controller?
     
  4. smirk macrumors 6502a

    smirk

    Joined:
    Jul 18, 2002
    Location:
    Orange County, CA
    #4
    Without knowing exactly what you're trying to accomplish, it's hard to say. Also, I'm not sure what you mean by "the number from the animal". But basically, you could do it different ways. Your model could expose the `animals` array and the controller could just iterate through animalLogic.animals ("for animal in animalLogic.animals {}"). Or, the model could provide methods like `nextAnimal()` that you call whenever you want the next one.
     
  5. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #5
    I was using a next button in the View. I thought of using nextAnimal() but then I would have to keep the number of the current animal in the View hence breaking MVC. I could pass the number in the nextAnimal() as a parameter of course.
     
  6. smirk macrumors 6502a

    smirk

    Joined:
    Jul 18, 2002
    Location:
    Orange County, CA
    #6
    You could add an ID number to your Animal struct that gets automatically populated by the initializer when a new Animal is created. Perhaps that would let you track the animals without having to separately track the animal number (still not sure what you mean by "number", though).
     
  7. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #7
    In my Controller I am using an Int to go from one animal to the other hence the number. The Model shouldn't know what is being used for though and I was trying not to break MVC.
     

    Attached Files:

  8. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #8
    Hi, I'm also struggling with MVC last months as well, besides all my other work related projects.

    When I actually started to understand MVC quite more. (maybe it's only regarding websites) is that
    The model contains all data and the actuall modifying data parts. However you only call these functions from the Controller.

    So also the controller contains almost all functions that the model does, but basic, variables check etc and pass it to the model and get the result(s)

    In the end you parse the result to your viewcontroller to setup the actual view which will be visible for the user.

    So all your array must be in model and all functions required to list all, list 1, add 1 or multiple, remove one or multiple, edit 1 or multiple, but strictly only for the data itself.

    your controller should be passing all data to the functiona of the model to get it to work.
    Once thats done the controller creates the view with desired data from the model.

    And there we got the mvc model, ofcourse you can use libraries for a lot as most commands will be used more often and to prevent retyping it all again, create a library to prepare everything to make life easier :)

    hopefully it makes sense to you as it did to me :)

    for your navigation you could use the controller to pass the current index of the array as a variable, that should have said enough i think
     

Share This Page