What is the main difference between "static var" and "var" in Swift? Can someone explain this difference to me, possibly with a little example?
Swift中“static var”和“var”之间的主要区别是什么?有人可以用一个小例子来解释这个差异吗?
3 个解决方案
#1
10
static var
belongs to type itself while var
belongs to instance (specific value that is of specific type) of type. For example:
static var属于类型本身,而var属于类型的实例(特定类型的特定值)。例如:
struct Car {
static var numberOfWheels = 4
var plateNumber: String
}
Car.numberOfWheels = 3
let myCar = Car(plateNumber: "123456")
All cars has same amount of wheels. An you change it on type Car
itself.
所有汽车都有相同数量的车轮。您可以在Car类型上更改它。
In order to change plate number you need to have instance of Car
. For example, myCar
.
要更改车牌号,您需要拥有Car的实例。例如,myCar。
#2
1
A static var is property variable on a struct versus an instance of the struct. Note that static var can exist for an enum too.
静态var是结构上的属性变量,而不是结构的实例。请注意,枚举也可以存在静态var。
Example:
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
Notice the difference? bar is defined on an instance of the struct. Whereas foo is defined on the struct itself.
请注意区别? bar是在struct的实例上定义的。而foo是在结构本身上定义的。
#3
1
I'll give you a very nice Swifty example based on this post. Though this is a bit more sophisticated.
我将基于这篇文章给你一个非常好的Swifty示例。虽然这有点复杂。
Imagine you have a project in which you have 15 collectionViews in your app. For each you have to set the cellIdentifier & nibName. Do you really want to rewrite all code for your that 15 times?
想象一下,你有一个项目,你的应用程序中有15个collectionViews。对于每个,您必须设置cellIdentifier&nibName。你真的想要为你重写所有代码15次吗?
There is a very POP solution to your problem:
您的问题有一个非常POP解决方案:
Let's help ourselves by writing a protocol which returns a string version of our ClassName
让我们通过编写一个返回ClassName字符串版本的协议来帮助自己
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(Self)
}
}
extension BookCell : ReusableView{
}
The same for the nibName
of each custom cell you have created:
您创建的每个自定义单元格的nibName都是相同的:
protocol NibLoadableView: class {
static var nibName: String { get }
}
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String(Self)
}
}
extension BookCell: NibLoadableView {
}
so now where ever I need nibName
I would just do
所以现在我需要nibName,我会这么做
BookCell.nibName
And where ever I need cellIdentifier
I would just do:
在哪里我需要cellIdentifier我会这样做:
BookCell.defaultReuseIdentifier
Now specifically to your question. Do you think we need to change the cellIdentifier per each new instance of BookCell?! No! All cells of BookCell will have the same identifier. It's not something that would change per instance. As a result it's been made static
现在专门针对你的问题。你认为我们需要为每个新的BookCell实例更改cellIdentifier吗?!没有! BookCell的所有单元格都具有相同的标识符。这不是每个实例都会改变的东西。结果它变成了静态的
While I did answer your question, the solution to reducing the number of lines for the 15 collectionViews can still be significantly improved so do see the blog post linked.
虽然我确实回答了你的问题,但是减少15个collectionViews的行数的解决方案仍然可以得到显着改善,所以看到链接的博客文章。
That blog post has actually been turned into a video by NatashaTheRobot
那篇博文实际上已经变成NatashaTheRobot的视频
#1
10
static var
belongs to type itself while var
belongs to instance (specific value that is of specific type) of type. For example:
static var属于类型本身,而var属于类型的实例(特定类型的特定值)。例如:
struct Car {
static var numberOfWheels = 4
var plateNumber: String
}
Car.numberOfWheels = 3
let myCar = Car(plateNumber: "123456")
All cars has same amount of wheels. An you change it on type Car
itself.
所有汽车都有相同数量的车轮。您可以在Car类型上更改它。
In order to change plate number you need to have instance of Car
. For example, myCar
.
要更改车牌号,您需要拥有Car的实例。例如,myCar。
#2
1
A static var is property variable on a struct versus an instance of the struct. Note that static var can exist for an enum too.
静态var是结构上的属性变量,而不是结构的实例。请注意,枚举也可以存在静态var。
Example:
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
Notice the difference? bar is defined on an instance of the struct. Whereas foo is defined on the struct itself.
请注意区别? bar是在struct的实例上定义的。而foo是在结构本身上定义的。
#3
1
I'll give you a very nice Swifty example based on this post. Though this is a bit more sophisticated.
我将基于这篇文章给你一个非常好的Swifty示例。虽然这有点复杂。
Imagine you have a project in which you have 15 collectionViews in your app. For each you have to set the cellIdentifier & nibName. Do you really want to rewrite all code for your that 15 times?
想象一下,你有一个项目,你的应用程序中有15个collectionViews。对于每个,您必须设置cellIdentifier&nibName。你真的想要为你重写所有代码15次吗?
There is a very POP solution to your problem:
您的问题有一个非常POP解决方案:
Let's help ourselves by writing a protocol which returns a string version of our ClassName
让我们通过编写一个返回ClassName字符串版本的协议来帮助自己
protocol ReusableView: class {
static var defaultReuseIdentifier: String { get }
}
extension ReusableView where Self: UIView {
static var defaultReuseIdentifier: String {
return String(Self)
}
}
extension BookCell : ReusableView{
}
The same for the nibName
of each custom cell you have created:
您创建的每个自定义单元格的nibName都是相同的:
protocol NibLoadableView: class {
static var nibName: String { get }
}
extension NibLoadableView where Self: UIView {
static var nibName: String {
return String(Self)
}
}
extension BookCell: NibLoadableView {
}
so now where ever I need nibName
I would just do
所以现在我需要nibName,我会这么做
BookCell.nibName
And where ever I need cellIdentifier
I would just do:
在哪里我需要cellIdentifier我会这样做:
BookCell.defaultReuseIdentifier
Now specifically to your question. Do you think we need to change the cellIdentifier per each new instance of BookCell?! No! All cells of BookCell will have the same identifier. It's not something that would change per instance. As a result it's been made static
现在专门针对你的问题。你认为我们需要为每个新的BookCell实例更改cellIdentifier吗?!没有! BookCell的所有单元格都具有相同的标识符。这不是每个实例都会改变的东西。结果它变成了静态的
While I did answer your question, the solution to reducing the number of lines for the 15 collectionViews can still be significantly improved so do see the blog post linked.
虽然我确实回答了你的问题,但是减少15个collectionViews的行数的解决方案仍然可以得到显着改善,所以看到链接的博客文章。
That blog post has actually been turned into a video by NatashaTheRobot
那篇博文实际上已经变成NatashaTheRobot的视频