Every little thing about private and non-private Swift attributes

0/5 No votes

Report this app



Have you ever ever heard about Swift language attributes? On this article I am attempting to assemble all of the @ annotations and their meanings.


Have you ever ever heard about Swift language attributes? On this article I am attempting to assemble all of the @ annotations and their meanings.

Public attributes

Public Swift language attributes are marked with the @ image, they’re (kind of) properly documented and prepared to be used. Right here is the entire listing of all the general public Swift language attributes. Most of them will appear very acquainted… ๐Ÿ˜‰


In case you mark a property with the @IBOutlet attribute, the Interface Builder (IB) will acknowledge that variable and you’ll join your supply along with your visuals by the offered “outlet” mechanism.

@IBOutlet weak var textLabel: UILabel!


Equally, @IBAction is an attribute that makes attainable connecting actions despatched from Interface Builder. So the marked methodology will immediately obtain the occasion fired up by the consumer interface. ๐Ÿ”ฅ

@IBaction func buttonTouchedAction(_ sender: UIButton) {}

@IBInspectable, @GKInspectable

Marking an NSCodable property with the @IBInspectable attribute will make it simply editable from the Interface Builderโ€™s inspector panel. Utilizing @GKInspectable has the identical habits as @IBInspectable, however the property can be uncovered for the SpriteKit editor UI as a substitute of IB. ๐ŸŽฎ

@IBInspectable var borderColor: UIColor = .black
@GKInspectable var mass: Float = 1.21


When utilized to a UIView or NSView subclass, the @IBDesignable attribute lets Interface Builder know that it ought to show the precise view hierarchy. So principally something that you simply draw inside your view can be rendered into the IB canvas.

@IBDesignable class MyCustomView: UIView {  }

@UIApplicationMain, @NSApplicationMain

With this attribute you’ll be able to mark a category as the appliance’s delegate. Normally that is already there in each AppDelegate.swift file that you’re going to ever create, nevertheless you’ll be able to present a essential.swift file and name the [UI|NS]ApplicationMain methodology by hand. #pleasedontdoit ๐Ÿ˜…

@out there

With the @out there attribute you’ll be able to mark varieties out there, deprecated, unavailable, and so forth. for particular platforms. I am not going into the small print there are some nice posts about easy methods to use the attribute with availability checkings in Swift.

@out there(swift 4.1)
@out there(iOS 11, *)
func avaialbleMethod() {  }


You possibly can mark a property with this attribute to make a replica of it as a substitute of the worth of the property iself. Clearly this may be actually useful if you copy reference varieties.

class Instance: NSOBject {
    @NSCopying var objectToCopy: NSObject


In case you are utilizing Core Knowledge entities (normally NSManagedObject subclasses), you’ll be able to mark saved variables or occasion strategies as @NSManaged to point that the Core Knowledge framework will dynamically present the implementation at runtime.

class Individual: NSManagedObject {
    @NSManaged var identify: NSString


It is principally a comfort attribute for marking a number of attributes out there for Goal-C. It is legacy stuff for Goal-C dinosaurs, with efficiency caveats. ๐Ÿฆ•

@objcMembers class Individual {
    var firstName: String?
    var lastName: String?


You possibly can mark closure parameters as @escaping, if you wish to point out that the worth may be saved for later execution, so in different phrases the worth is allowed to survive the lifetime of the decision. ๐Ÿ’€

var completionHandlers: [() -> Void] = []

func add(_ completionHandler: @escaping () -> Void) {


By default the compiler raises a warning when a perform returns with one thing, however that returned worth is rarely used. You possibly can suppress the warning by marking the return worth discardable with this Swift language attribute. โš ๏ธ

@discardableResult func logAdd(_ a: Int, _ b: Int) -> Int {
    let c = a + b
    return c
logAdd(1, 2)


This attribute can magically flip a perform with a closure parameter that has no arguments, however a return sort, right into a perform with a parameter sort of that authentic closure return sort, so you’ll be able to name it way more simple. ๐Ÿค“

func log(_ closure: @autoclosure () -> String) {



In case you mark an imported module with the @testable attribute all the inner access-level entities can be seen (out there) for testing functions. ๐Ÿ‘

@testable import CoreKit


This attribute tells the compiler {that a} declaration is on the market to make use of in Goal-C code. Optionally you’ll be able to present a single identifier that’ll be the identify of the Goal-C illustration of the unique entity. ๐Ÿฆ–

class ExampleClass: NSObject {

    @objc non-public var retailer: Bool = false

    @objc var enabled: Bool {
        @objc(isEnabled) get {
            return self.retailer
        @objc(setEnabled:) set {
            self.retailer = newValue

    func set(enabled: Bool) {
        self.enabled = enabled


Use this attribute to supress an implicit objc attribute. The @nonobjc attribute tells the compiler to make the declaration unavailable in Goal-C code, though itโ€™s attainable to characterize it in Goal-C. ๐Ÿ˜Ž

@nonobjc static let check = "check"


This attribute point out perform calling conventions. It may possibly have one parameter which signifies Swift perform reference (swift), Goal-C suitable block reference (block) or C perform reference (c).

func a(a: Int) -> Int {
    return a
let exampleSwift: @conference(swift) (Int) -> Int = a

Personal attributes

Personal Swift language attributes ought to solely be utilized by the creators of the language, or hardcore builders. They normally present additional (compiler) performance that’s nonetheless work in progress, so please be very cautious… ๐Ÿ˜ณ

Please don’t use non-public attributes in manufacturing code, until you actually know what you’re doing!!! ๐Ÿ˜…


If you wish to import an exterior module on your entire module you should use the @_exported key phrase earlier than your import. From now the imported module can be out there all over the place. Bear in mind PCH recordsdata? ๐Ÿ™ƒ

@_exported import UIKit


With the @inline attribute you explicitly inform the compiler the perform inlining habits. For instance if a perform is sufficiently small or it is solely getting known as a number of occasions the compiler is possibly going to inline it, until you disallow it explicitly.

@inline(by no means) func a() -> Int {
    return 1

@inline(__always) func b() -> Int {
    return 2

@_inlineable public func c() {

@inlinable is the long run (@_inlineable) by Marcin Krzyzanowskim ๐Ÿ‘


The @results attribute describes how a perform impacts “the state of the world”. Extra virtually how the optimizer can modify this system based mostly on data that’s offered by the attribute.

You will discover the corresponding docs right here.

@results(readonly) func foo() {  }


Mainly you’ll be able to power inlining with the @_transparent attribute, however please learn the unofficial documentation for more information.

func instance() {


With the @_specialize Swift attribute you can provide hints for the compiler by itemizing concrete varieties for the generic signature. Extra detailed docs are right here.

struct S<T> {
  var x: T
  @_specialize(the place T == Int, U == Float)
  mutating func exchangeSecond<U>(_ u: U, _ t: T) -> (U, T) {
    x = t
    return (u, x)


The Swift optimizer can detect code in the usual library whether it is marked with particular attributes @_semantics, that identifies the capabilities.

You possibly can examine semantics right here and right here, or inside this concurrency proposal.

func getCount() -> Int {
    return _buffer.rely


This attribute specifies the identify {that a} declaration could have at hyperlink time.

You possibly can examine it contained in the Commonplace Librery Programmers Handbook.

inner func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) {


Swift compiler comes with a built-in libFuzzer integration, which you should use with the assistance of the @_cdecl annotation. You possibly can be taught extra about libFuzzer right here.

@_cdecl("LLVMFuzzerTestOneInput") public func fuzzMe(Knowledge: UnsafePointer<CChar>, Dimension: CInt) -> CInt{

Unavailable, undocumented, unknown

As you’ll be able to see that is already fairly a listing, however there’s much more. Contained in the official Swift repository you could find the attr exams. In case you want extra information concerning the remaining Swift annotations you’ll be able to go immediately there and test the supply code feedback. In case you may assist me writing concerning the leftovers, please drop me a number of strains, I would actually respect any assist. ๐Ÿ˜‰๐Ÿ‘

  • @requiresstoredproperty_inits
  • @warnunqualifiedaccess
  • @fixedlayout
  • @_versioned
  • @showin_interface
  • @_alignment
  • @objcnonlazy_realization
  • @_frozen
  • @_optimize(none|pace|measurement)
  • @_weakLinked
  • @consuming
  • @_restatedObjCConformance
  • @_staticInitializeObjCMetadata
  • @setterAccess
  • @rawdoccomment
  • @objc_bridged
  • @noescape -> eliminated, see @escaping
  • @noreturn -> eliminated, see By no means sort
  • @downgradeexhaustivity_check -> no impact on swap case anymore?
  • @_implements(…) – @implements(Equatable, ==(:_:))
  • @swiftnativeobjcruntime_base(class)

The @_implements attribute, which treats a decl because the implementation for some named protocol requirement (however in any other case not-visible by that identify).

This attribute signifies a category that ought to be handled semantically as a local Swift root class, however which inherits a selected Goal-C class at runtime. For many courses that is the runtime’s “SwiftObject” root class. The compiler doesn’t must know concerning the class; it is the construct system’s accountability to hyperlink towards the ObjC code that implements the foundation class, and the ObjC implementation’s accountability to make sure cases start with a Swift-refcounting-compatible object header and override all the required NSObject refcounting strategies.

This permits us to subclass an Goal-C class and use the quick Swift reminiscence allocator.

If you wish to add some notes about these attributes, please contact me on twitter.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.