3

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 Answer 1

3

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))
3
  • "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" How do I do that in the main.swift?
    – Leo Dabus
    Oct 31, 2014 at 21:27
  • create main.swift and write main function.Swift application looks if main.swift is present or not.If present calls main function.
    – codester
    Oct 31, 2014 at 21:31
  • NSApplicationMain(C_ARGC, C_ARGV) thats how it is right now
    – Leo Dabus
    Oct 31, 2014 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.