Swift学习笔记

1. Mutability

Objective-C offers several classes in both “regular” and mutable versions,

such as NSString/NSMutableString, NSArray/NSMutableArray, and so on.

In Swift, mutability is determined when you create an instance, not by choice of class.

An instance is declared as being either a variable or constant, thus establishing whether it can or cannot be changed.

Variables are declared using the var keyword and are mutable.
Constants are immutable and declared using the let keyword.

In Swift, constants are used ubiquitously. Apple advises to always
declare a stored value as a constant when you know its
value is not going to change, because doing so aids performance

eg:

Variable    var valueThatMayChange = "Hello "
Constant   let valueThatWillNotChange = "Hello World"

var greeting: String = "Hello world"
var greeting = "Hello world"

以上两种都是可以的,指定和不指定类型

2. typedef

Objective-C:

typedef NSInteger VolumeLevel;
VolumeLevel volume = 0;

Swift:

typealias VolumeLevel = UInt
let volume = VolumeLevel.min
原文地址:https://www.cnblogs.com/davidgu/p/4766881.html