Post

Replies

Boosts

Views

Activity

Reply to Label.text Error! - Unexpectedly found nil while implicitly unwrapping an Optional value: file...
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() } }
Feb ’20
Reply to Pass button title to function - nil while unwrapping optiona
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!)
May ’20
Reply to Delay updating label text SwiftPlaygrounds
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?
May ’20