Lukesivi
Lukesivi

Reputation: 2226

Trying to add value to array index from the UI in Swift

So I'm trying to store the average vote on images in an array (each value for each image is stored respectively.)

Here's what happens: The user votes, the average of the votes is shown to the user (from past votes), and then the user taps "next" to move onto the next movie to vote on.

How would I go about storing the value of each image so that it has its own average stored? I'm pretty new to swift...

Here's the function with which I'm calculating the average and the global variable above it which holds the array of the votes:

    var votes:[Double] = []

    func average(nums:[Double]) ->Double {    
    var total = 0.0
    for vote in nums {
        total += Double(vote)
    }
    let votesTotal = Double(nums.count)
    var average = total/votesTotal
    return average
}

I'm accessing the images through incrementing (only 10). This is in an UIButton normally.
The counter variable is outside of the scope of the IBAction where the counter increment is being performed.

var counter = 1

   (IBACTION)
   counter++
    if counter == 10 {

        counter = 1
    }

    imageOfPerson.image = UIImage(named:"image\(counter).jpg")

    resultLabel.text = ""

    enableButtons()
    }

Thanks a lot in advance.

Upvotes: 0

Views: 87

Answers (2)

jwlaughton
jwlaughton

Reputation: 925

Consider this code. To test this I pasted the following code into a class I have called AnimationObserver, which is instantiated with animationObserver:AnimationObserver = AnimationObserver();

struct imageVotes
{
    var imageNumber:Int = 0
    var vote:Double = 0.0
}

var votes:[imageVotes] = []
var averages:[Double] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
var imageNumber:Int = 0

var thisVote:Double = 0.0
{
    didSet
    {
        var imageVote:imageVotes = imageVotes()
        imageVote.imageNumber = imageNumber
        imageVote.vote = thisVote
        votes.append(imageVote)
        averages[imageNumber] = average(votes)
    }
}

func average(nums:[imageVotes]) ->Double
{
    var total:Double = 0.0
    var numVotes:Double = 0.0

    for var i = 0; i < nums.count; i++
    {
        if nums[i].imageNumber == imageNumber
        {
            total += nums[i].vote
            numVotes += 1.0
        }
    }
    var average = total/numVotes
    return average
}

If then from somewhere else in the code (where animationObserver is visible, probably an IBAction for you) I write:

    animationObserver.imageNumber = 0
    animationObserver.thisVote = 5.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")
    animationObserver.thisVote = 8.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")


    animationObserver.imageNumber = 1
    animationObserver.thisVote = 7.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")
    animationObserver.thisVote = 10.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")

this prints:

Average Vote for imageNumber 0 is 5.0
Average Vote for imageNumber 0 is 6.5
Average Vote for imageNumber 1 is 7.0
Average Vote for imageNumber 1 is 8.5

Upvotes: 1

Syed Tariq
Syed Tariq

Reputation: 2918

You could use a dictionary with the image filename as the key. For example:

var dictAverages = [String:Double]()
dictAverages["image\(counter).jpg"] =  average(nums) //nums is the data array corresponding to the counter

I would have helped more specifically but I am having difficulty understanding your code.

Upvotes: 0

Related Questions