@property/@synthesize equivalent in swift

2020-01-29 04:37发布

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 ?

6条回答
虎瘦雄心在
2楼-- · 2020-01-29 04:51

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).
查看更多
手持菜刀,她持情操
3楼-- · 2020-01-29 04:59

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.

查看更多
劳资没心,怎么记你
4楼-- · 2020-01-29 05:03

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)
查看更多
淡お忘
5楼-- · 2020-01-29 05:04

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.

查看更多
姐就是有狂的资本
6楼-- · 2020-01-29 05:05

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

查看更多
Luminary・发光体
7楼-- · 2020-01-29 05:15

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.

查看更多
登录 后发表回答