Skip to content

finder39/Swignals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swignals

Swignals is an observable pattern system written entirely in Swift.

CocoaPods Compatible Platform

Installing

You can either drag all the files from the Source folder into your project, or install it using CocoaPods.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 0.39.0+ is required to build Swignals.

To integrate Swignals into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Swignals', '~> 1.0'
end

Then, run the following command:

$ pod install

Example

Lets say we wanted to add a swignal to an AudioPlayer class whenever shuffle is set. It could look something like this:

typealias OnShuffleChangedSwignal = Swignal1Arg<Bool>

class AudioPlayer {
    static let sharedInstance = AudioPlayer()

    let onShuffleChanged = OnShuffleChangedSwignal()
    var shuffle: Bool = false {
        didSet {
            onShuffleChanged.fire(shuffle)
        }
    }
}

Then to subscribe to that signal you'd do the following:

class ControlsViewController: UIViewController {
    init() {
        AudioPlayer.sharedInstance.onShuffleChanged.addObserver(self) { (observer, arg1) in
        // note: you can rename the variables in the callback such as
        // callback: { (weakSelf, shuffle) in
            if let favoriteTracksDataSource = observer.tracksDataSource as? FavoriteTracksDataSource {
                favoriteTracksDataSource.shuffle = arg1
                favoriteTracksDataSource.refresh()
            }
        }
    }

    func updateViewBasedOnShuffle(shuffle: Bool) {
        // do important things
    }
}

Authors

Acknowledgements

License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published