PAPreferences icon indicating copy to clipboard operation
PAPreferences copied to clipboard

How to make PAPreferences support Swift?

Open Fykec opened this issue 11 years ago • 2 comments

I want inherit one swift class from PAPreferences, but don't know how to deal with @dymamic

class MUSPreferences : PAPreferences
{
    @dynamic var firstVersionInstalled:String

    @dynamic var latestVersionInstalled:String
    ...
}

cause the init method in PAPreferences. will dynamic assign all value from NSUserDefault to the property, so I don't need to set them in the MUSPreferences's init(), but if I don't set them, xcode will report error "Property xxx not intiialized at super.init call", Do you have some solution to deal with this problem?

Fykec avatar Aug 04 '14 08:08 Fykec

Currently (as of Beta 5), although the Swift language has a dynamic modifier, it's not correctly marking the property as dynamic in the sense that Obj-C expects. Because of this, Swift dynamic properties don't work.

The short-term workaround is to simply have a PAPreferences subclass written in Objective-C and use that from Swift.

Once the Swift runtime settles down a bit, I'll investigate directly compatibility again.

dhennessy avatar Aug 04 '14 21:08 dhennessy

Thanks!

class MUSPreferences : PAPreferences
{
    dynamic var firstVersionInstalled:String?
    dynamic var extensionIntraEmphasis:Bool = false
    init()
    {
        super.init()
        let version = NSBundle.mainBundle().infoDictionary["CFBundleVersion"]  as AnyObject!  as String
        self.firstVersionInstalled = version
    }
}

Now, I workaround with set the type with "?" or give it a default value, just make it build ok,

Fykec avatar Aug 05 '14 04:08 Fykec