Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios &引用;“不允许在顶层使用表达式”;Swift中类的函数调用_Ios_Iphone_Xcode_Swift - Fatal编程技术网

Ios &引用;“不允许在顶层使用表达式”;Swift中类的函数调用

Ios &引用;“不允许在顶层使用表达式”;Swift中类的函数调用,ios,iphone,xcode,swift,Ios,Iphone,Xcode,Swift,下面是我的代码。。我不明白为什么每次都会出现这个错误 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 }

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

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

在最后一行中,我得到了一个错误“Expression are not allowed at top level”

您试图在类外键入代码。你们需要把它放在你们的类中,并封装在函数体中。请看一下我的解决方案:

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

}

除了在操场或命令行项目中运行它, 我们也可以在单视图应用程序中运行它

假设我们的代码写在ViewController.swift文件中:

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

class ViewController:UIViewController {
    override func viewDidLoad() {
        var lbl =  BaseLabel()
        lbl.setFontAndTitle (FontName: "Areal", FontSize: 14, Title: "Check label")
        print(lbl)
    }
}

问题是Xcode不知道何时运行
var lbl=BaseLabel()lbl.setFontAndTitle(“Areal”,FontSize:14,Title:“Check label”)
。要解决这个问题,您必须将代码放在具有特定运行点的位置(函数或操场)。您可以将其放入ViewController中的viewDidLoad函数中,将其放入按顺序运行其代码的游乐场或其他文件中,或者将其放入新函数中

解决方案1: 解决方案2: 把它放在操场上

解决方案3(可能是最好、最有效的方法):
对于解决方案3,在viewDidLoad或其他活动运行点内运行
customizeLabel()

将其粘贴到操场Beta 6中,效果良好。作为旁注,不要在参数名称中使用大写字母,这样可以很容易地将它们与类型区分开来。实际上,我使用的是真实的项目,每次应用小尺寸参数后都会显示相同的结果杰夫-尝试使用真实的项目,而不是操场。在游乐场我甚至不能导入UIKit。@EvgeniyKleban如果你不能导入UIKit,这意味着你已经创建了一个Mac OS X游乐场,其中UIKit不是一个有效的框架。在Swift中,顶级语句在游乐场中工作(并且“脚本”文件从命令行运行),但在应用程序类文件中不工作。
class ViewController: UIViewController {
   override func viewDidLoad() {
      var lbl = BaseLabel()
      lbl.setFontAndTitle(FontName: "Calibri", FontSize: 14, Title: "Check Label"
   }
}
func customizeLabel() {
    var lbl = BaseLabel()
    lbl.setFontAndTitle(FontName: "Calibri", FontSize: 14, Title: "Check Label"
}