I have plist file and database, in the plist file has English letters and in database row has String for example (London) and I convert String to characters array and append
What you need to do is first create an array with all your target letters (shuffled and added random letters) and then run over that array to update the button titles.
Start by filling a 14 value array 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)
}
Generate available positions for the answer string:
var availableIndexes : [Int] = Array(0..<presentedLetters.count)
Then go over your letters from the db and add them in random places inside the target array (while remembering added indexes to prevent overriding letters):
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 presentedLetters will have an array of all relevant values.
EDIT:
To add the titles to the buttons, just go over presentedLetters array:
for i in 1...14 {
let tileButton = UIButton(type: .roundedRect)
tileButton.setTitle(presentedLetters[i-1], for: .normal)
......
}
}
Good luck!