From efc7f11f86f369545b235431362d880791fa91c2 Mon Sep 17 00:00:00 2001 From: Viktor Radulov Date: Wed, 20 Jan 2021 16:36:03 +0200 Subject: [PATCH] Added basic magic --- .gitignore | 10 + KVOMagic/KVOMagic.xcodeproj/project.pbxproj | 497 ++++++++++++++++++++ KVOMagic/KVOMagic/ArrayKVO.swift | 81 ++++ KVOMagic/KVOMagic/Info.plist | 22 + KVOMagic/KVOMagic/KVOMagic.h | 18 + KVOMagic/KVOMagic/ObservingUtils.swift | 190 ++++++++ KVOMagic/KVOMagic/PropertyWrappers.swift | 336 +++++++++++++ KVOMagic/KVOMagic/Utils/Operators.swift | 14 + KVOMagic/KVOMagic/Utils/Optionals.swift | 18 + KVOMagic/KVOMagicTests/Info.plist | 22 + KVOMagic/KVOMagicTests/KVOMagicTests.swift | 33 ++ LICENSE | 201 ++++++++ 12 files changed, 1442 insertions(+) create mode 100644 .gitignore create mode 100644 KVOMagic/KVOMagic.xcodeproj/project.pbxproj create mode 100644 KVOMagic/KVOMagic/ArrayKVO.swift create mode 100644 KVOMagic/KVOMagic/Info.plist create mode 100644 KVOMagic/KVOMagic/KVOMagic.h create mode 100644 KVOMagic/KVOMagic/ObservingUtils.swift create mode 100644 KVOMagic/KVOMagic/PropertyWrappers.swift create mode 100644 KVOMagic/KVOMagic/Utils/Operators.swift create mode 100644 KVOMagic/KVOMagic/Utils/Optionals.swift create mode 100644 KVOMagic/KVOMagicTests/Info.plist create mode 100644 KVOMagic/KVOMagicTests/KVOMagicTests.swift create mode 100644 LICENSE diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b64cd6a --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +build/* +*.pbxuser +*.perspectivev3 +*.mode1v3 +*.xccheckout +xcuserdata +project.xcworkspace +KVOMagic.xcscmblueprint +Adobe After Effects Auto-Save +*.aep Logs diff --git a/KVOMagic/KVOMagic.xcodeproj/project.pbxproj b/KVOMagic/KVOMagic.xcodeproj/project.pbxproj new file mode 100644 index 0000000..0f99aed --- /dev/null +++ b/KVOMagic/KVOMagic.xcodeproj/project.pbxproj @@ -0,0 +1,497 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + B9FD2A9725B70EBE006A31AE /* KVOMagic.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B9FD2A8D25B70EBE006A31AE /* KVOMagic.framework */; }; + B9FD2A9C25B70EBE006A31AE /* KVOMagicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FD2A9B25B70EBE006A31AE /* KVOMagicTests.swift */; }; + B9FD2A9E25B70EBE006A31AE /* KVOMagic.h in Headers */ = {isa = PBXBuildFile; fileRef = B9FD2A9025B70EBE006A31AE /* KVOMagic.h */; settings = {ATTRIBUTES = (Public, ); }; }; + B9FD2AAA25B70F0E006A31AE /* ObservingUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FD2AA925B70F0E006A31AE /* ObservingUtils.swift */; }; + B9FD2AAF25B71107006A31AE /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FD2AAE25B71106006A31AE /* Operators.swift */; }; + B9FD2AB325B711C6006A31AE /* ArrayKVO.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FD2AB225B711C6006A31AE /* ArrayKVO.swift */; }; + B9FD2AB725B7120A006A31AE /* PropertyWrappers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FD2AB625B7120A006A31AE /* PropertyWrappers.swift */; }; + B9FD2ACF25B72218006A31AE /* Optionals.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FD2ACE25B72218006A31AE /* Optionals.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + B9FD2A9825B70EBE006A31AE /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = B9FD2A8425B70EBE006A31AE /* Project object */; + proxyType = 1; + remoteGlobalIDString = B9FD2A8C25B70EBE006A31AE; + remoteInfo = KVOMagic; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + B9FD2A8D25B70EBE006A31AE /* KVOMagic.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KVOMagic.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + B9FD2A9025B70EBE006A31AE /* KVOMagic.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KVOMagic.h; sourceTree = ""; }; + B9FD2A9125B70EBE006A31AE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + B9FD2A9625B70EBE006A31AE /* KVOMagicTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KVOMagicTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + B9FD2A9B25B70EBE006A31AE /* KVOMagicTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KVOMagicTests.swift; sourceTree = ""; }; + B9FD2A9D25B70EBE006A31AE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + B9FD2AA925B70F0E006A31AE /* ObservingUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservingUtils.swift; sourceTree = ""; }; + B9FD2AAE25B71106006A31AE /* Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = ""; }; + B9FD2AB225B711C6006A31AE /* ArrayKVO.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayKVO.swift; sourceTree = ""; }; + B9FD2AB625B7120A006A31AE /* PropertyWrappers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropertyWrappers.swift; sourceTree = ""; }; + B9FD2ACE25B72218006A31AE /* Optionals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Optionals.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + B9FD2A8A25B70EBE006A31AE /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + B9FD2A9325B70EBE006A31AE /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + B9FD2A9725B70EBE006A31AE /* KVOMagic.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + B9FD2A8325B70EBE006A31AE = { + isa = PBXGroup; + children = ( + B9FD2A8F25B70EBE006A31AE /* KVOMagic */, + B9FD2A9A25B70EBE006A31AE /* KVOMagicTests */, + B9FD2A8E25B70EBE006A31AE /* Products */, + ); + sourceTree = ""; + }; + B9FD2A8E25B70EBE006A31AE /* Products */ = { + isa = PBXGroup; + children = ( + B9FD2A8D25B70EBE006A31AE /* KVOMagic.framework */, + B9FD2A9625B70EBE006A31AE /* KVOMagicTests.xctest */, + ); + name = Products; + sourceTree = ""; + }; + B9FD2A8F25B70EBE006A31AE /* KVOMagic */ = { + isa = PBXGroup; + children = ( + B9FD2AA925B70F0E006A31AE /* ObservingUtils.swift */, + B9FD2AB225B711C6006A31AE /* ArrayKVO.swift */, + B9FD2AB625B7120A006A31AE /* PropertyWrappers.swift */, + B9FD2A9025B70EBE006A31AE /* KVOMagic.h */, + B9FD2A9125B70EBE006A31AE /* Info.plist */, + B9FD2AAD25B710DD006A31AE /* Utils */, + ); + path = KVOMagic; + sourceTree = ""; + }; + B9FD2A9A25B70EBE006A31AE /* KVOMagicTests */ = { + isa = PBXGroup; + children = ( + B9FD2A9B25B70EBE006A31AE /* KVOMagicTests.swift */, + B9FD2A9D25B70EBE006A31AE /* Info.plist */, + ); + path = KVOMagicTests; + sourceTree = ""; + }; + B9FD2AAD25B710DD006A31AE /* Utils */ = { + isa = PBXGroup; + children = ( + B9FD2AAE25B71106006A31AE /* Operators.swift */, + B9FD2ACE25B72218006A31AE /* Optionals.swift */, + ); + path = Utils; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + B9FD2A8825B70EBE006A31AE /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + B9FD2A9E25B70EBE006A31AE /* KVOMagic.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + B9FD2A8C25B70EBE006A31AE /* KVOMagic */ = { + isa = PBXNativeTarget; + buildConfigurationList = B9FD2AA125B70EBE006A31AE /* Build configuration list for PBXNativeTarget "KVOMagic" */; + buildPhases = ( + B9FD2A8825B70EBE006A31AE /* Headers */, + B9FD2A8925B70EBE006A31AE /* Sources */, + B9FD2A8A25B70EBE006A31AE /* Frameworks */, + B9FD2A8B25B70EBE006A31AE /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = KVOMagic; + productName = KVOMagic; + productReference = B9FD2A8D25B70EBE006A31AE /* KVOMagic.framework */; + productType = "com.apple.product-type.framework"; + }; + B9FD2A9525B70EBE006A31AE /* KVOMagicTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = B9FD2AA425B70EBE006A31AE /* Build configuration list for PBXNativeTarget "KVOMagicTests" */; + buildPhases = ( + B9FD2A9225B70EBE006A31AE /* Sources */, + B9FD2A9325B70EBE006A31AE /* Frameworks */, + B9FD2A9425B70EBE006A31AE /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + B9FD2A9925B70EBE006A31AE /* PBXTargetDependency */, + ); + name = KVOMagicTests; + productName = KVOMagicTests; + productReference = B9FD2A9625B70EBE006A31AE /* KVOMagicTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + B9FD2A8425B70EBE006A31AE /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1230; + LastUpgradeCheck = 1230; + TargetAttributes = { + B9FD2A8C25B70EBE006A31AE = { + CreatedOnToolsVersion = 12.3; + LastSwiftMigration = 1230; + }; + B9FD2A9525B70EBE006A31AE = { + CreatedOnToolsVersion = 12.3; + }; + }; + }; + buildConfigurationList = B9FD2A8725B70EBE006A31AE /* Build configuration list for PBXProject "KVOMagic" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = B9FD2A8325B70EBE006A31AE; + productRefGroup = B9FD2A8E25B70EBE006A31AE /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + B9FD2A8C25B70EBE006A31AE /* KVOMagic */, + B9FD2A9525B70EBE006A31AE /* KVOMagicTests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + B9FD2A8B25B70EBE006A31AE /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + B9FD2A9425B70EBE006A31AE /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + B9FD2A8925B70EBE006A31AE /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B9FD2ACF25B72218006A31AE /* Optionals.swift in Sources */, + B9FD2AB325B711C6006A31AE /* ArrayKVO.swift in Sources */, + B9FD2AAA25B70F0E006A31AE /* ObservingUtils.swift in Sources */, + B9FD2AB725B7120A006A31AE /* PropertyWrappers.swift in Sources */, + B9FD2AAF25B71107006A31AE /* Operators.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + B9FD2A9225B70EBE006A31AE /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B9FD2A9C25B70EBE006A31AE /* KVOMagicTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + B9FD2A9925B70EBE006A31AE /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = B9FD2A8C25B70EBE006A31AE /* KVOMagic */; + targetProxy = B9FD2A9825B70EBE006A31AE /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin XCBuildConfiguration section */ + B9FD2A9F25B70EBE006A31AE /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.15; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Debug; + }; + B9FD2AA025B70EBE006A31AE /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.15; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Release; + }; + B9FD2AA225B70EBE006A31AE /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = KVOMagic/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 10.12; + MARKETING_VERSION = 0.1.0; + PRODUCT_BUNDLE_IDENTIFIER = dev.golova.KVOMagic; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + B9FD2AA325B70EBE006A31AE /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + APPLICATION_EXTENSION_API_ONLY = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEFINES_MODULE = YES; + DEVELOPMENT_TEAM = ""; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + INFOPLIST_FILE = KVOMagic/Info.plist; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 10.12; + MARKETING_VERSION = 0.1.0; + PRODUCT_BUNDLE_IDENTIFIER = dev.golova.KVOMagic; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + B9FD2AA525B70EBE006A31AE /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_TEAM = KMC5NA9YG7; + INFOPLIST_FILE = KVOMagicTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = dev.golova.KVOMagicTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + B9FD2AA625B70EBE006A31AE /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_TEAM = KMC5NA9YG7; + INFOPLIST_FILE = KVOMagicTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = dev.golova.KVOMagicTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + B9FD2A8725B70EBE006A31AE /* Build configuration list for PBXProject "KVOMagic" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B9FD2A9F25B70EBE006A31AE /* Debug */, + B9FD2AA025B70EBE006A31AE /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + B9FD2AA125B70EBE006A31AE /* Build configuration list for PBXNativeTarget "KVOMagic" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B9FD2AA225B70EBE006A31AE /* Debug */, + B9FD2AA325B70EBE006A31AE /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + B9FD2AA425B70EBE006A31AE /* Build configuration list for PBXNativeTarget "KVOMagicTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B9FD2AA525B70EBE006A31AE /* Debug */, + B9FD2AA625B70EBE006A31AE /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = B9FD2A8425B70EBE006A31AE /* Project object */; +} diff --git a/KVOMagic/KVOMagic/ArrayKVO.swift b/KVOMagic/KVOMagic/ArrayKVO.swift new file mode 100644 index 0000000..5f87bd5 --- /dev/null +++ b/KVOMagic/KVOMagic/ArrayKVO.swift @@ -0,0 +1,81 @@ +// +// ArrayKVO.swift +// KVOMagic +// +// Created by Viktor Radulov on 1/19/21. +// Copyright © 2021 Viktor Radulov. All rights reserved. + +import Foundation + +class ArrayWrapper: NSObject { + class Context: NSObject { + weak var observer: NSObject? + let keyPath: String + let context: UnsafeMutableRawPointer? + let options: NSKeyValueObservingOptions? + + init(_ observer: NSObject, _ keyPath: String, _ options: NSKeyValueObservingOptions?, _ context: UnsafeMutableRawPointer?) { + self.observer = observer + self.keyPath = keyPath + self.context = context + self.options = options + } + } + + var observingContexts = [Context]() + var array: NSArray { + didSet { + for observingContext in (observingContexts.filter { $0.observer != nil }) { + let observer = observingContext.observer! + let keyPath = observingContext.keyPath + let context = observingContext.context + let options = observingContext.options + + for object in oldValue { + (object as? NSObject)?.removeObserver(observer, forKeyPath: keyPath, context: context) + } + + for object in array { + (object as? NSObject)?.addObserver(observer, forKeyPath: keyPath, options: options ?? [], context: context) + } + } + } + } + + init?(array: Any?, owner: NSObject, keyPath: String) { + guard let array = array as? NSArray else { return nil } + self.array = array + + super.init() + + owner.startObserving(keyPath) { [weak self, weak owner] _, _ in + guard let newArray = owner?.value(forKeyPath: keyPath) as? NSArray else { return } + self?.array = newArray + } + } + + override func addObserver(_ observer: NSObject, forKeyPath keyPath: String, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?) { + observingContexts.append(Context(observer, keyPath, options, context)) + for object in (array.compactMap { $0 as? NSObject }) { + object.addObserver(observer, forKeyPath: keyPath, options: options, context: context) + } + } + + override func removeObserver(_ observer: NSObject, forKeyPath keyPath: String) { + observingContexts.removeAll { $0.observer === observer && $0.keyPath == keyPath } + for object in (array.compactMap { $0 as? NSObject }) { + object.removeObserver(observer, forKeyPath: keyPath) + } + } + + override func removeObserver(_ observer: NSObject, forKeyPath keyPath: String, context: UnsafeMutableRawPointer?) { + observingContexts.removeAll { $0.observer === observer && $0.keyPath == keyPath && $0.context == context } + for object in (array.compactMap { $0 as? NSObject }) { + object.removeObserver(observer, forKeyPath: keyPath, context: context) + } + } +} + +extension String { + static var arrayKVO: String { "$" } +} diff --git a/KVOMagic/KVOMagic/Info.plist b/KVOMagic/KVOMagic/Info.plist new file mode 100644 index 0000000..c0701c6 --- /dev/null +++ b/KVOMagic/KVOMagic/Info.plist @@ -0,0 +1,22 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + $(PRODUCT_BUNDLE_PACKAGE_TYPE) + CFBundleShortVersionString + $(MARKETING_VERSION) + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + + diff --git a/KVOMagic/KVOMagic/KVOMagic.h b/KVOMagic/KVOMagic/KVOMagic.h new file mode 100644 index 0000000..46f5511 --- /dev/null +++ b/KVOMagic/KVOMagic/KVOMagic.h @@ -0,0 +1,18 @@ +// +// KVOMagic.h +// KVOMagic +// +// Created by Viktor Radulov on 1/19/21. +// + +#import + +//! Project version number for KVOMagic. +FOUNDATION_EXPORT double KVOMagicVersionNumber; + +//! Project version string for KVOMagic. +FOUNDATION_EXPORT const unsigned char KVOMagicVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + diff --git a/KVOMagic/KVOMagic/ObservingUtils.swift b/KVOMagic/KVOMagic/ObservingUtils.swift new file mode 100644 index 0000000..82aa956 --- /dev/null +++ b/KVOMagic/KVOMagic/ObservingUtils.swift @@ -0,0 +1,190 @@ +// +// ObservingUtils.swift +// KVOMagic +// +// Created by Viktor Radulov on 1/19/21. +// Copyright © 2021 Viktor Radulov. All rights reserved. + +import Foundation +import ObjectiveC + +public extension NSObjectProtocol where Self: NSObject { + func startObserving(_ keyPath: KeyPath, _ owner: Any? = nil, options: NSKeyValueObservingOptions = [], changeHandler: @escaping (Self, KeyValueObservedChange) -> Void) { + let uuid: NSObject = UUID() as NSObject + + let contextPtr = owner != nil ? &|self : &|uuid + let observer = KeyValueObserver(object: self, keyPath, options: options) { object, change in + let converter = { (changeValue: Any?) -> Value? in + guard let optionalType = Value.self as? OptionalForGenerics.Type, + let unwrapped = changeValue else { return changeValue as? Value } + + let nullEliminatedValue = optionalType.cast(unwrapped) as Any + let transformedOptional = nullEliminatedValue + return transformedOptional as? Value + } + + let notification = KeyValueObservedChange(kind: change.kind, + newValue: converter(change.newValue), + oldValue: converter(change.oldValue), + indexes: change.indexes, + isPrior: change.isPrior) + changeHandler(object, notification) + } + + var observers = objc_getAssociatedObject(owner ?? self, contextPtr) as? [String: KeyValueObserverProtocol] + if observers == nil { + observers = [:] + } + + guard let keyPath = observer.path else { return } + + assert(owner == nil || observers?[keyPath] == nil) + observers?[keyPath] = observer + objc_setAssociatedObject(owner ?? self, contextPtr, observers, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + observer.start() + } + + func startObserving(_ keyPath: String, _ owner: Any? = nil, options: NSKeyValueObservingOptions = [], changeHandler: @escaping (Self, KeyValueObservedChange) -> Void) { + let uuid: NSObject = UUID() as NSObject + + let contextPtr = owner != nil ? &|self : &|uuid + let observer = KeyValueObserver(object: self, keyPath, options: options, changeHandler: changeHandler) + + var observers = objc_getAssociatedObject(owner ?? self, contextPtr) as? [String: KeyValueObserverProtocol] + if observers == nil { + observers = [:] + } + + guard let keyPath = observer.path else { return } + + assert(owner == nil || observers?[keyPath] == nil) + observers?[keyPath] = observer + objc_setAssociatedObject(owner ?? self, contextPtr, observers, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + observer.start() + } + + func startObserving(_ object: Object, + _ keyPath: KeyPath, + _ owner: Any? = nil, + options: NSKeyValueObservingOptions = [], + changeHandler: @escaping (Object, KeyValueObservedChange) -> Void) { + let nsObject: NSObject = object + nsObject.startObserving(keyPath._kvcKeyPathString!, owner, options: options) { object, change in + guard let object = object as? Object else { return } + + changeHandler(object, KeyValueObservedChange(kind: change.kind, + newValue: change.newValue as? Value, + oldValue: change.oldValue as? Value, + indexes: change.indexes, + isPrior: change.isPrior)) + } + } + + func stopObserving(_ object: Object, + _ keyPath: KeyPath, + _ owner: Any? = nil) { + object.stopObserving(keyPath, owner) + } + + func stopObserving(_ keyPath: KeyPath, _ owner: Any?) { + guard let keyPath = keyPath._kvcKeyPathString else { return } + stopObserving(keyPath, owner) + } + + func stopObserving(_ keyPath: String, _ owner: Any?) { + guard let owner = owner else { return } + var observers = objc_getAssociatedObject(owner, &|self) as? [String: KeyValueObserverProtocol] + + observers?[keyPath]?.invalidate() + observers?[keyPath] = nil + + objc_setAssociatedObject(owner, &|self, observers, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } +} + +public struct KeyValueObservedChange { + public typealias Kind = NSKeyValueChange + public let kind: KeyValueObservedChange.Kind + public let newValue: Value? + public let oldValue: Value? + public let indexes: IndexSet? + public let isPrior: Bool +} + +protocol KeyValueObserverProtocol { + func invalidate() +} + +public class KeyValueObserver: NSObject, KeyValueObserverProtocol { + var changeHandler: ((Object, KeyValueObservedChange) -> Void)? + let path: String? + let options: NSKeyValueObservingOptions + unowned(unsafe) var object: Object? + + init(object: Object, _ keyPath: KeyPath, options: NSKeyValueObservingOptions = [], changeHandler: @escaping (Object, KeyValueObservedChange) -> Void) { + self.changeHandler = changeHandler + self.object = object + self.path = keyPath._kvcKeyPathString + self.options = options + + super.init() + } + + init(object: Object, _ stringKeyPath: String, options: NSKeyValueObservingOptions = [], changeHandler: @escaping (Object, KeyValueObservedChange) -> Void) { + self.changeHandler = changeHandler + self.object = object + self.path = stringKeyPath + self.options = options + + super.init() + } + + // swiftlint:disable block_based_kvo + override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) { + + guard let object = object as? Object, + let change = change, + let rawKind = change[NSKeyValueChangeKey.kindKey] as? UInt, + let kind = NSKeyValueChange(rawValue: rawKind) else { return } + + let notification = KeyValueObservedChange(kind: kind, + newValue: change[NSKeyValueChangeKey.newKey], + oldValue: change[NSKeyValueChangeKey.oldKey], + indexes: change[NSKeyValueChangeKey.indexesKey] as? IndexSet, + isPrior: change[NSKeyValueChangeKey.notificationIsPriorKey] as? Bool ?? false) + changeHandler?(object, notification) + } + + func start() { + guard let stringPath = path else { return } + + object?.addObserver(self, forKeyPath: stringPath, options: options, context: nil) + } + + func invalidate() { + invalidate(async: true) + } + + private func invalidate(async: Bool) { + guard let stringPath = path else { return } + changeHandler = nil + + // Async here for weird edge case: + // `Stop observing called in .initial KVO-message callback`. + // It should be async because this callback performed in addObserver() call. + // Before observer is added + if async && options.contains(.initial) { + DispatchQueue.main.async { + self.object?.removeObserver(self, forKeyPath: stringPath) + self.object = nil + } + } else { + object?.removeObserver(self, forKeyPath: stringPath) + object = nil + } + } + + deinit { + invalidate(async: false) + } +} diff --git a/KVOMagic/KVOMagic/PropertyWrappers.swift b/KVOMagic/KVOMagic/PropertyWrappers.swift new file mode 100644 index 0000000..83c75a5 --- /dev/null +++ b/KVOMagic/KVOMagic/PropertyWrappers.swift @@ -0,0 +1,336 @@ +// +// PropertyWrappers.swift +// KVOMagic +// +// Created by Viktor Radulov on 1/19/21. +// Copyright © 2021 Viktor Radulov. All rights reserved. + +import Foundation + +protocol OwnedWrapper { + var anyOwner: Any? { get set } + var ownerKeypath: String? { get set } +} + +protocol ObjectOwnedWrapper { + var owner: NSObject? { get set } + var ownerKeypath: String? { get set } +} + +extension NSObject { + func initWrappers() { + let mirror = Mirror(reflecting: self) + for (key, value) in mirror.children { + guard let keyPath = key?.replacingOccurrences(of: "_", with: "") else { continue } + if var wrapper = value as? OwnedWrapper { + wrapper.ownerKeypath = keyPath + wrapper.anyOwner = self + } + if var wrapper = value as? ObjectOwnedWrapper { + wrapper.ownerKeypath = keyPath + wrapper.owner = self + } + } + } +} + +class WrapperOwner: NSObject { + override init() { + super.init() + + initWrappers() + } +} + +@propertyWrapper +class UIProperty: ObjectOwnedWrapper { + var ownerKeypath: String? + var owner: NSObject? + + init(wrappedValue: PropertyType) { + storredValue = wrappedValue + } + + var storredValue: PropertyType + var wrappedValue: PropertyType { + get { + storredValue + } + set { + assert(owner != nil, "Use me only in WrapperOwner subclasses!") + guard let owner = owner, let ownerKeypath = ownerKeypath else { return } + let block = { + owner.willChangeValue(forKey: ownerKeypath) + self.storredValue = newValue + owner.didChangeValue(forKey: ownerKeypath) + } + + if Thread.current == Thread.main { + block() + } else { + DispatchQueue.main.sync(execute: block) + } + } + } +} + +extension NSObject { + + static var arrayKVOContext: NSObject? + var wrappers: [String: ArrayWrapper] { + get { + (objc_getAssociatedObject(self, &NSObject.arrayKVOContext) as? [String: ArrayWrapper]) ?? [:] + } + set { + objc_setAssociatedObject(self, &NSObject.arrayKVOContext, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } + } + + func arrayCompatibleValue(forKey key: String, defaultValue: (String) -> Any?) -> Any? { + let prefix = String.arrayKVO + if key.hasPrefix(prefix) { + let keyPath = String(key.dropFirst(prefix.count)) + if let wrapper = wrappers[keyPath] { + return wrapper + } + let result = ArrayWrapper(array: defaultValue(keyPath), owner: self, keyPath: keyPath) + wrappers[keyPath] = result + + return result + } else { + return defaultValue(key) + } + } +} + +@propertyWrapper +class Computed: OwnedWrapper where Owner: NSObject { + let affectings: [String] + let block: (Owner) -> PropertyType + + private var owner: Owner! { + didSet { + updateStoredValue() + } + } + var ownerKeypath: String? + var anyOwner: Any? { + get { + return owner + } + set { + guard let owner = newValue as? Owner else { return } + + self.owner = owner + for affecting in affectings { + owner.startObserving(affecting, self, options: [.initial]) { [weak self] _, _ in + guard let self = self, + self.updateTimer == nil else { return } + + let timeFromUpdate = Date().timeIntervalSince(self.lastUpdate) + if timeFromUpdate < self.changeRate { + self.updateTimer = Timer.scheduledTimer(withTimeInterval: self.changeRate - timeFromUpdate, repeats: false, block: { [weak self] _ in + self?.updateTimer = nil + self?.sendNotifications() + }) + } else { + self.sendNotifications() + } + } + } + } + } + + private func sendNotifications() { + guard let ownerKeypath = ownerKeypath else { return } + + owner.willChangeValue(forKey: ownerKeypath) + updateStoredValue() + owner.didChangeValue(forKey: ownerKeypath) + + lastUpdate = Date() + } + + // typeProvider is used only for generics. + // Should be `self` in majority of cases. + init(_ block: @escaping (Owner) -> PropertyType, _ typeProvider: (Owner) -> () -> Owner, changeRate: TimeInterval = 0.1, _ affectings: String...) { + self.affectings = affectings + self.block = block + self.changeRate = changeRate + } + + private lazy var lastUpdate = Date(timeIntervalSinceNow: -(changeRate * 2)) + private var updateTimer: Timer? + private let changeRate: TimeInterval + + var storedValue: PropertyType? + var wrappedValue: PropertyType { + storedValue ?? block(owner) + } + + func updateStoredValue() { + storedValue = block(owner) + } +} + +// Revisit me after Swift has templates implemented +@propertyWrapper +class Computed1: OwnedWrapper where Owner: NSObject { + let affecting: KeyPath + let block: (Affected) -> PropertyType + + private var owner: Owner! { + didSet { + updateStoredValue() + } + } + var ownerKeypath: String? + var anyOwner: Any? { + get { + return owner + } + set { + guard let owner = newValue as? Owner else { return } + self.owner = owner + owner.startObserving(affecting, self, options: [.initial]) { [weak self] owner, _ in + guard let self = self, let ownerKeypath = self.ownerKeypath else { return } + + owner.willChangeValue(forKey: ownerKeypath) + self.updateStoredValue() + owner.didChangeValue(forKey: ownerKeypath) + } + } + } + + var storedValue: PropertyType? + var wrappedValue: PropertyType { + storedValue ?? block(owner[keyPath: affecting]) + } + + init(_ block: @escaping (Affected) -> PropertyType, _ typeProvider: (Owner) -> () -> Owner, _ affecting: KeyPath) { + self.affecting = affecting + self.block = block + } + + func updateStoredValue() { + storedValue = block(owner[keyPath: affecting]) + } +} + +@propertyWrapper +class Computed2: OwnedWrapper where Owner: NSObject { + let affectings: (KeyPath, KeyPath) + let block: (Affected1, Affected2) -> PropertyType + + private var owner: Owner! { + didSet { + updateStoredValue() + } + } + var ownerKeypath: String? + var anyOwner: Any? { + get { + return owner + } + set { + guard let owner = newValue as? Owner else { return } + self.owner = owner + let observing: (Owner) -> Void = { [weak self] owner in + guard let self = self, let ownerKeypath = self.ownerKeypath else { return } + + owner.willChangeValue(forKey: ownerKeypath) + self.updateStoredValue() + owner.didChangeValue(forKey: ownerKeypath) + } + + owner.startObserving(affectings.0, self, options: [.initial]) { owner, _ in observing(owner) } + owner.startObserving(affectings.1, self, options: [.initial]) { owner, _ in observing(owner) } + } + } + var storedValue: PropertyType? + var wrappedValue: PropertyType { + storedValue ?? block(owner[keyPath: affectings.0], owner[keyPath: affectings.1]) + } + + init(_ block: @escaping (Affected1, Affected2) -> PropertyType, _ typeProvider: (Owner) -> () -> Owner, _ affecting1: KeyPath, _ affecting2: KeyPath) { + self.affectings = (affecting1, affecting2) + self.block = block + } + + func updateStoredValue() { + storedValue = block(owner[keyPath: affectings.0], owner[keyPath: affectings.1]) + } +} + +@propertyWrapper +class Computed3: OwnedWrapper where M: NSObject { + let affectings: (KeyPath, KeyPath, KeyPath) + let block: (U1, U2, U3) -> T + + private var owner: M! { + didSet { + updateStoredValue() + } + } + var ownerKeypath: String? + var anyOwner: Any? { + get { + return owner + } + set { + guard let owner = newValue as? M else { return } + self.owner = owner + + let observing: (M) -> Void = { [weak self] owner in + guard let self = self, let ownerKeypath = self.ownerKeypath else { return } + + owner.willChangeValue(forKey: ownerKeypath) + self.updateStoredValue() + owner.didChangeValue(forKey: ownerKeypath) + } + + owner.startObserving(affectings.0, self, options: [.initial]) { owner, _ in observing(owner) } + owner.startObserving(affectings.1, self, options: [.initial]) { owner, _ in observing(owner) } + owner.startObserving(affectings.2, self, options: [.initial]) { owner, _ in observing(owner) } + } + } + + init(_ block: @escaping (U1, U2, U3) -> T, _ typeProvider: (M) -> () -> M, _ affecting1: KeyPath, _ affecting2: KeyPath, _ affecting3: KeyPath) { + + self.affectings = (affecting1, affecting2, affecting3) + self.block = block + } + + var storedValue: T? + var wrappedValue: T { + storedValue ?? block(owner[keyPath: affectings.0], owner[keyPath: affectings.1], owner[keyPath: affectings.2]) + } + + func updateStoredValue() { + storedValue = block(owner[keyPath: affectings.0], owner[keyPath: affectings.1], owner[keyPath: affectings.2]) + } +} + +class ArrayOwner: WrapperOwner { + override func value(forKey key: String) -> Any? { + arrayCompatibleValue(forKey: key, defaultValue: { super.value(forKey: $0) }) + } + + // Don't reorder this calls! + // ...except you know what you're doing + override func willChangeValue(forKey key: String) { + if wrappers[key] != nil { + super.willChangeValue(forKey: .arrayKVO + key) + } + + super.willChangeValue(forKey: key) + } + + override func didChangeValue(forKey key: String) { + super.didChangeValue(forKey: key) + + if wrappers[key] != nil { + super.didChangeValue(forKey: .arrayKVO + key) + } + } +} diff --git a/KVOMagic/KVOMagic/Utils/Operators.swift b/KVOMagic/KVOMagic/Utils/Operators.swift new file mode 100644 index 0000000..21baf2c --- /dev/null +++ b/KVOMagic/KVOMagic/Utils/Operators.swift @@ -0,0 +1,14 @@ +// +// Operators.swift +// KVOMagic +// +// Created by Viktor Radulov on 1/19/21. +// Copyright © 2021 Viktor Radulov. All rights reserved. + +import Foundation + +prefix operator &| + +public prefix func &| (_ obj: AnyObject) -> UnsafeRawPointer { + UnsafeRawPointer(Unmanaged.passUnretained(obj).toOpaque()) +} diff --git a/KVOMagic/KVOMagic/Utils/Optionals.swift b/KVOMagic/KVOMagic/Utils/Optionals.swift new file mode 100644 index 0000000..f59b1e1 --- /dev/null +++ b/KVOMagic/KVOMagic/Utils/Optionals.swift @@ -0,0 +1,18 @@ +// +// Optionals.swift +// KVOMagic +// +// Created by Viktor Radulov on 1/19/21. +// Copyright © 2021 Viktor Radulov. All rights reserved. + +import Foundation + +public protocol OptionalForGenerics { + static func cast(_ value: Any) -> Any? +} + +extension Optional: OptionalForGenerics { + public static func cast(_ value: Any) -> Any? { + value as? Wrapped + } +} diff --git a/KVOMagic/KVOMagicTests/Info.plist b/KVOMagic/KVOMagicTests/Info.plist new file mode 100644 index 0000000..64d65ca --- /dev/null +++ b/KVOMagic/KVOMagicTests/Info.plist @@ -0,0 +1,22 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + $(PRODUCT_BUNDLE_PACKAGE_TYPE) + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/KVOMagic/KVOMagicTests/KVOMagicTests.swift b/KVOMagic/KVOMagicTests/KVOMagicTests.swift new file mode 100644 index 0000000..be3b2a6 --- /dev/null +++ b/KVOMagic/KVOMagicTests/KVOMagicTests.swift @@ -0,0 +1,33 @@ +// +// KVOMagicTests.swift +// KVOMagicTests +// +// Created by Viktor Radulov on 1/19/21. +// Copyright © 2021 Viktor Radulov. All rights reserved. + +import XCTest +@testable import KVOMagic + +class KVOMagicTests: XCTestCase { + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testExample() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct results. + } + + func testPerformanceExample() throws { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..065d363 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright © 2021 Viktor Radulov. All rights reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License.