0

For a custom class, the following works fine. But I'm getting errors that I think are specific to NSUserDefaults

class NSUserDefaultsManager:NSObject {
    struct Static {
        static var udefaults = NSUserDefaults.standardUserDefaults()
    }  

    class var udefaults: [NSUserDefaults] {
        get { return Static. udefaults }
        set { Static. udefaults = newValue }
    }
}

On the get line, there is this error:

'NSUserDefaults' is not identical to 'AnyObject'

On the set line, I get this:

'NSArray' is not subtype of 'NSUserDefaults'

The goal is to access udefaults like this NSUserDefaults.udefaults. Do I need to create a property singleton for NSUserDefaults and access it like this NSUserDefaults.instance().udefaults?

Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
4thSpace
  • 43,672
  • 97
  • 296
  • 475

1 Answers1

-1

This seems to be impossible. What you are trying to do is to create an extension of NSUserDefaults. The code to do this would look like this:

extension NSUserDefaults
{
    static let udefaults = NSUserDefaults.standardUserDefaults()
}

However, Xcode complains with the following error message:

Extensions may not contain stored properties.

UPDATE

The following code compiles w/o any syntax errors, but it crashes Xcode :-)

extension NSUserDefaults
{
    class var shared : NSUserDefaults {
        struct Static {
            static let instance : NSUserDefaults = NSUserDefaults.standardUserDefaults()
        }
        return Static.instance
    }
}


var qq = NSUserDefaults.shared

The error message is "Execution was interrupted, reason SIGABRT."

Alexey
  • 1,177
  • 2
  • 14
  • 23
  • I don't want an extension. Just a singleton but I don't think you can do it with NSUserDefaults for some reason. – 4thSpace Mar 16 '15 at 03:43
  • If you want NSUserDefaults behave like a singleton then you have to add this behaviour , since it is not available in the original class. And when you add something to existing class, this is called extension. – Alexey Mar 16 '15 at 03:46
  • Are you saying this is specific to NSUserDefaults or all classes (even custom ones)? – 4thSpace Mar 16 '15 at 03:48
  • This is specific to standard library classes (UIKit, Foundation, etc.) . If you extend your own class, then it can contain additional properties. In the above example, replace "extension NSUserDefaults" with "extension MyClass" and it'll work (assuming you define MyClass somewhere) – Alexey Mar 16 '15 at 03:52
  • I have a custom class and to create the singleton, I do not have to extend it. I'm using the same code as shown above and it works fine. – 4thSpace Mar 16 '15 at 03:56
  • Yes, for a custom class you can use static property. Btw., it is not even necessary to create a nested Struct as in your example, since Swift 1.2 supports "static let" in classes. More info here: http://stackoverflow.com/a/24147830/995049 – Alexey Mar 16 '15 at 04:12
  • Sorry for bugging you, but approach #2 from the link above actually compiles , but crashes at run-time :-) Seems like a bug in Xcode 6.2. So, here what I tried: – Alexey Mar 16 '15 at 04:21
  • static properties are not allowed in iOS with Swift. Can I add other methods into the extension? – 4thSpace Mar 16 '15 at 04:31
  • Yes, functions can be added without problem. Just follow standard syntax inside extension: func myFunc() { ... } – Alexey Mar 16 '15 at 04:58