mercredi 13 novembre 2019

How to use IF statement for UILabel and String in Swift?

For if timeInWord == "twelveAM" {

I got the following error: Binary operator '==' cannot be applied to operands of type 'UILabel' and 'String'

Could you please fix this problem?

import UIKit

class ViewController: UIViewController {

    let scrollView = DScrollView()
    let scrollViewContainer = DScrollViewContainer(axis: .vertical, spacing: 10)
    let scrollViewElement = DScrollViewElement(height: 1200, backgroundColor: .lightGray)

    let timeComponents = ["twelveAM": "12 AM", "oneAM": "1 AM", "twoAM": "2 AM", "threeAM": "3 AM", "fourAM": "4 AM", "fiveAM": "5 AM", "sixAM": "6 AM", "sevenAM": "7 AM", "eightAM": "8 AM", "nineAM": "9 AM", "tenAM": "10 AM", "elevenAM": "11 AM", "Noon": "Noon", "onePM": "1 PM", "twoPM": "2 PM", "threePM": "3 PM", "fourPM": "4 PM", "fivePM": "5 PM", "sixPM": "6 PM", "sevenPM": "7 PM", "eightPM": "8 PM", "ninePM": "9 PM", "tenPM": "10 PM", "elevenPM": "11 PM", "midnight": "12 AM"]

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white


        view.addScrollView(scrollView, withSafeArea: .withSpaceForHeadline, hasStatusBarCover: false, statusBarBackgroundColor: .white, container: scrollViewContainer, elements: [scrollViewElement])


        for (timeInWord, timeInNumber) in timeComponents{
            let timeInWord = UILabel()
            timeInWord.text = timeInNumber
            scrollViewElement.addSubview(timeInWord)
            if timeInWord == "twelveAM" {
                timeInWord.edgeTo(scrollViewElement, safeArea: .twelveAm)
            }
            else {
                timeInWord.edgeTo(scrollViewElement, safeArea: .followingHour)
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire