Swift中的静态属性 [英] Static properties in Swift

查看:117
本文介绍了Swift中的静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将以下Objective-C代码转换为Swift。在我的Objective-C代码中,有一个静态变量,并从类方法访问。

  @implementation SomeClass 

static NSMutableArray * _items;

+(void)someMethod {
[_items removeAll];
}

@end

类型声明为像这样 private var items = [AnyObject]()从类函数在Swift,我创建了一个存储属性像这样。



class var items:[AnyObject] {
return [AnyObject]()
}



我想从类函数中调用一个方法。

  class func someFunction(){
items.removeAll(keepCapacity:false)
}

但我收到此错误类型[AnyObject]的不可变值只有名为removeAll的变异成员



任何人都可以告诉我这个错误的原因是什么以及如何更正?



谢谢。

解决方案

使用此代码:

  AnyObject] {
return [AnyObject]()
}

创建一个存储属性 - 而是一个计算属性,最糟糕的部分是,每次你访问它,创建一个新的实例 [AnyObject] ,所以无论什么



对于错误,static计算属性返回一个不可变的数组副本,你创建的数组因此您不能使用任何声明为 mutating 的数组方法 - 和 removeAll 是其中之一。它不可变的原因是因为你已经定义了一个getter,而不是一个setter。



目前Swift类不支持静态属性,但是结构体我经常使用是定义一个内部结构:

  class SomeClass {
struct Static {
static var items = [AnyObject]()
}
}

SomeClass.Static.items.append(test)
pre>

如果你想要每次引用 Static > items 属性,只需定义一个包装器计算属性:

  class var items:[AnyObject] 
get {return Static.items}
set {Static.items = newValue}
}

,使得该属性可以更简单地访问:

  SomeClass.items.append )


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.

@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.

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'.

Can anyone please tell me what's the cause of this error and how to correct it?

Thank you.

解决方案

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.

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.

Currently Swift classes don't support static properties, but structs do - the workaround I often use is to define an inner struct:

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:

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")

这篇关于Swift中的静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆