Swift 3 - UIButton add setTitle from plist and database

I have a plist file and a database, the plist file has English letters and the database string has a String like (London) and I convert the String to a character array and add it to the plist file. I created 14 UIButtons by (for a loop) and was given setTitle from the db string and given random order and adding letters from plist and given shuffle.

So my problem is that adding my symbols from the database only shows me the symbols from the database and shuffles like this:

enter image description here

But I don't just need that I need to first add all characters from the database to setTitle and shuffle, and the remaining empty button will add from the plist file like this:

enter image description here

How can I do as shown above (2nd image) ?!

This is my code, what's wrong with writing the code ???:

let fileName : String = " EnglishLetters"
let fileExt : String = "plist"
let pathRes = Bundle.main.path(forResource: fileName, ofType: fileExt)
let pathDict = NSDictionary(contentsOfFile: pathRes!)
var letters : [String] = pathDict?.object(forKey: "Letters") as! [String]

for data in listdata { // SQLite database

    let dataAnswer = data.ans // ans is a row in the SQLite database 
    let dataArrayAnswer = dataAnswer.characters.map{String($0)}
    letters.append(contentsOf: dataArrayAnswer)

    for char in dataArrayAnswer {}

        for i in 1...14 {

            let tileButton = UIButton(type: .roundedRect)
            let lettersAns = dataArrayAnswer.shuffled()[letters.distance(from: id, to: id)] // id is parameter
            tileButton.setTitle(lettersAns, for: .normal)
            tileButton.titleLabel?.font = UIFont(name: "HelveticaNeueW23forSKY-Bd", size: 15)
            tileButton.setTitleColor(.black, for: .normal)
            tileButton.setBackgroundImage(UIImage(named: "Cell"), for: .normal)            
            tileButton.addTarget(self, action: #selector(moveTile(sender:)), for: .touchUpInside)
            xAxis = xAxis + buttonWidth + space

            view.addSubview(tileButton)


            if i%7 == 0 {

               xAxis = emptySpace / 2
               yAxis = yAxis2 + space

            }

        }
    }
 }

      

-1


source to share


1 answer


What you need to do is first create an array with all your target letters (shuffle and add random letters) and then loop through that array to update the button names.

Start by filling an array of 14 values ​​with random letters:

var presentedLetters : [String] = [String]()
var availableLetters : [String] = pathDict?.object(forKey: "Letters") as! [String]

var availableLetterIndexes : [Int] = Array(0..<availableLetters.count)

for letterAt in 0 ..< 14
{
    let randomIndex : Int = Int(arc4random_uniform(UInt32(availableLetterIndexes.count)))
    var charIndex : Int = availableLetterIndexes[randomIndex]

    presentedLetters.append(availableLetters[charIndex])

    availableLetterIndexes.remove(at: randomIndex)
}

      

Create available positions for the response line:

var availableIndexes : [Int] = Array(0..<presentedLetters.count)

      

Then, letter by letter from db and add them to random places inside the target array (while keeping the added indices to prevent letter overrides):



var addedAnswerIndexes : [Int] = [Int]()

let answerString = data.ans // ans is a row in the SQLite database 
let answerCharacters = answerString.characters.map{String($0)}

for answerCharAt in answerCharacters
{
    let randomIndex : Int = Int(arc4random_uniform(UInt32(availableIndexes.count)))
    var charIndex : Int = availableIndexes[randomIndex]

    presentedLetters[charIndex] = answerCharAt

    availableIndexes.remove(at: randomIndex)
}

      

And then the presentedLetters will have an array of all matching values.

EDIT:

To add titles to buttons, simply navigate to the array of representedLetters:

        for i in 1...14 {

            let tileButton = UIButton(type: .roundedRect)
            tileButton.setTitle(presentedLetters[i-1], for: .normal)

            ......

        }
    }

      

Good luck!

+1


source







All Articles