Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fix warning? #2

Open
Igor-Poltavtsev opened this issue Jun 30, 2017 · 1 comment
Open

How to fix warning? #2

Igor-Poltavtsev opened this issue Jun 30, 2017 · 1 comment

Comments

@Igor-Poltavtsev
Copy link

Hi!
I get in line 66 this warning: "Operator should no longer be declared with body; use a precedence group instead".

How to fix it?

@DevAndArtist
Copy link
Owner

DevAndArtist commented Jun 30, 2017

@iOS-altima

Quick but not tested update for Swift 3. It also does not support any way to release the instances. You could implement it yourself ;)

public protocol Instantiatable { init() }
fileprivate var instances = [String: Instantiatable]()

infix operator <~

extension Instantiatable {

	public static var shared: Self { return set { $0 } }

	public static func set(with setter: (Self) -> Self) -> Self {
		guard let instance = instances["\(self)"] as? Self else {
			return setInstance(self.init(), with: setter, overridable: true)
		}
		return setInstance(instance, with: setter, overridable: false)
	}

	private static func setInstance(_ instance: Self,
	                                with setter: (Self) -> Self,
	                                overridable: Bool) -> Self {
		let instance = restoreInstanceIfNeeded(instance1: instance,
		                                       instance2: setter(instance),
		                                       overridable: overridable)
		instances["\(self)"] = instance
		return instance
	}

	private static func restoreInstanceIfNeeded(instance1 i1: Self,
	                                            instance2 i2: Self,
	                                            overridable: Bool) -> Self {
		guard type(of: i1) is AnyClass else { return i2 }
		return ((i1 as AnyObject) !== (i2 as AnyObject)) && !overridable ? i1 : i2
	}
}

extension Instantiatable {

	public static var instance: Self.Type { return self }

	@discardableResult
	public static func <~ (type: Self.Type, newInstance: Self) -> Self {
		return type.set { _ in newInstance }
	}
}

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

No branches or pull requests

2 participants