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 ?
Properties in Objective-C correspond to properties in Swift. There are two ways to implement properties in Objective-C and Swift:
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.var topSpeed : Double { get { getter code here } set { setter code here } }
(forreadwrite
properties), orvar topSpeed : Double { getter code here }
(forreadonly
properties).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, youimport
a module, like:To access properties in another class, import that class.
From the Swift Programming Book:
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
You access property values via dot notation. As of Xcode6 beta 4, there also are access modifiers (
public
,internal
andprivate
) in Swift. By default every property isinternal
. See here for more information.For more information, refer to the Swift Programming Guide:
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
Using Properties.
From the Swift Programming Guide: