diff --git a/APNSUtil.podspec b/APNSUtil.podspec index 0d68d71..e534289 100644 --- a/APNSUtil.podspec +++ b/APNSUtil.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'APNSUtil' - s.version = '1.1.4' + s.version = '1.1.5' s.summary = 'APNSUtil is makes code simple using apple push notification service.' s.description = 'APNSUtil is makes code simple using apple push notification service.' s.homepage = 'https://github.com/pisces/APNSUtil' @@ -16,15 +16,6 @@ Pod::Spec.new do |s| s.author = { 'pisces' => 'hh963103@gmail.com' } s.source = { :git => 'https://github.com/pisces/APNSUtil.git', :tag => s.version.to_s } s.ios.deployment_target = '9.0' + s.source_files = 'APNSUtil/Classes/**/*' s.dependency 'ObjectMapper', '~> 3.1' - s.default_subspec = 'Core' - - s.subspec 'Core' do |core| - core.source_files = 'APNSUtil/Classes/**/*' - end - - s.subspec 'AppExtension' do |ext| - ext.source_files = 'APNSUtil/Classes/**/*' - ext.pod_target_xcconfig = { 'OTHER_SWIFT_FLAGS' => '-DAPP_EXTENSIONS' } - end end diff --git a/APNSUtil/Classes/NSObject+Codable.swift b/APNSUtil/Classes/NSObject+Codable.swift index 90b4a62..52b6408 100644 --- a/APNSUtil/Classes/NSObject+Codable.swift +++ b/APNSUtil/Classes/NSObject+Codable.swift @@ -9,23 +9,66 @@ import Foundation extension NSObject { + + // MARK: - Properties + public var mirrorChildList: [Mirror.Child] { return Mirror(reflecting: self).children.filter { $0.label != nil } } + // MARK: - Public methods + public func encodeProperties(with corder: NSCoder, ignoreKeys: [String]? = nil) { - mirrorChildList.forEach { + let mirror = Mirror(reflecting: self) + var current = mirror.superclassMirror + + while current != nil { + encodeChildren(filteredChildren(current), corder: corder, ignoreKeys: ignoreKeys) + current = current?.superclassMirror + } + + encodeChildren(filteredChildren(mirror), corder: corder, ignoreKeys: ignoreKeys) + } + public func decodeProperties(with corder: NSCoder, ignoreKeys: [String]? = nil) { + let mirror = Mirror(reflecting: self) + var current = mirror.superclassMirror + + while current != nil { + decodeChildren(filteredChildren(current), corder: corder, ignoreKeys: ignoreKeys) + current = current?.superclassMirror + } + + decodeChildren(filteredChildren(mirror), corder: corder, ignoreKeys: ignoreKeys) + } + public func dictionary(ignoreKeys: [String]? = nil) -> [String: Any] { + let mirror = Mirror(reflecting: self) + var current = mirror.superclassMirror + var dict = [String: Any]() + + while current != nil { + setValue(with: filteredChildren(current), dict: &dict, ignoreKeys: ignoreKeys) + current = current?.superclassMirror + } + + setValue(with: filteredChildren(mirror), dict: &dict, ignoreKeys: ignoreKeys) + + return dict + } + + // MARK: - Private methods + + private func encodeChildren(_ children: [Mirror.Child]?, corder: NSCoder, ignoreKeys: [String]? = nil) { + children?.forEach { if let ignoreKeys = ignoreKeys, ignoreKeys.contains($0.label!) { return } - if !($0.value is NSNull) { corder.encode($0.value, forKey: $0.label!) } } } - public func decodeProperties(with corder: NSCoder, ignoreKeys: [String]? = nil) { - mirrorChildList.forEach { + private func decodeChildren(_ children: [Mirror.Child]?, corder: NSCoder, ignoreKeys: [String]? = nil) { + children?.forEach { if let ignoreKeys = ignoreKeys, ignoreKeys.contains($0.label!) { return } @@ -34,14 +77,15 @@ extension NSObject { } } } - public func dictionary(ignoreKeys: [String]? = nil) -> [String: Any] { - var dict = [String: Any]() - mirrorChildList.forEach { + private func filteredChildren(_ mirror: Mirror?) -> [Mirror.Child] { + return mirror?.children.filter { $0.label != nil } ?? [] + } + private func setValue(with children: [Mirror.Child]?, dict: inout [String: Any], ignoreKeys: [String]? = nil) { + children?.forEach { if let ignoreKeys = ignoreKeys, ignoreKeys.contains($0.label!) { return } dict[$0.label!] = value(forKey: $0.label!) } - return dict } } diff --git a/APNSUtilAppExtension.podspec b/APNSUtilAppExtension.podspec new file mode 100644 index 0000000..d756ed6 --- /dev/null +++ b/APNSUtilAppExtension.podspec @@ -0,0 +1,22 @@ +# +# Be sure to run `pod lib lint APNSUtil.podspec' to ensure this is a +# valid spec before submitting. +# +# Any lines starting with a # are optional, but their use is encouraged +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html +# + +Pod::Spec.new do |s| + s.name = 'APNSUtilAppExtension' + s.version = '1.1.5' + s.summary = 'APNSUtil is makes code simple using apple push notification service.' + s.description = 'APNSUtil is makes code simple using apple push notification service.' + s.homepage = 'https://github.com/pisces/APNSUtil' + s.license = { :type => 'MIT', :file => 'LICENSE' } + s.author = { 'pisces' => 'hh963103@gmail.com' } + s.source = { :git => 'https://github.com/pisces/APNSUtil.git', :tag => s.version.to_s } + s.ios.deployment_target = '9.0' + s.source_files = 'APNSUtil/Classes/**/*' + s.dependency 'ObjectMapper', '~> 3.1' + s.pod_target_xcconfig = { 'OTHER_SWIFT_FLAGS' => '-DAPP_EXTENSIONS' } +end diff --git a/README.md b/README.md index 52f0ce8..ee91212 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ struct APNSPayload: Mappable { $ gem install cocoapods ``` -> CocoaPods 1.1.2+ is required to build APNSUtil 1.0.0+. +> CocoaPods 1.0.0+ is required to build APNSUtil 1.1.5+. To integrate APNSUtil into your Xcode project using CocoaPods, specify it in your `Podfile`: @@ -141,12 +141,12 @@ platform :ios, '9.0' # Default target '' do - pod 'APNSUtil', '~> 1.1.4' + pod 'APNSUtil', '~> 1.1.5' end # for AppExtension target '' do - pod 'APNSUtil/AppExtension', '~> 1.1.4' + pod 'APNSUtil/AppExtension', '~> 1.1.5' end ``` @@ -170,7 +170,7 @@ $ brew install carthage To integrate Alamofire into your Xcode project using Carthage, specify it in your `Cartfile`: ```ogdl -github "pisces/APNSUtil" ~> 1.1.4 +github "pisces/APNSUtil" ~> 1.1.5 ``` Run `carthage update` to build the framework and drag the built `APNSUtil.framework` into your Xcode project.