diff --git a/Sucrose.podspec b/Sucrose.podspec index b179025..4bd3515 100644 --- a/Sucrose.podspec +++ b/Sucrose.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.swift_version = "5.0" s.name = "Sucrose" - s.version = "3.2.0" + s.version = "3.3.0" s.summary = "🍬 Everyday sugar" s.description = "Collection of handy methods & objects" diff --git a/Sucrose.xcodeproj/project.pbxproj b/Sucrose.xcodeproj/project.pbxproj index 1796953..0a9ecf9 100644 --- a/Sucrose.xcodeproj/project.pbxproj +++ b/Sucrose.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 209692162252C57A007EF9DB /* Observable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 209692152252C57A007EF9DB /* Observable.swift */; }; 20D4D24021E67CEF00A3F80A /* Sucrose.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 20D4D23621E67CEF00A3F80A /* Sucrose.framework */; }; 20D4D25121E67E5600A3F80A /* Collection+SafeIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D4D25021E67E5600A3F80A /* Collection+SafeIndex.swift */; }; 20D4D25721E67ED000A3F80A /* NSObject+Create.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D4D25621E67ED000A3F80A /* NSObject+Create.swift */; }; @@ -44,6 +45,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 209692152252C57A007EF9DB /* Observable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Observable.swift; sourceTree = ""; }; 20D4D23621E67CEF00A3F80A /* Sucrose.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sucrose.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 20D4D23A21E67CEF00A3F80A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 20D4D23F21E67CEF00A3F80A /* SucroseTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SucroseTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -155,6 +157,7 @@ isa = PBXGroup; children = ( 20D4D26121E6800900A3F80A /* Weak.swift */, + 209692152252C57A007EF9DB /* Observable.swift */, ); path = Objects; sourceTree = ""; @@ -255,6 +258,7 @@ developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( + en, ); mainGroup = 20D4D22C21E67CEF00A3F80A; productRefGroup = 20D4D23721E67CEF00A3F80A /* Products */; @@ -301,6 +305,7 @@ 20D4D25921E67F0600A3F80A /* UIButton+Init.swift in Sources */, 20D4D26221E6800900A3F80A /* Weak.swift in Sources */, 20D4D25F21E67FAA00A3F80A /* UIViewController+Hierarchy.swift in Sources */, + 209692162252C57A007EF9DB /* Observable.swift in Sources */, 531857832215A5D700009D11 /* UIView+Nameable.swift in Sources */, 20D4D25B21E67F3D00A3F80A /* UIStackView+Hierarchy.swift in Sources */, 531857872215B5BC00009D11 /* UIView+Shape.swift in Sources */, diff --git a/Sucrose/Objects/Observable.swift b/Sucrose/Objects/Observable.swift new file mode 100644 index 0000000..b1cd52e --- /dev/null +++ b/Sucrose/Objects/Observable.swift @@ -0,0 +1,31 @@ +// +// Observable.swift +// Sucrose +// +// Created by Pedro Carrasco on 01/04/2019. +// Copyright © 2019 Pedro Carrasco. All rights reserved. +// + +import Foundation + +public final class Observable { + public typealias Observer = (T) -> () + + public var value: T { + didSet { observer?(value) } + } + + private var observer: Observer? + + public init(_ value: T) { + self.value = value + } +} + +extension Observable { + + public func subscribe(_ observer: Observer?) { + self.observer = observer + observer?(value) + } +}