gpt4 book ai didi

ios - Swift 中类的函数调用中的 "Expression are not allowed at top level"

转载 作者:行者123 更新时间:2023-11-30 14:05:28 24 4
gpt4 key购买 nike

以下是我的代码..我不明白为什么每次都会出现这个错误。

import UIKit
import Foundation

class BaseLabel:UILabel
{
func setFontAndTitle(FontName:String,FontSize:CGFloat,Title:String) {
self.font = UIFont(name: FontName, size: FontSize)
self.text = Title
}

}

var lbl = BaseLabel()
lbl.setFontAndTitle ("Areal", FontSize: 14, Title: "Check label")

在最后一行中,我收到错误“顶层不允许表达式”

最佳答案

您尝试在类外输入代码。您需要将它放在您的类中并包含在函数体中。请看一下我的解决方案:

import UIKit
import Foundation

class BaseLabel:UILabel
{
func setFontAndTitle(FontName:String,FontSize:CGFloat,Title:String) {
self.font = UIFont(name: FontName, size: FontSize)
self.text = Title
}

func changePropertiesOfLabel(){
var lbl = BaseLabel()
lbl.setFontAndTitle ("Areal", FontSize: 14, Title: "Check label")
}

}

关于ios - Swift 中类的函数调用中的 "Expression are not allowed at top level",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com