For if timeInWord==“十二进制”{
我得到以下错误:二进制运算符“=”不能应用于“UILabel”和“String”类型的操作数
你能解决这个问题吗?

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)
            }
        }
    }
}

最佳答案

timeInWord是一个UILabeltimeInWord.text是一个String。因此,如果要将标签中的文本与某个常量字符串进行比较,应该使用if timeInWord.text == "twelveAM"。您可以通过在Xcode中选择单击任何变量来获取其类型。确保类型符合您的期望。

10-06 04:13