ios Swift 中的静态属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26567480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static properties in Swift
提问by Isuru
I'm trying to convert the following Objective-C code to Swift. In my Objective-C code, there's a static variable and its accessed from a class method.
我正在尝试将以下 Objective-C 代码转换为 Swift。在我的 Objective-C 代码中,有一个静态变量,它是从类方法访问的。
@implementation SomeClass
static NSMutableArray *_items;
+ (void)someMethod {
[_items removeAll];
}
@end
Since you can't access types declared like this private var items = [AnyObject]()
from class functions in Swift, I created a stored property for it like this.
由于您无法private var items = [AnyObject]()
从 Swift 中的类函数访问这样声明的类型,因此我为它创建了一个这样的存储属性。
class var items: [AnyObject] {
return [AnyObject]()
}
And I'm trying to call a method on it from a class function like so.
我试图从这样的类函数中调用一个方法。
class func someFunction() {
items.removeAll(keepCapacity: false)
}
But I get this error Immutable value of type '[AnyObject]' only has mutating members named 'removeAll'.
但我收到此错误'[AnyObject]' 类型的不可变值仅具有名为 'removeAll' 的变异成员。
Can anyone please tell me what's the cause of this error and how to correct it?
谁能告诉我这个错误的原因是什么以及如何纠正它?
Thank you.
谢谢你。
回答by Antonio
With this code:
使用此代码:
class var items: [AnyObject] {
return [AnyObject]()
}
you are not creating a stored property - instead it's a computed property, and the worst part is that every time you access to it, a new instance of [AnyObject]
is created, so whatever you add to it, it's lost as soon as its reference goes out of scope.
您不是在创建存储属性 - 相反,它是一个计算属性,最糟糕的是,每次访问它时,[AnyObject]
都会创建一个新实例,因此无论您添加什么,只要引用消失,它就会丢失的范围。
As for the error, the static computed property returns an immutable copy of the array that you create in its body, so you cannot use any of the array method declared as mutating
- and removeAll
is one of them. The reason why it is immutable is because you have defined a getter, but not a setter.
至于错误,静态计算属性返回您在其主体中创建的数组的不可变副本,因此您不能使用任何声明为mutating
-的数组方法,并且removeAll
是其中之一。它不可变的原因是因为您定义了一个 getter,而不是一个 setter。
Currently Swift classes don't support static properties, but structs do - the workaround I often use is to define an inner struct:
目前 Swift 类不支持静态属性,但结构支持 - 我经常使用的解决方法是定义一个内部结构:
class SomeClass {
struct Static {
static var items = [AnyObject]()
}
}
SomeClass.Static.items.append("test")
If you want to get rid of the Static
struct every time you refer to the items
property, just define a wrapper computed property:
如果你想在Static
每次引用items
属性时都去掉结构体,只需定义一个包装器计算属性:
class var items: [AnyObject] {
get { return Static.items }
set { Static.items = newValue }
}
so that the property can be accessed more simply as:
以便可以更简单地访问该属性:
SomeClass.items.append("test")
回答by tounaobun
Updated to Swift1.2
更新到 Swift1.2
In Swift1.2[Xcode6.3], you can declare static properties using keyword static, also you can declare static methods using keyword class or static.
在Swift1.2[Xcode6.3]中,可以使用关键字static声明静态属性,也可以使用关键字class或static声明静态方法。
class SomeClass {
// use static modifier to declare static properties.
static var items: [AnyObject]!
// use class modifier to declare static methods.
class func classMethod() {
items.removeAll(keepCapacity: false)
}
// use static modifier to declare static methods.
static func staticMethod() {
items.removeAll(keepCapacity: false)
}
}
EDIT:
编辑:
The difference between static
and class
modifier is that static
is just an alias for "class final",so methods modified with static
can not be overridden in subclasses.
static
和class
修饰符的区别在于它static
只是“class final”的别名,所以修饰的方法static
不能在子类中被覆盖。
Thanks @Maiaux's
谢谢@Maiaux
回答by user1785898
Yet the manual for Swift 2 still claims just enumeration ond structures may use static store properities.
然而 Swift 2 的手册仍然声称只是枚举和结构可以使用静态存储属性。