@property/@synthesize equivalent in swift

37,523

Solution 1

Swift provides no differentiation between properties and instance variables (i.e, the underlying store for a property). To define a property, you simply declare a variable in the context of a class.

A swift class is simply a ClassName.swift file.

You declare a class and properties as

class SomeClass {

  var topSpeed: Double
  var aStrProperty: String
  var anIntProperty: Int

  //Initializers and other functions

}

You access property values via dot notation. As of Xcode6 beta 4, there also are access modifiers (public, internal and private) in Swift. By default every property is internal. See here for more information.

For more information, refer to the Swift Programming Guide:

Stored Properties and Instance Variables

If you have experience with Objective-C, you may know that it provides two ways to store values and references as part of a class instance. In addition to properties, you can use instance variables as a backing store for the values stored in a property.

Swift unifies these concepts into a single property declaration. A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly. This approach avoids confusion about how the value is accessed in different contexts and simplifies the property’s declaration into a single, definitive statement. All information about the property—including its name, type, and memory management characteristics—is defined in a single location as part of the type’s definition.

Solution 2

Using Properties.

From the Swift Programming Guide:

Stored Properties and Instance Variables

If you have experience with Objective-C, you may know that it provides two ways to store values and references as part of a class instance. In addition to properties, you can use instance variables as a backing store for the values stored in a property.

Swift unifies these concepts into a single property declaration. A Swift property does not have a corresponding instance variable, and the backing store for a property is not accessed directly. This approach avoids confusion about how the value is accessed in different contexts and simplifies the property’s declaration into a single, definitive statement. All information about the property—including its name, type, and memory management characteristics—is defined in a single location as part of the type’s definition.

Solution 3

Properties in Objective-C correspond to properties in Swift. There are two ways to implement properties in Objective-C and Swift:

  1. Synthesized/auto-synthesized properties in Objective C -- these are called "stored properties" in Swift. You simply declare it with var topSpeed : Double or let topSpeed : Double = 4.2 in a class declaration, exactly as you would declare a local variable in a function body. You don't get to specify the name of the backing instance variable because, well, there are currently no instance variables in Swift. You must always use the property instead of its backing instance variable.
  2. Manually implemented properties in Objective-C -- these are called "computed properties" in Swift. You declare them in the class declaration like var topSpeed : Double { get { getter code here } set { setter code here } } (for readwrite properties), or var topSpeed : Double { getter code here } (for readonly properties).

Solution 4

It sounds like at least part of your question relates to communicating a given class's interface to other classes. Like Java (and unlike C, C++, and Objective-C), Swift doesn't separate the interface from the implementation. You don't import a header file if you want to use symbols defined somewhere else. Instead, you import a module, like:

import Foundation
import MyClass

To access properties in another class, import that class.

Share:
37,523
Mutawe
Author by

Mutawe

I am a 29 year, Lead Mobile applications developer. I have a passion for the iOS platform and an obsession about making every single detail of an application right. Readily embracing cutting-edge technology, I have gained an in-depth understanding of the main mobile platforms and recognise the subtle differences between each. Having helped develop a series of commercially successful apps on iOS, I am seeking a new opportunity that will allow me to further expand on these skills. SOreadytohelp

Updated on September 16, 2020

Comments

  • Mutawe
    Mutawe over 3 years

    We used to declare property to pass data between classes as following:

    .h file (interface file)
    @property (nonatomic) double topSpeed;
    
    .m file (implementation file)
    @synthesize topSpeed;
    

    Now there is no interface class, how to pass data between .swift classes ?

  • Mieczysław Daniel Dyba
    Mieczysław Daniel Dyba almost 10 years
    If you have a class such as MySimpleViewController.swift, you DO NOT need to add import MySimpleViewController. Xcode should automatically pick up your class definition. With Xcode6 beta (6A215l), you may encounter an error message stating that the type MySimpleViewController is not declared when using it inside of another class definition. Just be sure to clean and rebuild your project: SHIFT+CMD+K and CMD+B
  • Ruben Martinez Jr.
    Ruben Martinez Jr. almost 10 years
    Thanks! I'm trying to do this in that notation but I keep getting Class 'MyClass' has no initializers error message on the class declaration line.
  • Cezar
    Cezar almost 10 years
    @RubenMartinezJr. You need to provide an initializer, or default values for each property. The code above is just to illustrate property declarations.
  • Ky -
    Ky - about 9 years
    Can their getters/setters be overridden? For instance, Obj-C @property (readwrite) Type *name comes with a name getter and a setName setter, which can be called with Type *newVar = holder.name; and holder.name = newVar; or Type *newVar = [holder name]; and [holder setName: newVar];. This means if I want to add special behavior to these, all I must do is create a - (Type) name andor - (void) setName: (Type *)newName;. (see also stackoverflow.com/questions/6843125/custom-setter-for-proper‌​ty) Is there a Swift equivalent of this?
  • AMH
    AMH over 5 years
    I think this could be wrong as we use @synthesize to rename variable if we wish to change access level there are other ways in Objective-c then we can do it in other way