Later, I will summarize the notes and deepen the study of Swift. Hopefully, developers who already have objective-c experience will be able to learn swift faster. We learn together and savor Swift's subtlety.
conclusion at the beginning: I think Swift is more elegant, safer and more modern and sexier than objective-c.
First, learn the syntax differences from objective-c to Swift. How the Objective-c features we are familiar with are shown in Swift:
1. Property and instance variables (instance variable)
Objective-c Property in Swift World
In the process of cocoa world development, the most common thing we deal with is property.
The typical declarations are:
1 |
@property (strong,nonatomic) NSString *string; |
And in swift, after getting rid of the burden of C, it becomes more refined, and we just need to declare it directly in the class.
123 |
class Shape { var name = "shape" } |
Notice here that we no longer need @property instructions, and in objective-c we can specify the property attribute, such as strong,weak,readonly.
In Swift's world, we declare the nature of these properties in other ways.
A few things to note:
1 |
weak var delegate: UITextFieldDelegate? |
Readonly,readwrie directly by declaring a constant let, declaring the variable var to indicate
Copy is declared through the @nscopying directive.
It is important to note that String,array and dictionary appear in Swift as value type (value types) instead of reference type (reference type), so they are copied in terms of assignment, initialization, and parameter passing (in simple terms, String,array,dictionary is implemented by struct in swift)
Nonatomic,atomic all the swift properties are nonatomic. But we already have many mechanisms on the thread security, such as NSLOCK,GCD related APIs. The reason for this is that Apple wants to get rid of the very few features that are used, and thread safety gives us more mechanisms to deal with.
It is then worth noting that in objective-c, we can cross the property directly with the instance variable, while in Swift it is not possible.
For example: we may not need to declare the somestring as a property and use it directly. Even if we declare otherstring as property, we can use _otherstring directly with the instance variables behind the property.
1234 |
@interface SomeClass : NSObject { NSString *someString; } @property(nonatomic, copy) NSString* otherString; |
In Swift, we cannot deal directly with instance variable. That is, the way we declare it is simplified to a simple one, in short, in Swift, we only deal with property.
A Swift property does not has a corresponding instance variable, and the backing store for a property was not accessed Dir ectly
Summary
As a result, the development controversy that was led by the previous OC was no longer disputed, and in Swift's world we only deal with property.
And we can be resolved and unified in OC where Init and dealloc cannot be set using attribute Self.property = XXX.
Personally, this seemingly small change makes swift development more secure and more unified and stable in the style of the code.
Swift Property Extension:
In swift, property is divided into two categories: Stored properties and computed properties simply, is Stored properties can save values, and computed Properties only provide getter and setter, using stored properties to generate their own values. Personal feelings computed properties are more like methods than traditional meanings. But the existence of such a feature makes it easier for us to organize our code.
Swift provides a way to define class variables at the language level.
In C and objective-c, you define the static constants and variables associated with a type as global static variables. In Swift, however, type properties is written as part of the type ' s definition, within the type ' s outer curly braces, and Each of the Type property are explicitly scoped to the type it supports.
In Objective-c, we can only construct class variables by using singleton or static variable-class methods:
123456789101112 |
@interface Model + (int) value; + (void) setValue:(int)val; @end @implementation Model static int value; + (int) value { @synchronized(self) { return value; } } + (void) setValue:(int)val { @synchronized(self) { value = val; } } @end |
12345678910111213141516171819 |
// Foo.h
@interface Foo {
}
+(NSDictionary*) dictionary;
// Foo.m
+(NSDictionary*) dictionary
{
static NSDictionary* fooDict = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
// create dict
});
return
fooDict;
}
|
In swift, we can define class variables with clear syntax:
class variables defined by static cannot be overridden in subclasses, and class variables defined by class can be overridden in subclasses.
123456789 |
struct SomeStructure {
static
var
storedTypeProperty =
"Some value."
static
var
computedTypeProperty: Int {
return
1
}
class
var
overrideableComputedTypeProperty: Int {
return
107
}
}
|
We also have a more elegant singleton pattern implementation with the use of class variables:
1234 |
class singletonClass { static let sharedInstance = singletonClass() private init() {} // 这就阻止其他对象使用这个类的默认的‘()‘初始化方法 } |
Extension: The stored properties type in the currently supported type Propertis in Swift is not a class variable in the traditional sense (class variable) and cannot be defined by the class keyword. A class variable defined by static is similar to a class variable in Java and cannot be inherited, and the parent class and the subclass's class variable point to the same static variable.
123456 |
class SomeStructure { class var storedTypeProperty = "Some value." } //Swift 2.0 Error: Class stored properties not yet supported in classes |
The compiler throws an error message and believes that the type properties will be perfected in a future release.
Swift Learning (i)