“不允许在顶级表达”在Swift中的类的函数调用中 [英] "Expression are not allowed at top level" in function call of a class in Swift

查看:90
本文介绍了“不允许在顶级表达”在Swift中的类的函数调用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following is my code.. I don't understand why this error shows me everytime.

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

最后我得到错误表达式不允许在顶级

In the last line I am getting the the error "Expression are not allowed at top level"

推荐答案

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

You trying to typing code outside of class. You need to put it in you class and enclose in function body. Please take a look at my solution:

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

}

这篇关于“不允许在顶级表达”在Swift中的类的函数调用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆