@IBAction只是Swift中的语法还是@Something意味着Swift中的特定内容? [英] Is @IBAction just a syntax in Swift or does @Something mean a particular thing in Swift?

查看:267
本文介绍了@IBAction只是Swift中的语法还是@Something意味着Swift中的特定内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@IBAction在Swift中只是一个语法差异,还是表示具体的东西。
此外,在AppDelegate.swift中可以看到类似的用法,其中写有@UpppplicationMain。

Is @IBAction just a syntactical difference in Swift or does it signify something specific. Also, similar usage is seen in AppDelegate.swift where @UIApplicationMain is written.

推荐答案

这些是<$ c swift中的$ c>属性。它们对编译器有一些特殊含义。例如: @UIApplicationMain 合成 main.swift 编译器的文件作为application的入口点。来自swift 指南

These are attributes in swift.They have some special meaning for compiler.For eg: @UIApplicationMain synthesize main.swift file by compiler as entry point for application.From swift guide


属性提供有关声明或类型的更多信息。
Swift中有两种属性,适用于
声明的属性和适用于类型的属性。

Attributes provide more information about a declaration or type. There are two kinds of attributes in Swift, those that apply to declarations and those that apply to types.

NSApplicationMain
将此属性应用于类以指示它是应用程序委托。使用此属性相当于调用
NSApplicationMain函数并将此类的名称作为委托类的
名称传递。

NSApplicationMain Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the NSApplicationMain function and passing this class’s name as the name of the delegate class.

如果不这样做使用此属性,为main.swift文件提供一个调用NSApplicationMain函数的主
函数。例如,如果
您的应用使用NSApplication的自定义子类作为其主要
类,请调用NSApplicationMain函数而不是使用此
attrib

If you do not use this attribute, supply a main.swift file with a main function that calls the NSApplicationMain function. For example, if your app uses a custom subclass of NSApplication as its principle class, call the NSApplicationMain function instead of using this attrib

以下是swift中的完整属性列表

来自apple swift 博客

From apple swift blog


In Xcode,Mac模板默认包含main.swift文件,但iOS应用程序的
新iOS项目模板的默认设置是将
@UIApplicationMain添加到常规Swift文件中。这会导致编译器
合成iOS应用程序的主入口点,并消除
对main.swift文件的需求。

In Xcode, Mac templates default to including a "main.swift" file, but for iOS apps the default for new iOS project templates is to add @UIApplicationMain to a regular Swift file. This causes the compiler to synthesize a main entry point for your iOS app, and eliminates the need for a "main.swift" file.

或者,您可以链接到
Objective-C中编写的main实现,这在将项目从
Objective-C逐步迁移到Swift时很常见。

Alternatively, you can link in an implementation of main written in Objective-C, common when incrementally migrating projects from Objective-C to Swift.

main.swift 中编写代码。它将作为应用程序的点数工作

write code in main.swift.It will work as enty point for application

//main.swift
import Foundation
import UIKit

UIApplicationMain(C_ARGC, C_ARGV,nil, NSStringFromClass(AppDelegate))

这篇关于@IBAction只是Swift中的语法还是@Something意味着Swift中的特定内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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