Returning Dictionaries from a function?

  1. 8 months ago

    So I'm trying to return a random dictionary from an array of dictionaries using a function in Swift.

    Here is the part not working:

    //method for returning a random array from the collection sourse without doing it twice...
    func noRepeatArray(array: Array) -> Dictionary{
        let newRandomNumber = randomNumber()
        let randomSet = array[newRandomNumber]
        let canUseSet: Bool
        
        if(temp.contains(randomSet)){
            newRandomNumber == randomNumber()
            canUseSet = false
        } else {
            temp.append(randomSet)
            canUseSet = true
        }
        if canUseSet == true {
            return randomSet
        }
    }
    

    Here is the code around it:

    //Slot Types for Button
    enum slotNumber {
        case slotOne
        case slotTwo
        case slotThree
        case slotFour
        
    }
    
    //Historical Events dictionaries
    let appleHistoryI: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
    let appleHistoryII: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
    let presidants: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
    let gameOfThrones: [[slotNumber: String]] = [[.slotOne :"Macintosh"], [.slotTwo :"Apple II"],[.slotThree :"PowerPC"], [.slotFour : "PowerBookG4"]]
    
    //collection of dictionaries...
    let collectionOfDictionaries = [
        appleHistoryI,
        appleHistoryII,
        presidants,
        gameOfThrones
    ]
    
    //random number method
    func randomNumber() -> Int{
        let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(collectionOfDictionaries.count)
        return randomNumber
    }
    //method for returning a random array from the collection sourse without doing it twice...
    func noRepeatArray(array: AnyObject) -> AnyObject{
        let newRandomNumber = randomNumber()
        let randomSet = array[newRandomNumber]
        let canUseSet: Bool
        
        if(temp.contains(randomSet)){
            newRandomNumber == randomNumber()
            canUseSet = false
        } else {
            temp.append(randomSet)
            canUseSet = true
        }
        if canUseSet == true {
            return randomSet
        }
    }
    //temperary collection to keep track of used arrays
    let temp = []
    

    Is there a better way i should do this or do i need to figure a way to return dictionaries?

or Sign Up to reply!