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,551
309
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])

    }
 
Last edited:
I could change the variable photosArray to [(Int, Photo)] in the Gallery class. The Int indicating the current photo number.

Wouldn't this Int just be reproducing your index in the photosArray? So, photosArray[0] = [(0, Photo0)], photosArray[1] = [(1, Photo1)], etc?
 
Wouldn't this Int just be reproducing your index in the photosArray? So, photosArray[0] = [(0, Photo0)], photosArray[1] = [(1, Photo1)], etc?
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
 
Wouldn't this Int just be reproducing your index in the photosArray? So, photosArray[0] = [(0, Photo0)], photosArray[1] = [(1, Photo1)], etc?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.