40

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 ?

Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • You don't explain though, you say `"We used to declare property to pass data between classes as following:"` followed by your question `"Now there is no interface class, how to pass data between .swift classes ?"` Where does it explain? – Popeye Jun 03 '14 at 12:18

6 Answers6

56

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.

Community
  • 1
  • 1
Cezar
  • 55,636
  • 19
  • 86
  • 87
  • 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. – Ruben Martinez Jr. Jul 17 '14 at 16:47
  • @RubenMartinezJr. You need to provide an initializer, or default values for each property. The code above is just to illustrate property declarations. – Cezar Jul 17 '14 at 18:18
  • 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 http://stackoverflow.com/questions/6843125/custom-setter-for-property) **Is there a Swift equivalent of this?** – Ky - Apr 01 '15 at 15:24
  • 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 – AMH Sep 16 '18 at 03:51
13

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.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
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).
newacct
  • 119,665
  • 29
  • 163
  • 224
1

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    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 – Mieczysław Daniel Dyba Jun 07 '14 at 22:54
0

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.

From the Swift Programming Book:

    struct FixedLengthRange {
    var firstValue: Int
    let length: Int
    }

    var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
0

I say : typealias is equivalent even more in swift for @synthesize

just look at this link : https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

enter image description here

AMH
  • 461
  • 5
  • 12