vendredi 16 juillet 2021

fetch a dictionary item at random and print it [duplicate]

I want my swift code to fetch a item from a dictionary. From my understanding the dictionary is keyed with a letter in the alphabet. What I want to do is print a random girls name in the debug section. Every time the button is pressed a new key and name is printed.

import UIKit

class ViewController: UIViewController {
    
    
    var girlsName = ["a": "Jessica Biel", "b": "Gwen Stefani","c":"Jessica Alba" ]
    
    var btn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        [btn].forEach{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.backgroundColor = UIColor(
                red: .random(in: 0.0...1),
                green: .random(in: 0.9...1),
                blue: .random(in: 0.7...1),
                alpha: 1
            )
            
        }
        btn.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        btn.addTarget(self, action: #selector(newark), for: .touchDown)
    }
    
    
    @objc func newark(){
        //print a letter and the corresponding name
        print()
        
    }
    }



Aucun commentaire:

Enregistrer un commentaire