Add a stored variable property or not

Discussion in 'iOS Programming' started by grandM, Dec 21, 2015.

  1. grandM, Dec 21, 2015
    Last edited: Dec 21, 2015

    grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    Hi

    The following code works like a charm. Basically I have this photoArray which I filled with photos during init. In a View I'm iterating through these photos. To do so I added a variable to the ViewController keeping the current ranking (in the array) of the photo. When I want to go to the next photo I'm using the Int returned by the function showNextPhoto in the tuple.

    I could change the variable photosArray to [(Int, Photo)] in the Gallery class. The Int indicating the current photo number. In that case I would no longer return a tuple in the nextPhoto function as each photo would have its own ranking. If I would do so it complicates however my code in other parts of my code. So I wondered what you guys would do?

    Code:
    var photosArray: [Photo] = []
    func nextPhoto(var currentPhotoRanking: Int) -> (Int, Photo?) {
    
            if currentPhotoRanking < self.photosArray.count - 1 {
    
                currentPhotoRanking++
    
            } else {
    
                currentPhotoRanking = 0
    
            }
    
            return (currentPhotoRanking, self.photosArray[currentPhotoRanking])
    
        }
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Wouldn't this Int just be reproducing your index in the photosArray? So, photosArray[0] = [(0, Photo0)], photosArray[1] = [(1, Photo1)], etc?
     
  3. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #3
    yes it is but this way the Model gives me the index of the next Photo in its array
    At the end of photosArray it restarts at 0
    Otherwise I would have to do the count of photosArray in the Controller or add a count property to Gallery
    Now my Controller just asks for the next Photo hence respecting MVC
     
  4. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    I just realized I am still breaking MVC. The best solution will be to add an ID to the Photo. This way I can set the currentPhoto as a variable in the Controller. Then use the id of the currentPhoto to let the Model fetch the next one. Otherwise the Controller remains intertwined with the Model.
     

Share This Page