From a718f061aa73c4878a508fd9ba957d30c3db99ef Mon Sep 17 00:00:00 2001 From: Imthath M Date: Thu, 28 Mar 2019 15:28:43 +0530 Subject: [PATCH] add a project. use static encoder and decoder. --- CodableExtensions.swift | 49 --- .../project.pbxproj | 339 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + UtilitiesExample/UtilitiesExample/main.swift | 11 + .../Utils/CodableExtensions.swift | 59 +++ .../Utils/DateExtension.swift | 47 +-- .../Utils/EquatableExtension.swift | 9 +- .../Utils/FileIO.swift | 20 +- .../Utils/OptionalExtension.swift | 8 +- UtilitiesExample/Utils/StringExtension.swift | 22 ++ 11 files changed, 489 insertions(+), 90 deletions(-) delete mode 100644 CodableExtensions.swift create mode 100644 UtilitiesExample/UtilitiesExample.xcodeproj/project.pbxproj create mode 100644 UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 UtilitiesExample/UtilitiesExample/main.swift create mode 100644 UtilitiesExample/Utils/CodableExtensions.swift rename DateExtension.swift => UtilitiesExample/Utils/DateExtension.swift (52%) rename EquatableExtension.swift => UtilitiesExample/Utils/EquatableExtension.swift (61%) rename FileIO.swift => UtilitiesExample/Utils/FileIO.swift (80%) rename OptionalExtension.swift => UtilitiesExample/Utils/OptionalExtension.swift (75%) create mode 100644 UtilitiesExample/Utils/StringExtension.swift diff --git a/CodableExtensions.swift b/CodableExtensions.swift deleted file mode 100644 index 8c2beee..0000000 --- a/CodableExtensions.swift +++ /dev/null @@ -1,49 +0,0 @@ -// -// CodableExtensions.swift -// -// Created by Imthath M on 21/02/19. -// No Copyright. -// - -extension String { - public func objects() -> [T]? { - guard let data = self.data(using: .utf8) else { return nil } - guard let objects = try? JSONDecoder().decode([T].self, from: data) else { return nil } - return objects - } -} - -extension Encodable { - public var jsonData: Data? { - return try? JSONEncoder().encode(self) - } -} - -extension Decodable { - public init?(from jsonString: String) { - let data = Data(jsonString.utf8) - guard let object = try? JSONDecoder().decode(Self.self, from: data) else { return nil } - self = object - } -} - -protocol Imitable: Codable { - var copy: Self? { get } -} - -extension Imitable { - var copy: Self? { - guard let data = try? JSONEncoder().encode(self) else { return nil } - return try? JSONDecoder().decode(Self.self, from: data) - } -} - -protocol Distinguishable: Codable { - func isDistinct(from other: Self) -> Bool -} - -extension Distinguishable { - func isDistinct(from other: Self) -> Bool { - return self.jsonData != other.jsonData - } -} diff --git a/UtilitiesExample/UtilitiesExample.xcodeproj/project.pbxproj b/UtilitiesExample/UtilitiesExample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..7a4bfee --- /dev/null +++ b/UtilitiesExample/UtilitiesExample.xcodeproj/project.pbxproj @@ -0,0 +1,339 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + A8B6EA18224CCCF800A765A3 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA17224CCCF800A765A3 /* main.swift */; }; + A8B6EA23224CCD3D00A765A3 /* FileIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA1E224CCD3D00A765A3 /* FileIO.swift */; }; + A8B6EA24224CCD3D00A765A3 /* OptionalExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA1F224CCD3D00A765A3 /* OptionalExtension.swift */; }; + A8B6EA25224CCD3D00A765A3 /* EquatableExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA20224CCD3D00A765A3 /* EquatableExtension.swift */; }; + A8B6EA26224CCD3D00A765A3 /* CodableExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA21224CCD3D00A765A3 /* CodableExtensions.swift */; }; + A8B6EA27224CCD3D00A765A3 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA22224CCD3D00A765A3 /* DateExtension.swift */; }; + A8B6EA2A224CD03E00A765A3 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B6EA29224CD03E00A765A3 /* StringExtension.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + A8B6EA12224CCCF800A765A3 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + A8B6EA14224CCCF800A765A3 /* UtilitiesExample */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = UtilitiesExample; sourceTree = BUILT_PRODUCTS_DIR; }; + A8B6EA17224CCCF800A765A3 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + A8B6EA1E224CCD3D00A765A3 /* FileIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileIO.swift; sourceTree = ""; }; + A8B6EA1F224CCD3D00A765A3 /* OptionalExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptionalExtension.swift; sourceTree = ""; }; + A8B6EA20224CCD3D00A765A3 /* EquatableExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EquatableExtension.swift; sourceTree = ""; }; + A8B6EA21224CCD3D00A765A3 /* CodableExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodableExtensions.swift; sourceTree = ""; }; + A8B6EA22224CCD3D00A765A3 /* DateExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = ""; }; + A8B6EA29224CD03E00A765A3 /* StringExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + A8B6EA11224CCCF800A765A3 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + A8B6EA0B224CCCF800A765A3 = { + isa = PBXGroup; + children = ( + A8B6EA28224CCD5600A765A3 /* Utils */, + A8B6EA16224CCCF800A765A3 /* UtilitiesExample */, + A8B6EA15224CCCF800A765A3 /* Products */, + ); + sourceTree = ""; + }; + A8B6EA15224CCCF800A765A3 /* Products */ = { + isa = PBXGroup; + children = ( + A8B6EA14224CCCF800A765A3 /* UtilitiesExample */, + ); + name = Products; + sourceTree = ""; + }; + A8B6EA16224CCCF800A765A3 /* UtilitiesExample */ = { + isa = PBXGroup; + children = ( + A8B6EA17224CCCF800A765A3 /* main.swift */, + ); + path = UtilitiesExample; + sourceTree = ""; + }; + A8B6EA28224CCD5600A765A3 /* Utils */ = { + isa = PBXGroup; + children = ( + A8B6EA21224CCD3D00A765A3 /* CodableExtensions.swift */, + A8B6EA22224CCD3D00A765A3 /* DateExtension.swift */, + A8B6EA20224CCD3D00A765A3 /* EquatableExtension.swift */, + A8B6EA1E224CCD3D00A765A3 /* FileIO.swift */, + A8B6EA1F224CCD3D00A765A3 /* OptionalExtension.swift */, + A8B6EA29224CD03E00A765A3 /* StringExtension.swift */, + ); + path = Utils; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + A8B6EA13224CCCF800A765A3 /* UtilitiesExample */ = { + isa = PBXNativeTarget; + buildConfigurationList = A8B6EA1B224CCCF800A765A3 /* Build configuration list for PBXNativeTarget "UtilitiesExample" */; + buildPhases = ( + A8B6EA10224CCCF800A765A3 /* Sources */, + A8B6EA11224CCCF800A765A3 /* Frameworks */, + A8B6EA12224CCCF800A765A3 /* CopyFiles */, + A8B6EA2B224CD06E00A765A3 /* ShellScript */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = UtilitiesExample; + productName = UtilitiesExample; + productReference = A8B6EA14224CCCF800A765A3 /* UtilitiesExample */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + A8B6EA0C224CCCF800A765A3 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1010; + LastUpgradeCheck = 1010; + ORGANIZATIONNAME = "Sky Dev"; + TargetAttributes = { + A8B6EA13224CCCF800A765A3 = { + CreatedOnToolsVersion = 10.1; + }; + }; + }; + buildConfigurationList = A8B6EA0F224CCCF800A765A3 /* Build configuration list for PBXProject "UtilitiesExample" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = A8B6EA0B224CCCF800A765A3; + productRefGroup = A8B6EA15224CCCF800A765A3 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + A8B6EA13224CCCF800A765A3 /* UtilitiesExample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXShellScriptBuildPhase section */ + A8B6EA2B224CD06E00A765A3 /* ShellScript */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "# Type a script or drag a script file from your workspace to insert its path.\nif which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n"; + }; +/* End PBXShellScriptBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + A8B6EA10224CCCF800A765A3 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A8B6EA25224CCD3D00A765A3 /* EquatableExtension.swift in Sources */, + A8B6EA2A224CD03E00A765A3 /* StringExtension.swift in Sources */, + A8B6EA27224CCD3D00A765A3 /* DateExtension.swift in Sources */, + A8B6EA24224CCD3D00A765A3 /* OptionalExtension.swift in Sources */, + A8B6EA26224CCD3D00A765A3 /* CodableExtensions.swift in Sources */, + A8B6EA18224CCCF800A765A3 /* main.swift in Sources */, + A8B6EA23224CCD3D00A765A3 /* FileIO.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + A8B6EA19224CCCF800A765A3 /* 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_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; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + 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.14; + 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"; + }; + name = Debug; + }; + A8B6EA1A224CCCF800A765A3 /* 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_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; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + 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.14; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + A8B6EA1C224CCCF800A765A3 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.2; + }; + name = Debug; + }; + A8B6EA1D224CCCF800A765A3 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.2; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + A8B6EA0F224CCCF800A765A3 /* Build configuration list for PBXProject "UtilitiesExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A8B6EA19224CCCF800A765A3 /* Debug */, + A8B6EA1A224CCCF800A765A3 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + A8B6EA1B224CCCF800A765A3 /* Build configuration list for PBXNativeTarget "UtilitiesExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A8B6EA1C224CCCF800A765A3 /* Debug */, + A8B6EA1D224CCCF800A765A3 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = A8B6EA0C224CCCF800A765A3 /* Project object */; +} diff --git a/UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..e20a6a7 --- /dev/null +++ b/UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/UtilitiesExample/UtilitiesExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/UtilitiesExample/UtilitiesExample/main.swift b/UtilitiesExample/UtilitiesExample/main.swift new file mode 100644 index 0000000..4df1d75 --- /dev/null +++ b/UtilitiesExample/UtilitiesExample/main.swift @@ -0,0 +1,11 @@ +// +// main.swift +// UtilitiesExample +// +// Created by Imthath M on 28/03/19. +// Copyright © 2019 Sky Dev. All rights reserved. +// + +import Foundation + +print("Hello, World!") diff --git a/UtilitiesExample/Utils/CodableExtensions.swift b/UtilitiesExample/Utils/CodableExtensions.swift new file mode 100644 index 0000000..a8a659d --- /dev/null +++ b/UtilitiesExample/Utils/CodableExtensions.swift @@ -0,0 +1,59 @@ +// +// CodableExtensions.swift +// +// Created by Imthath M on 21/02/19. +// Copyright © 2019 Sky Dev. All rights reserved. +// + +import Foundation + +internal let encoder = JSONEncoder() +internal let decoder = JSONDecoder() + +extension Encodable { + public var jsonString: String? { + do { + return String(data: try encoder.encode(self), encoding: .utf8) + } catch { + return nil + } + } + + public var jsonData: Data? { + return try? encoder.encode(self) + } +} + +extension Decodable { + public init?(from jsonString: String) { + let data = Data(jsonString.utf8) + guard let object = try? decoder.decode(Self.self, from: data) else { return nil } + self = object + } + + public init?(from jsonData: Data) { + guard let object = try? decoder.decode(Self.self, from: jsonData) else { return nil } + self = object + } +} + +protocol Imitable: Codable { + var copy: Self? { get } +} + +extension Imitable { + var copy: Self? { + guard let data = try? encoder.encode(self) else { return nil } + return try? decoder.decode(Self.self, from: data) + } +} + +protocol Distinguishable: Codable { + func isDistinct(from other: Self) -> Bool +} + +extension Distinguishable { + func isDistinct(from other: Self) -> Bool { + return self.jsonData != other.jsonData + } +} diff --git a/DateExtension.swift b/UtilitiesExample/Utils/DateExtension.swift similarity index 52% rename from DateExtension.swift rename to UtilitiesExample/Utils/DateExtension.swift index 960141c..7672d3d 100644 --- a/DateExtension.swift +++ b/UtilitiesExample/Utils/DateExtension.swift @@ -2,55 +2,58 @@ // DateExtension.swift // // Created by Imthath M on 21/02/19. -// No Copyright. +// Copyright © 2019 Sky Dev. All rights reserved. // +import Foundation + +private let currentCal = Calendar.current + extension Date { - + public static func get(from string: String?, with formatter: DateFormatter, in timezone: TimeZone? = nil) -> Date? { - + guard let dateString = string else { return nil } - + if let timeZone = timezone { - formatter.timezone = timeZone + formatter.timeZone = timeZone } - + return formatter.date(from: dateString) } - + public func string(with formatter: DateFormatter, in timezone: TimeZone? = nil) -> String { - + if let timeZone = timezone { - formatter.timezone = timeZone + formatter.timeZone = timeZone } - + return formatter.string(from: self) } } - extension Date { - + public var startOfWeek: Date { - return Calendar.current.date(from: Calendar.current.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))! + return currentCal.date(from: currentCal.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))! } - + public var endOfWeek: Date { - return Calendar.current.date(byAdding: .day, value: 6, to: self.startOfWeek)! + return currentCal.date(byAdding: .day, value: 6, to: self.startOfWeek)! } - + public var dayOfWeek: Int { - return Calendar.current.component(.weekday, from: self)-1 + return currentCal.component(.weekday, from: self)-1 } - + public var startOfMonth: Date { - return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: self))! + return currentCal.date(from: currentCal.dateComponents([.year, .month], from: self))! } - + public var endOfMonth: Date { - let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: self.startOfMonth)! - return Calendar.current.date(byAdding: .day, value: -1, to: nextMonth)! + let nextMonth = currentCal.date(byAdding: .month, value: 1, to: self.startOfMonth)! + return currentCal.date(byAdding: .day, value: -1, to: nextMonth)! } } diff --git a/EquatableExtension.swift b/UtilitiesExample/Utils/EquatableExtension.swift similarity index 61% rename from EquatableExtension.swift rename to UtilitiesExample/Utils/EquatableExtension.swift index 26a3d58..ec8a68e 100644 --- a/EquatableExtension.swift +++ b/UtilitiesExample/Utils/EquatableExtension.swift @@ -2,18 +2,15 @@ // EquatableExtension.swift // // Created by Imthath M on 28/11/18. -// No Copyright. +// Copyright © 2019 Sky Dev. All rights reserved. // import Foundation extension Equatable { - public func findFirstMatch(in array: [Self]) -> Self? { - for element in array { - if element == self { - return element - } + for element in array where element == self { + return element } return nil } diff --git a/FileIO.swift b/UtilitiesExample/Utils/FileIO.swift similarity index 80% rename from FileIO.swift rename to UtilitiesExample/Utils/FileIO.swift index a2f9ef4..b0d6413 100644 --- a/FileIO.swift +++ b/UtilitiesExample/Utils/FileIO.swift @@ -2,25 +2,26 @@ // FileIO.swift // // Created by Imthath M on 28/11/18. -// No Copyright. +// Copyright © 2019 Sky Dev. All rights reserved. // +import Foundation + public class FileIO { public static func getOjbectFromFile(named name: String) -> T? { var object: T? do { if let data = readData(from: name) { - object = try JSONDecoder().decode(T.self, from: data) + object = try decoder.decode(T.self, from: data) } } catch let error as NSError { print("unable to decode object from text file: \(error.description)") } return object } - + public static func save(_ object: T, to name: String) where T: Codable { do { - let encoder = JSONEncoder() encoder.outputFormatting = .prettyPrinted let text = String(data: try encoder.encode(object), encoding: .utf8)! try text.write(to: getUrl(of: name), atomically: true, encoding: .utf8) @@ -29,7 +30,7 @@ public class FileIO { print("unable to save: \(error.description)") } } - + public static func readData(from name: String) -> Data? { var result: Data? do { @@ -39,7 +40,7 @@ public class FileIO { } return result } - + public static func readText(from name: String) -> String? { var result: String? do { @@ -49,12 +50,13 @@ public class FileIO { } return result } - + public static func getUrl(of name: String) -> URL { var fileUrl: URL? do { - let documentDirectoryUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) - fileUrl = documentDirectoryUrl.appendingPathComponent(name).appendingPathExtension("txt") + let docDirectoryUrl = try FileManager.default.url(for: .documentDirectory, + in: .userDomainMask, appropriateFor: nil, create: true) + fileUrl = docDirectoryUrl.appendingPathComponent(name).appendingPathExtension("txt") } catch let error as NSError { print("unable to get file url: \(error.description)") } diff --git a/OptionalExtension.swift b/UtilitiesExample/Utils/OptionalExtension.swift similarity index 75% rename from OptionalExtension.swift rename to UtilitiesExample/Utils/OptionalExtension.swift index e171b38..04f0aed 100644 --- a/OptionalExtension.swift +++ b/UtilitiesExample/Utils/OptionalExtension.swift @@ -2,19 +2,19 @@ // OptionalExtension.swift // // Created by Imthath M on 17/12/18. -// No Copyright. +// Copyright © 2019 Sky Dev. All rights reserved. // import Foundation extension Optional { - - public func safeUnwrap(to object: inout Wrapped?){ + + public func safeUnwrap(to object: inout Wrapped?) { if let strongSelf = self { object = strongSelf } } - + public func safeUnwrap(to object: inout Wrapped) { if let strongSelf = self { object = strongSelf diff --git a/UtilitiesExample/Utils/StringExtension.swift b/UtilitiesExample/Utils/StringExtension.swift new file mode 100644 index 0000000..d11af53 --- /dev/null +++ b/UtilitiesExample/Utils/StringExtension.swift @@ -0,0 +1,22 @@ +// +// StringExtension.swift +// UtilitiesExample +// +// Created by Imthath M on 28/03/19. +// Copyright © 2019 Sky Dev. All rights reserved. +// + +import Foundation + +extension String { + public func objects() -> [T]? { + guard let data = self.data(using: .utf8) else { return nil } + guard let objects = try? decoder.decode([T].self, from: data) else { return nil } + return objects + } + + func getTimeIntervalSince1970(with dateFormatter: DateFormatter) -> TimeInterval? { + guard let date = Date.get(from: self, with: dateFormatter) else { return nil } + return date.timeIntervalSince1970 + } +}