0

Hi I have this extension for NSUserDefaults and I need to access it using Obj-C.

Struct in Extension

public struct UserDefaultsEntry {
    let key: String

    func get() -> AnyObject? {
        return NSUserDefaults.standardUserDefaults().objectForKey(key)
    }

    func set(value: AnyObject) {
        switch value {
        case is Bool:
            NSUserDefaults.standardUserDefaults().setBool(value as! Bool, forKey: key)
            break
        case is String:
            NSUserDefaults.standardUserDefaults().setObject(value as! String, forKey: key)
            break
        case is NSData:
            NSUserDefaults.standardUserDefaults().setObject(value as! NSData, forKey: key)
            break
        default:
            break
        }
    }

    func remove() {
        NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
    }
}

Static Variable in same extension class

public static let testDefault = UserDefaultsEntry(key: "testDefault")

so I can call it normally in swift class by using

let string = "test"
NSUserDefaults.testDefault.set(string)

but failed to call it in Obj-C class. So how can I call it? Thanks in advance

EDIT: Tried to use

[NSUserDefaults.testDefault get];

but I'm getting

Property 'testDefault' not found on object type 'NSUserDefaults'

0

1 Answer 1

2

Structs are not available in Objective-C. For code in Swift to be available in Objective-C, it must be a class that subclasses NSObject. testDefault is a struct, and therefore, can't be seen by Objective-C.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.