ios @property/@synthesize 等效于 swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24014800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:18:03  来源:igfitidea点击:

@property/@synthesize equivalent in swift

iosobjective-cswiftios8

提问by Mutawe

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

我们曾经声明property在类之间传递数据如下:

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

.m file (implementation file)
@synthesize topSpeed;

Now there is no interfaceclass, how to pass data between .swiftclasses ?

现在没有interface类,如何在.swift类之间传递数据?

回答by Cezar

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.

Swift 没有区分属性和实例变量(即属性的底层存储)。要定义属性,只需在类的上下文中声明一个变量。

A swift class is simply a ClassName.swift file.

一个 swift 类只是一个 ClassName.swift 文件。

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, internaland private) in Swift. By default every property is internal. See herefor more information.

您可以通过点表示法访问属性值。从 Xcode6 beta 4 开始,Swift 中也有访问修饰符(public,internalprivate)。默认情况下,每个属性都是internal. 请参阅此处了解更多信息。

For more information, refer to the Swift Programming Guide:

有关更多信息,请参阅Swift 编程指南

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.

存储属性和实例变量

如果您有使用 Objective-C 的经验,您可能知道它提供了两种方法来将值和引用存储为类实例的一部分。除了属性之外,您还可以使用实例变量作为存储在属性中的值的后备存储。

Swift 将这些概念统一到一个单一的属性声明中。Swift 属性没有相应的实例变量,并且不能直接访问属性的后备存储。这种方法避免了在不同上下文中如何访问值的混淆,并将属性的声明简化为单个明确的语句。有关属性的所有信息(包括其名称、类型和内存管理特征)都在一个位置中定义为类型定义的一部分。

回答by trojanfoe

Using Properties.

使用属性。

From the Swift Programming Guide:

来自Swift 编程指南

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.

存储属性和实例变量

如果您有使用 Objective-C 的经验,您可能知道它提供了两种方法来将值和引用存储为类实例的一部分。除了属性之外,您还可以使用实例变量作为存储在属性中的值的后备存储。

Swift 将这些概念统一到一个单一的属性声明中。Swift 属性没有相应的实例变量,并且不能直接访问属性的后备存储。这种方法避免了在不同上下文中如何访问值的混淆,并将属性的声明简化为单个明确的语句。有关属性的所有信息(包括其名称、类型和内存管理特征)都在一个位置中定义为类型定义的一部分。

回答by newacct

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

Objective-C 中的属性对应于 Swift 中的属性。在 Objective-C 和 Swift 中有两种实现属性的方法:

  1. Synthesized/auto-synthesized properties in Objective C -- these are called "stored properties" in Swift. You simply declare it with var topSpeed : Doubleor let topSpeed : Double = 4.2in 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 readwriteproperties), or var topSpeed : Double { getter code here }(for readonlyproperties).
  1. Objective C 中的合成/自动合成属性——这些在 Swift 中被称为“存储属性”。您只需在类声明中var topSpeed : Doublelet topSpeed : Double = 4.2在类声明中声明它,就像在函数体中声明局部变量一样。您无法指定支持实例变量的名称,因为目前 Swift 中没有实例变量。您必须始终使用该属性而不是其支持的实例变量。
  2. 在 Objective-C 中手动实现的属性——这些在 Swift 中被称为“计算属性”。您可以在类声明中声明它们,例如var topSpeed : Double { get { getter code here } set { setter code here } }(对于readwrite属性)或var topSpeed : Double { getter code here }(对于readonly属性)。

回答by Caleb

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 importa header file if you want to use symbols defined somewhere else. Instead, you importa module, like:

听起来您的问题至少有一部分与将给定类的接口与其他类进行通信有关。与 Java 一样(与 C、C++ 和 Objective-C 不同),Swift 没有将接口与实现分开。import如果您想使用在其他地方定义的符号,则不需要头文件。相反,您import是一个模块,例如:

import Foundation
import MyClass

To access properties in another class, import that class.

要访问另一个类中的属性,请导入该类。

回答by Erick Gonzales

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.

存储属性和实例变量

如果您有使用 Objective-C 的经验,您可能知道它提供了两种方法来将值和引用存储为类实例的一部分。除了属性之外,您还可以使用实例变量作为存储在属性中的值的后备存储。

Swift 将这些概念统一到一个单一的属性声明中。Swift 属性没有相应的实例变量,并且不能直接访问属性的后备存储。这种方法避免了在不同上下文中如何访问值的混淆,并将属性的声明简化为单个明确的语句。有关属性的所有信息(包括其名称、类型和内存管理特征)都在一个位置中定义为类型定义的一部分。

From the Swift Programming Book:

来自 Swift 编程书:

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

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

回答by AMH

I say : typealiasis equivalent even more in swift for @synthesize

我说: typealias在 swift 中更等效@synthesize

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

看看这个链接:https: //docs.swift.org/swift-book/ReferenceManual/Declarations.html

enter image description here

在此处输入图片说明