我们曾经声明 property
在类之间传递数据如下:
.h file (interface file)
@property (nonatomic) double topSpeed;
.m file (implementation file)
@synthesize topSpeed;
现在没有interface
类,.swift
类之间如何传递数据?
最佳答案
Swift 没有区分属性和实例变量(即属性的底层存储)。要定义属性,您只需在类的上下文中声明一个变量。
一个 swift 类只是一个 ClassName.swift 文件。
你声明一个类和属性为
class SomeClass {
var topSpeed: Double
var aStrProperty: String
var anIntProperty: Int
//Initializers and other functions
}
您可以通过点符号访问属性值。从 Xcode6 beta 4 开始,Swift 中还有访问修饰符(public
、internal
和 private
)。默认情况下,每个属性都是 internal
。 See here了解更多信息。
有关详细信息,请参阅 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.
https://stackoverflow.com/questions/24014800/