Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Swiftish syntax? #98

Open
lexrus opened this issue Dec 24, 2015 · 2 comments
Open

Swiftish syntax? #98

lexrus opened this issue Dec 24, 2015 · 2 comments

Comments

@lexrus
Copy link

lexrus commented Dec 24, 2015

let tweak = FBTweak(identifier: "com.tweaks.example.advanced")
tweak.name = "Advanced settings"
tweak.defaultValue = false

let collection = FBTweakCollection(name: "Enable");
collection.addTweak(tweak)

let category = FBTweakCategory(name: "Settings")
category.addTweakCollection(collection);

let store = FBTweakStore.sharedInstance()
store.addTweakCategory(category)

tweak.addObserver(self)

// ...
func tweakDidChange(tweak: FBTweak!)
{
    self.advancedSettingsEnabled = tweak.currentValue as Bool;
}

Compared to:

FBTweakStore.sharedInstance()
  + FBTweakCategory(name: "Settings")
  + FBTweakCollection(name: "Enabled")
  + FBTweak<Bool>("com.fb.example", "Advanced settings", false) {
      tweak in
      self.advancedSettingsEnabled = tweak.currentValue
    }

And the rhs should support Array as well:

let collection = FBTweakCollection(name: "Enabled")
    + [
        FBTweak<Bool>("com.fb.example", "Advanced settings", false) {
          tweak in
          self.advancedSettingsEnabled = tweak.currentValue
        },
        // Another Tweak
      ]
@grp
Copy link
Contributor

grp commented Dec 24, 2015

I like that syntax!

The main barrier to Tweaks in Swift is that they aren't loaded at load time — the code has to run to initialize the tweak. Without a workaround for that, it's hard to make it nearly as practical as in Objective-C, since you'll often end up with duplicate tweaks (if you create them within an object) or a lot of boilerplate code to store static instances of them.

I haven't come up with a good way to work around that, but I'm still looking. If one is found, I think that will be a good impetus to make the rest of Tweaks work well in Swift too.

@dulacp
Copy link

dulacp commented Jan 26, 2016

Some time ago I came up with this implement for a Swift tweak syntax.

Still there is the fact that it needs to run to be loaded, but it doesn't result in duplicates.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants