Hello,I have tried the dispatch queue. I don't know if I did that right.Game is a public classe that is in another source file in the playground. And checkVictory is a method of that classe.import Foundation
import UIKit
public class Game {
public var word: String
public var clue: String
public var numberOfTriesLeft = 6
public init(word: String, clue: String) {
self.word = word
self.clue = clue
}
public func checkLetterInWord(letter: String) -> [Int] {
let wordCharacters = Array(word)
var indexesInWord: [Int] = []
var i = 0
for char in wordCharacters {
//print("\(char) and \(letter)")
if char == Character(letter) {
indexesInWord.append(i)
}
i += 1
}
return indexesInWord
}
public func checkVictory(label: String) -> Bool {
for char in label {
if char == "?" {
return false
}
}
return true
}
}The function updateWordLabelAndCheckVictory calls presentVictoryView method.This is how I am using queues:func updateWordLabelAndCheckVictory(letter: String, indexArray: [Int]) {
if let currentLabel = wordLabel.text {
var wordArray = Array(currentLabel)
for i in indexArray {
wordArray[i] = Character(letter)
}
DispatchQueue.main.async { [weak self] in
self?.wordLabel.text = String(wordArray)
}
if (game.checkVictory(label: wordLabel.text!)) {
print("Aqui")
currentGame += 1
DispatchQueue.main.async { [weak self] in
self?.presentVictoryView()
}
}
}
}func presentVictoryView() {
DispatchQueue.main.async { [weak self ] in
self?.blackView.isHidden = false
for _ in 0 ... 30 {
let objectView = UIView()
objectView.translatesAutoresizingMaskIntoConstraints = false
objectView.frame = CGRect(x: 0, y: 0, width: 20, height: 100)
objectView.backgroundColor = .clear
//objectView.alpha = CGFloat(0.9)
objectView.isHidden = false
let ballon = UILabel()
ballon.translatesAutoresizingMaskIntoConstraints = false
ballon.frame = CGRect(x: 0, y: 0, width: 20, height: 100)
ballon.backgroundColor = .clear
//ballon.alpha = CGFloat(0.9)
ballon.text = ""
ballon.font = UIFont.systemFont(ofSize: 60)
objectView.addSubview(ballon)
NSLayoutConstraint.activate([
ballon.centerXAnchor.constraint(equalTo: objectView.centerXAnchor),
ballon.centerYAnchor.constraint(equalTo: objectView.centerYAnchor)
])
self?.blackView.addSubview(objectView)
let randomXOffset = Int.random(in: -120 ..< 200)
let path = UIBezierPath()
path.move(to: CGPoint(x: 160 + randomXOffset, y: 1000))
path.addCurve(to: CGPoint(x: 100 + randomXOffset, y: -300), controlPoint1: CGPoint(x: 300 - randomXOffset, y: 600), controlPoint2: CGPoint(x: 70 + randomXOffset, y: 300))
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = path.cgPath
animation.repeatCount = 1
let random = Double.random(in: 3.0 ..< 7.0)
animation.duration = random
//animation.timeOffset = Double(arc4random_uniform(50))
objectView.layer.add(animation, forKey: "animate position along path")
}
self?.newGame()
}
}Could you tell me how I should use the queues?
Post
Replies
Boosts
Views
Activity
This was the problem... πThank you!!!
Line 9 in the sencond block of code I posted:game.checkLetterInWord(letter: buttonLetter!)
letterButtonPressed is a target for the button.let newButton = UIButton(type: .system)
newButton.setTitle(alphabet[letter], for: .normal)
newButton.setTitleColor(UIColor.black, for: .normal)
newButton.addTarget(self, action: #selector(letterButtonPressed), for: .touchUpInside)This is my current code:@objc func letterButtonPressed(_ sender: UIButton) {
sender.isHidden = true
hiddenButtons.append(sender)
let buttonLetter: String? = sender.currentTitle?.uppercased()
if buttonLetter != nil {
game.checkLetterInWord(letter: buttonLetter!)
}
}This is the function that receives the title of the button:public func checkLetterInWord(letter: String) -> Bool {
print(letter)
return true
}The error I get is:Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value in line game.checkLetterInWord(letter: buttonLetter!)
Thank you!!! πI literally pasted the code in Scene Delegate and it worked perfectly.Why do we have to use scene delegate with iOS 13? What is now done in App Delegate and how are app delegate and scene delegate related?
Thank you so much to both Claude and OOPer.Here is my code after all the help. Turned out a TextView served my interests best.I hope it helps someone out there with the same problem as me.To instantiate the PetitionView Controller here is the code:let viewController = self.storyboard!.instantiateViewController(identifier: "PetitionDetails") as! PetitionDetailsViewController
viewController.textLabel = petitions[indexPath.row].body
navigationController?.pushViewController(viewController, animated: true)I had to create another variable in this classe I could not passa it directly to the outlet before the view was loaded.import UIKit
class PetitionDetailsViewController: UIViewController {
var textLabel: String?
@IBOutlet weak var PetitionDetailsOutlet: UITextView!
override func viewWillAppear(_ animated: Bool) {
if let text = textLabel {
PetitionDetailsOutlet.isEditable = false
PetitionDetailsOutlet.text = text
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Thank you so much! I did it! π
Hello,Thank you for your reply. Could you be a little bit more specific?Thanks!
Hello, Thank you so much for your reply. I have just done it the way you recomended but it continues to crash. π The error is the same. I gave the view controller an id as you said.What else could be wrong?
So basically what I am saying is that I am going to trust that the classe that implements the WebView itself has all the functions that are needed?