Swift’s Inherent Characteristics

Using the keyword static, you can declare both static properties and methods in Swift1.2[Xcode6.3]. However, when declaring static variables, it is not possible to use instance variables.

Question:

I’m attempting to transform the subsequent Objective-C syntax into Swift. Within the Objective-C code, there exists a
static variable
which is retrieved from a class function.

@implementation SomeClass
static NSMutableArray *_items;
+ (void)someMethod {
    [_items removeAll];
}
@end

Due to the inability to access the declared types, such as

private var items = [AnyObject]()

, from
class functions
in Swift, I resolved the issue by devising a stored property.

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

I am attempting to invoke a method on it through a class function.

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

The error message states that a variable of type ‘[AnyObject]’ cannot be mutated using the ‘removeAll’ method as it is an immutable value.

Could someone kindly explain the reason behind this error and provide guidance on how to fix it?

Thank you.


Solution 1:

With this code:

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

The property you’re creating is computed, not stored. However, each time you access it, a new instance of

[AnyObject]

is generated. As a result, any modifications made to it are lost once its reference goes out of scope.

The static computed property produces an unchangeable replica of the array created within its body, resulting in the inability to implement any of the array methods, including

mutating

and

removeAll

. The reason for this immutability is due to the absence of a setter and only the presence of a getter.

At present,
Swift classes
lacks support for
static properties
. However, a viable alternative is to employ structs, which I frequently use by defining an internal struct.

class SomeClass {
    struct Static {
        static var items = [AnyObject]()
    }
}
SomeClass.Static.items.append("test")

To avoid the need to reference the

Static

structure whenever you mention the

items

property, simply create a wrapper computed property.

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

To enable easier access to the property, it can be accessed more simply by:

SomeClass.items.append("test")


Solution 2:


The latest version is Swift 1.2.


Using the keyword “static” in Swift1.2[Xcode6.3], one can declare properties as well as functions. Both can be declared as either class or 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:


static

and

class

modifiers differ in that

static

is merely a replacement for “class final”, which implies that methods modified with

static

cannot be overridden in any subclasses.

Thanks @Maiaux’s


Solution 3:


The manual for Swift 2 specifies that only enumeration and structures can utilize static store properties.

Frequently Asked Questions

Posted in Ios