2014-12-01 10 views

risposta

10

static var appartiene al tipo stesso mentre var appartiene all'istanza (valore specifico di tipo specifico) di tipo. Ad esempio:

struct Car { 
    static var numberOfWheels = 4 
    var plateNumber: String 
} 

Car.numberOfWheels = 3 
let myCar = Car(plateNumber: "123456") 

Tutte le auto hanno la stessa quantità di ruote. Un si cambia su tipo Car stesso.

Per modificare il numero di targa è necessario disporre dell'istanza di Car. Ad esempio, myCar.

+0

FYI myCar non ha alcuna proprietà di 'numberOfWheels'. Non esiste NESSUNA cosa "myCar.numberOfWheels". Perché perché è una proprietà della ** classe ** * non * dell'istanza ** **. Di conseguenza puoi fare "Car.numberOfWheels = 7' – Honey

1

Una variabile statica è una variabile di proprietà su una struct rispetto a un'istanza della struct. Notare che la variabile statica può esistere anche per un enum.

Esempio:

struct MyStruct { 
    static var foo:Int = 0 
    var bar:Int 
} 

println("MyStruct.foo = \(MyStruct.foo)") // Prints out 0 

MyStruct.foo = 10 

println("MyStruct.foo = \(MyStruct.foo)") // Prints out 10 

var myStructInstance = MyStruct(bar:12) 

// bar is not 
// println("MyStruct.bar = \(MyStruct.bar)") 

println("myStructInstance = \(myStructInstance.bar)") // Prints out 12 

notare la differenza? la barra è definita su un'istanza della struttura. Mentre foo è definito sulla struttura stessa.

1

Ti darò un bellissimo esempio Swifty basato su this post. Anche se questo è un po 'più sofisticato.

Immagina di avere un progetto in cui hai 15 raccolte di immagini nella tua app. Per ognuno devi impostare il cellIdentifier & nibName. Vuoi veramente riscrivere tutto il codice per il tuo 15 volte?

C'è una soluzione molto POP al tuo problema:

Aiutiamo noi stessi scrivendo un protocollo che restituisce una versione stringa del nostro NomeClasse

protocol ReusableView: class { 
    static var defaultReuseIdentifier: String { get } 
} 

extension ReusableView where Self: UIView { 
    static var defaultReuseIdentifier: String { 
     return String(Self) 
    } 
} 
extension BookCell : ReusableView{ 
} 

Lo stesso per la nibName di ogni cella personalizzato hanno creato:

protocol NibLoadableView: class { 
    static var nibName: String { get } 
} 

extension NibLoadableView where Self: UIView { 
    static var nibName: String { 
     return String(Self) 
    } 
} 

extension BookCell: NibLoadableView { 
} 

così ora dove mai ho bisogno nibName vorrei solo fare

BookCell.nibName 

E dove mai ho bisogno cellIdentifier vorrei solo fare:

BookCell.defaultReuseIdentifier 

Ora specificamente alla tua domanda. Pensi che abbiamo bisogno di cambiare il cellIdentifier per ogni nuova istanza di BookCell ?! No! Tutte le celle di BookCell avranno lo stesso identificatore . Non è qualcosa che cambierebbe per istanza. Di conseguenza è stato effettuato static

Mentre ho risposto alla tua domanda, la soluzione per ridurre il numero di righe per le 15 raccolte di immagini può ancora essere migliorata in modo significativo, quindi vedi il post del blog collegato.


Questo post sul blog è stato effettivamente trasformato in un video da NatashaTheRobot

+0

È un'ottima risposta POP. –

Problemi correlati