ios - swift 中的@property/@synthesize 等价物

我们曾经声明 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 中还有访问修饰符(publicinternalprivate)。默认情况下,每个属性都是 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/

相关文章:

iphone - NSDateFormatter,我做错了什么还是这是一个错误?

objective-c - 为什么不鼓励使用伞式框架?

ios - 切换隐私设置将杀死应用程序

ios - 如何在 iOS 中使用 'Container View'?

iphone - Objective-C 中的实例变量是否默认设置为 nil?

iphone - Objective-C - 将 float 转换为字符串的其他方法

iphone - Objective-C 实现文件中方法名后的分号

iphone - 如何以编程方式创建 UIScrollView?

objective-c - 处理由于 iOS 通讯录 API 中的链接卡导致的重复联系人

ios - 带有 AVPlayer 的多个视频