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
@property/@synthesize equivalent in swift
提问by Mutawe
We used to declare property
to 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 interface
class, how to pass data between .swift
classes ?
现在没有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
, internal
and private
) in Swift. By default every property is internal
. See herefor more information.
您可以通过点表示法访问属性值。从 Xcode6 beta 4 开始,Swift 中也有访问修饰符(public
,internal
和private
)。默认情况下,每个属性都是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 中有两种实现属性的方法:
- Synthesized/auto-synthesized properties in Objective C -- these are called "stored properties" in Swift. You simply declare it with
var topSpeed : Double
orlet 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. - 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 } }
(forreadwrite
properties), orvar topSpeed : Double { getter code here }
(forreadonly
properties).
- Objective C 中的合成/自动合成属性——这些在 Swift 中被称为“存储属性”。您只需在类声明中
var topSpeed : Double
或let topSpeed : Double = 4.2
在类声明中声明它,就像在函数体中声明局部变量一样。您无法指定支持实例变量的名称,因为目前 Swift 中没有实例变量。您必须始终使用该属性而不是其支持的实例变量。 - 在 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 import
a header file if you want to use symbols defined somewhere else. Instead, you import
a 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 : typealias
is 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