Is @IBAction just a syntax in Swift or does @Something mean a particular thing in Swift?

孤人 提交于 2019-12-10 22:34:08

问题


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.


回答1:


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

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

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

Here is whole list of attributes in swift

From apple swift blog

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.

Alternatively, you can link in an implementation of main written in Objective-C, common when incrementally migrating projects from Objective-C to 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))


来源:https://stackoverflow.com/questions/26683242/is-ibaction-just-a-syntax-in-swift-or-does-something-mean-a-particular-thing-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!