From 0a6a8ddb77a0ba23f8158f01204fc6bf9b9eed02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Wed, 8 Jun 2016 07:52:08 +0200 Subject: [PATCH 1/7] [Podspec] Add AppKit and UIKit to required frameworks --- HandySwift.podspec | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HandySwift.podspec b/HandySwift.podspec index ba62a25..e2ae63a 100644 --- a/HandySwift.podspec +++ b/HandySwift.podspec @@ -23,5 +23,8 @@ Pod::Spec.new do |s| s.source = { :git => "https://github.com/Flinesoft/HandySwift.git", :tag => "1.2.0" } s.source_files = "Sources", "Sources/**/*.swift" s.framework = "Foundation" + s.osx.framework = "AppKit" + s.ios.framework = "UIKit" + s.tvos.framework = "UIKit" end From 76c2d164a9687cf7ff416c7ce78d9fd80f381474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Thu, 30 Jun 2016 17:25:42 +0200 Subject: [PATCH 2/7] Fix SwiftLint whitespace violations --- .swiftlint.yml | 6 -- Sources/Extensions/ArrayExtension.swift | 18 ++--- .../Extensions/CharacterViewExtension.swift | 13 ++-- Sources/Extensions/ColorExtension.swift | 76 +++++++++---------- .../Extensions/CoreGraphicsExtensions.swift | 72 +++++++++--------- Sources/Extensions/DictionaryExtension.swift | 26 +++---- Sources/Extensions/IntExtension.swift | 8 +- Sources/Extensions/IntegerTypeExtension.swift | 6 +- Sources/Extensions/StringExtension.swift | 18 ++--- Sources/Globals.swift | 12 +-- Sources/Structs/FrequencyTable.swift | 34 ++++----- Sources/Structs/SortedArray.swift | 64 ++++++++-------- 12 files changed, 172 insertions(+), 181 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 691066d..a9ca77c 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,9 +1,3 @@ -# rule identifiers to exclude from running -disabled_rules: -- trailing_newline -- trailing_whitespace -- cyclomatic_complexity - # some rules are only opt-in opt_in_rules: - empty_count diff --git a/Sources/Extensions/ArrayExtension.swift b/Sources/Extensions/ArrayExtension.swift index 32aed36..e9ebb6f 100644 --- a/Sources/Extensions/ArrayExtension.swift +++ b/Sources/Extensions/ArrayExtension.swift @@ -9,37 +9,37 @@ import Foundation public extension Array { - + /// Returns a random element from the `Array`. - /// + /// /// - Returns: A random element from the array or `nil` if empty. public var sample: Element? { if !self.isEmpty { let randomIndex = self.startIndex.advancedBy(Int(randomBelow: self.count)) return self[randomIndex] } - + return nil } - + /// Returns a given number of random elements from the `Array`. /// /// - Parameters: /// - size: The number of random elements wanted. /// - Returns: An array with the given number of random elements or `nil` if empty. public func sample(size size: Int) -> [Element]? { - + if !self.isEmpty { var sampleElements: [Element] = [] - + size.times { sampleElements.append(self.sample!) } - + return sampleElements } - + return nil } - + } diff --git a/Sources/Extensions/CharacterViewExtension.swift b/Sources/Extensions/CharacterViewExtension.swift index 9fc4877..b112318 100644 --- a/Sources/Extensions/CharacterViewExtension.swift +++ b/Sources/Extensions/CharacterViewExtension.swift @@ -18,29 +18,28 @@ public extension String.CharacterView { let randomIndex = self.startIndex.advancedBy(Int(randomBelow: self.count)) return self[randomIndex] } - + return nil } - + /// Returns a given number of random characters from the `CharacterView`. /// /// - Parameters: /// - size: The number of random characters wanted. /// - Returns: A `CharacterView` with the given number of random characters or `nil` if empty. public func sample(size size: Int) -> String.CharacterView? { - + if !self.isEmpty { var sampleElements: String.CharacterView = String.CharacterView() - + size.times { sampleElements.append(self.sample!) } - + return sampleElements } - + return String.CharacterView() } - } diff --git a/Sources/Extensions/ColorExtension.swift b/Sources/Extensions/ColorExtension.swift index e3df814..a9cb91c 100644 --- a/Sources/Extensions/ColorExtension.swift +++ b/Sources/Extensions/ColorExtension.swift @@ -7,11 +7,11 @@ // #if UIKIT - + import UIKit - + extension UIColor { - + /// A list of changeable attributes of the UIColor. /// /// - Red: The red color part of RGB & alpha. @@ -25,28 +25,28 @@ public enum ChangeableAttribute { case Red, Green, Blue, Hue, Saturation, Brightness, Alpha } - + // MARK: - Computed Properties - + /// The HSB & alpha attributes of the `UIColor` instance. public var hsba: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) { - + var hsba: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) = (0, 0, 0, 0) self.getHue(&(hsba.hue), saturation: &(hsba.saturation), brightness: &(hsba.brightness), alpha: &(hsba.alpha)) return hsba } - + /// The RGB & alpha attributes of the `UIColor` instance. public var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { - + var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) = (0, 0, 0, 0) self.getRed(&rgba.red, green: &rgba.green, blue: &rgba.blue, alpha: &rgba.alpha) return rgba } - - + + // MARK: - Methods - + /// Creates a new `UIColor` object with a single attribute changed by a given difference using addition. /// /// - Parameters: @@ -54,83 +54,83 @@ /// - by: The addition to be added to the current value of the attribute. /// - Returns: The resulting new `UIColor` with the specified change applied. public func change(attribute: ChangeableAttribute, by addition: CGFloat) -> UIColor { - + switch attribute { case .Red: return self.change(attribute, to: self.rgba.red + addition) - + case .Green: return self.change(attribute, to: self.rgba.green + addition) - + case .Blue: return self.change(attribute, to: self.rgba.blue + addition) - + case .Alpha: return self.change(attribute, to: self.rgba.alpha + addition) - + case .Hue: return self.change(attribute, to: self.hsba.hue + addition) - + case .Saturation: return self.change(attribute, to: self.hsba.saturation + addition) - + case .Brightness: return self.change(attribute, to: self.hsba.brightness + addition) } - + } - + /// Creates a new `UIColor` object with the value of a single attribute set to a given value. /// /// - Parameters: /// - attribute: The attribute to change. /// - to: The new value to be set for the attribute. /// - Returns: The resulting new `UIColor` with the specified change applied. - public func change(attribute: ChangeableAttribute, to newValue: CGFloat) -> UIColor { - + public func change(attribute: ChangeableAttribute, to newValue: CGFloat) -> UIColor { // swiftlint:disable:this cyclomatic_complexity + switch attribute { case .Red, .Green, .Blue, .Alpha: var newRgba = self.rgba - + switch attribute { case .Red: newRgba.red = newValue - + case .Green: newRgba.green = newValue - + case .Blue: newRgba.blue = newValue - + case .Alpha: newRgba.alpha = newValue - + default: break } - + return UIColor(red: newRgba.red, green: newRgba.green, blue: newRgba.blue, alpha: newRgba.alpha) - + case .Hue, .Saturation, .Brightness: var newHsba = self.hsba - + switch attribute { case .Hue: newHsba.hue = newValue - + case .Saturation: newHsba.saturation = newValue - + case .Brightness: newHsba.brightness = newValue - + default: break } - + return UIColor(hue: newHsba.hue, saturation: newHsba.saturation, brightness: newHsba.brightness, alpha: newHsba.alpha) } } - + } - - -#endif \ No newline at end of file + + +#endif diff --git a/Sources/Extensions/CoreGraphicsExtensions.swift b/Sources/Extensions/CoreGraphicsExtensions.swift index c39c871..d55bfe9 100644 --- a/Sources/Extensions/CoreGraphicsExtensions.swift +++ b/Sources/Extensions/CoreGraphicsExtensions.swift @@ -10,16 +10,16 @@ import Foundation #if UIKIT import UIKit - + // MARK: - iOS/tvOS CGSize Extension - + extension CGSize { - + /// Returns a new CGSize object with the width and height converted to true pixels on the main screen. public var inPixels: CGSize { return inPixels(UIScreen.mainScreen()) } - + /// Returns a new CGSize object with the width and height converted to true pixels on the given screen. /// /// - Parameters: @@ -27,20 +27,20 @@ import Foundation public func inPixels(screen: UIScreen) -> CGSize { return CGSize(width: width / screen.scale, height: height / screen.scale) } - + } - + // MARK: - iOS/tvOS CGPoint Extension - + extension CGPoint { - + /// Returns a new CGPoint object with the x and y converted to true pixels on the main screen. public var inPixels: CGPoint { return inPixels(UIScreen.mainScreen()) } - - + + /// Returns a new CGPoint object with the x and y converted to true pixels on the given screen. /// /// - Parameters: @@ -48,19 +48,19 @@ import Foundation public func inPixels(screen: UIScreen) -> CGPoint { return CGPoint(x: x / screen.scale, y: y / screen.scale) } - + } - - + + // MARK: - iOS/tvOS CGRect Extension - + extension CGRect { - + /// Returns a new CGRect object with the origin and size converted to true pixels on the main screen. public var inPixels: CGRect { return inPixels(UIScreen.mainScreen()) } - + /// Returns a new CGRect object with the origin and size converted to true pixels on the given screen. /// /// - Parameters: @@ -68,16 +68,16 @@ import Foundation public func inPixels(screen: UIScreen) -> CGRect { return CGRect(origin: origin.inPixels(screen), size: size.inPixels(screen)) } - + } #else import AppKit - + // MARK: - OSX CGSize Extension - + extension CGSize { - + /// Returns a new CGSize object with the width and height converted to true pixels on the main screen. public var inPixels: CGSize { guard let mainScreen = NSScreen.mainScreen() else { @@ -85,7 +85,7 @@ import Foundation } return inPixels(mainScreen) } - + /// Returns a new CGSize object with the width and height converted to true pixels on the given screen. /// /// - Parameters: @@ -93,13 +93,13 @@ import Foundation public func inPixels(screen: NSScreen) -> CGSize { return CGSize(width: width / screen.backingScaleFactor, height: height / screen.backingScaleFactor) } - + } - + // MARK: - OSX CGPoint Extension - + extension CGPoint { - + /// Returns a new CGPoint object with the x and y converted to true pixels on the main screen. public var inPixels: CGPoint { guard let mainScreen = NSScreen.mainScreen() else { @@ -107,7 +107,7 @@ import Foundation } return inPixels(mainScreen) } - + /// Returns a new CGPoint object with the x and y converted to true pixels on the given screen. /// /// - Parameters: @@ -115,14 +115,14 @@ import Foundation public func inPixels(screen: NSScreen) -> CGPoint { return CGPoint(x: x / screen.backingScaleFactor, y: y / screen.backingScaleFactor) } - + } - - + + // MARK: - OSX CGRect Extension - + extension CGRect { - + /// Returns a new CGRect object with the origin and size converted to true pixels on the main screen or /// returns `nil` if no main screen found. public var inPixels: CGRect? { @@ -131,7 +131,7 @@ import Foundation } return inPixels(mainScreen) } - + /// Returns a new CGRect object with the origin and size converted to true pixels on the given screen. /// /// - Parameters: @@ -139,7 +139,7 @@ import Foundation public func inPixels(screen: NSScreen) -> CGRect { return CGRect(origin: origin.inPixels(screen), size: size.inPixels(screen)) } - + } #endif @@ -148,7 +148,7 @@ import Foundation // MARK: - Shared CGRect Extension extension CGRect { - + /// Creates a new CGRect object from origin zero with given size. /// /// - Parameters: @@ -156,7 +156,7 @@ extension CGRect { public init(size: CGSize) { self.init(origin: CGPoint.zero, size: size) } - + /// Creates a new CGRect object from origin zero with given size. /// /// - Parameters: @@ -166,4 +166,4 @@ extension CGRect { self.init(origin: CGPoint.zero, size: CGSize(width: width, height: height)) } -} \ No newline at end of file +} diff --git a/Sources/Extensions/DictionaryExtension.swift b/Sources/Extensions/DictionaryExtension.swift index 782d4f6..72ccc4a 100644 --- a/Sources/Extensions/DictionaryExtension.swift +++ b/Sources/Extensions/DictionaryExtension.swift @@ -11,24 +11,24 @@ import Foundation extension Dictionary { /// Initializes a new `Dictionary` and fills it with keys and values arrays. - /// + /// /// - Parameters: /// - keys: The `Array` of keys. /// - values: The `Array` of values. public init?(keys: [Key], values: [Value]) { - + guard keys.count == values.count else { return nil } - + self.init() - + for (index, key) in keys.enumerate() { self[key] = values[index] } - + } - + /// Merge given `Dictionary` into this `Dictionary` overriding existing values for matching keys. /// /// - Parameters: @@ -38,26 +38,24 @@ extension Dictionary { self[key] = value } } - - /// Create new merged `Dictionary` with the given `Dictionary` merged into this `Dictionary` + + /// Create new merged `Dictionary` with the given `Dictionary` merged into this `Dictionary` /// overriding existing values for matching keys. /// /// - Parameters: /// - otherDictionary: The other `Dictionary` to merge into this `Dictionary`. /// - Returns: The new Dictionary with merged keys and values from this and the other `Dictionary`. public func mergedWith(otherDictionary: [Key: Value]) -> [Key: Value] { - + var mergedDict: [Key: Value] = [:] - + [self, otherDictionary].forEach { dict in for (key, value) in dict { mergedDict[key] = value } } - + return mergedDict } - - - + } diff --git a/Sources/Extensions/IntExtension.swift b/Sources/Extensions/IntExtension.swift index c020532..75d4e08 100644 --- a/Sources/Extensions/IntExtension.swift +++ b/Sources/Extensions/IntExtension.swift @@ -9,8 +9,8 @@ import Foundation public extension Int { - - /// Initializes a new `Int` instance with a random value below a given `Int`. + + /// Initializes a new `Int ` instance with a random value below a given `Int`. /// /// - Parameters: /// - randomBelow: The upper bound value to create a random value with. @@ -19,9 +19,9 @@ public extension Int { self.init(0) return } - + let randomUInt32 = arc4random_uniform(UInt32(upperLimit)) self.init(randomUInt32) } - + } diff --git a/Sources/Extensions/IntegerTypeExtension.swift b/Sources/Extensions/IntegerTypeExtension.swift index 8f994b1..f7a12b8 100644 --- a/Sources/Extensions/IntegerTypeExtension.swift +++ b/Sources/Extensions/IntegerTypeExtension.swift @@ -11,15 +11,15 @@ import Foundation extension IntegerType { /// Runs the code passed as a closure the specified number of times. - /// + /// /// - Parameters: /// - closure: The code to be run multiple times. public func times(closure: () -> Void) { guard self > 0 else { return } - + for _ in 1...self { closure() } } - + } diff --git a/Sources/Extensions/StringExtension.swift b/Sources/Extensions/StringExtension.swift index 1d4046c..04fe657 100644 --- a/Sources/Extensions/StringExtension.swift +++ b/Sources/Extensions/StringExtension.swift @@ -9,21 +9,21 @@ import Foundation public extension String { - + /// Strips all whitespace characters from beginning and end. /// /// - Returns: The string stripped by whitespace characters from beginning and end. public var strip: String { return self.stringByTrimmingCharactersInSet(.whitespaceCharacterSet()) } - + /// Checks if contains any characters other than whitespace characters. /// /// - Returns: `true` if contains any cahracters other than whitespace characters. public var isBlank: Bool { return self.strip.isEmpty } - + /// The type of allowed characters. /// /// - Numeric: Allow all numbers from 0 to 9. @@ -36,14 +36,14 @@ public extension String { case AlphaNumeric case AllCharactersIn(String) } - + /// Create new instance with random numeric/alphabetic/alphanumeric String of given length. - /// + /// /// - Parameters: /// - randommWithLength: The length of the random String to create. /// - allowedCharactersType: The allowed characters type, see enum `AllowedCharacters`. public init(randomWithLength length: Int, allowedCharactersType: AllowedCharacters) { - + let allowedCharsString: String = { switch allowedCharactersType { case .Numeric: @@ -56,9 +56,9 @@ public extension String { return allowedCharactersString } }() - + self.init(allowedCharsString.characters.sample(size: length)!) - + } - + } diff --git a/Sources/Globals.swift b/Sources/Globals.swift index 3488deb..33c84e4 100644 --- a/Sources/Globals.swift +++ b/Sources/Globals.swift @@ -15,21 +15,21 @@ public enum DispatchLevel { case UserInitiated case Utility case Background - + var dispatchQueue: OS_dispatch_queue { switch self { case .Main: return dispatch_get_main_queue() - + case .UserInteractive: return dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0) - + case .UserInitiated: return dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0) - + case .Utility: return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0) - + case .Background: return dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) } @@ -43,6 +43,6 @@ public enum DispatchLevel { /// - dispatchLevel: The level that defines the thread type. /// - closure: The closure to run with delay. public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .Main, closure: () -> Void) { - let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))) + let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatchLevel.dispatchQueue, closure) } diff --git a/Sources/Structs/FrequencyTable.swift b/Sources/Structs/FrequencyTable.swift index 849846c..bfeeb38 100644 --- a/Sources/Structs/FrequencyTable.swift +++ b/Sources/Structs/FrequencyTable.swift @@ -12,55 +12,55 @@ import Foundation public struct FrequencyTable { // MARK: - Stored Instance Properties - + private let valuesWithFrequencies: [(T, Int)] private let frequentValues: [T] - - + + // MARK: - Initializers - + /// Creates a new FrequencyTable instance with values and their frequencies provided. - /// + /// /// - Parameters: /// - values: An array full of values to be saved into the frequency table. /// - frequencyClosure: The closure to specify the frequency for a specific value. public init(values: [T], frequencyClosure: (T) -> Int) { - + self.valuesWithFrequencies = values.map { ($0, frequencyClosure($0)) } self.frequentValues = Array(self.valuesWithFrequencies.map { (value, frequency) -> [T] in return (0.. [T]? { - + if !self.frequentValues.isEmpty { var sampleElements: [T] = [] - + size.times { sampleElements.append(self.sample!) } - + return sampleElements } - + return nil } - + } diff --git a/Sources/Structs/SortedArray.swift b/Sources/Structs/SortedArray.swift index d7b0683..6e8702b 100644 --- a/Sources/Structs/SortedArray.swift +++ b/Sources/Structs/SortedArray.swift @@ -10,19 +10,19 @@ import Foundation /// Data structure to keep a sorted array of elements for fast access. public struct SortedArray { - + // MARK: - Stored Instance Properties - + private var internalArray: Array = [] - + /// Returns the sorted array of elements. public var array: Array { return self.internalArray } - - + + // MARK: - Initializers - + /// Creates a new SortedArray with a given array of elements and sorts the elements. /// /// - Complexity: Probably O(n * log(n)) -- complexity of `sort()` on an Array. @@ -32,7 +32,7 @@ public struct SortedArray { public init(array: [Element]) { self.init(array: array, preSorted: false) } - + private init(array: [Element], preSorted: Bool) { if preSorted { self.internalArray = array @@ -40,59 +40,59 @@ public struct SortedArray { self.internalArray = array.sort() } } - - + + // MARK: - Instance Methods - + /// Returns the index of the left most matching element. Matching is done via binary search. - /// + /// /// - Complexity: O(log(n)) /// /// - Parameters: /// - predicate: The boolean predicate to match the elements with. /// - Returns: The index of the left most matching element. public func firstMatchingIndex(predicate: Element -> Bool) -> Array.Index? { - + // check if all elements match if let firstElement = self.array.first { if predicate(firstElement) { return self.array.startIndex } } - + // check if no element matches if let lastElement = self.array.last { if !predicate(lastElement) { return nil } } - + // binary search for first matching element var predicateMatched = false - + var lowerIndex = self.array.startIndex var upperIndex = self.array.endIndex - + while lowerIndex != upperIndex { - + let middleIndex = lowerIndex.advancedBy(lowerIndex.distanceTo(upperIndex) / 2) - + if predicate(self.array[middleIndex]) { upperIndex = middleIndex predicateMatched = true } else { lowerIndex = middleIndex.advancedBy(1) } - + } - + if !predicateMatched { return nil } - + return lowerIndex } - + /// Returns a sub array of a SortedArray to a given index without resorting. /// /// - Complexity: O(1) @@ -101,14 +101,14 @@ public struct SortedArray { /// - toIndex: The upper bound index until which to include elements. /// - Returns: A new SortedArray instance including all elements until the specified index. public func subArray(toIndex endIndex: Array.Index) -> SortedArray { - + let range = self.array.startIndex.. { /// - toIndex: The lower bound index from which to start including elements. /// - Returns: A new SortedArray instance including all elements starting at the specified index. public func subArray(fromIndex startIndex: Array.Index) -> SortedArray { - + let range = startIndex.. { public mutating func remove(atIndex index: Array.Index) { self.internalArray.removeAtIndex(index) } - + } From 61241b904dd81f1753222be93fa9c018514c4c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20G=C3=BCnd=C3=BCz?= Date: Sun, 24 Jul 2016 16:37:13 +0200 Subject: [PATCH 3/7] [README] Add codebeat badge --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5e62aa7..302f3a9 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ Build Status + + codebeat badge + Version: 1.2.0 From 1b2898e80085fe8292836037be9f5fbbaefcd699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Wed, 17 Aug 2016 01:56:57 +0200 Subject: [PATCH 4/7] Convert code to Swift 3 --- HandySwift.xcodeproj/project.pbxproj | 6 +++ Sources/Extensions/ArrayExtension.swift | 4 +- .../Extensions/CharacterViewExtension.swift | 4 +- Sources/Extensions/ColorExtension.swift | 38 +++++++++---------- .../Extensions/CoreGraphicsExtensions.swift | 18 ++++----- Sources/Extensions/DictionaryExtension.swift | 6 +-- Sources/Extensions/IntegerTypeExtension.swift | 4 +- Sources/Extensions/StringExtension.swift | 18 ++++----- Sources/Globals.swift | 38 +++++++++---------- Sources/Structs/FrequencyTable.swift | 2 +- Sources/Structs/SortedArray.swift | 12 +++--- Tests/Extensions/ColorExtensionTests.swift | 10 ++--- .../CoreGraphicsExtensionsTests.swift | 8 ++-- .../Extensions/DictionaryExtensionTests.swift | 4 +- Tests/Extensions/StringExtensionTests.swift | 8 ++-- Tests/GlobalsTests.swift | 6 +-- Tests/Structs/FrequencyTableTests.swift | 4 +- 17 files changed, 98 insertions(+), 92 deletions(-) diff --git a/HandySwift.xcodeproj/project.pbxproj b/HandySwift.xcodeproj/project.pbxproj index fc4b471..59a4c04 100644 --- a/HandySwift.xcodeproj/project.pbxproj +++ b/HandySwift.xcodeproj/project.pbxproj @@ -454,9 +454,11 @@ TargetAttributes = { 823B2B301C24AAB6007B3CDD = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 823B2B3A1C24AAB7007B3CDD = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 825EFDC91C3333B000558497 = { CreatedOnToolsVersion = 7.2; @@ -824,6 +826,7 @@ PRODUCT_NAME = HandySwift; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -845,6 +848,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.flinesoft.HandySwift; PRODUCT_NAME = HandySwift; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -857,6 +861,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.flinesoft.HandySwift-iOS-Tests"; PRODUCT_MODULE_NAME = HandySwift_iOS_Tests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -869,6 +874,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.flinesoft.HandySwift-iOS-Tests"; PRODUCT_MODULE_NAME = HandySwift_iOS_Tests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/Sources/Extensions/ArrayExtension.swift b/Sources/Extensions/ArrayExtension.swift index e9ebb6f..3a3b242 100644 --- a/Sources/Extensions/ArrayExtension.swift +++ b/Sources/Extensions/ArrayExtension.swift @@ -15,7 +15,7 @@ public extension Array { /// - Returns: A random element from the array or `nil` if empty. public var sample: Element? { if !self.isEmpty { - let randomIndex = self.startIndex.advancedBy(Int(randomBelow: self.count)) + let randomIndex = self.startIndex.advanced(by: Int(randomBelow: self.count)) return self[randomIndex] } @@ -27,7 +27,7 @@ public extension Array { /// - Parameters: /// - size: The number of random elements wanted. /// - Returns: An array with the given number of random elements or `nil` if empty. - public func sample(size size: Int) -> [Element]? { + public func sample(size: Int) -> [Element]? { if !self.isEmpty { var sampleElements: [Element] = [] diff --git a/Sources/Extensions/CharacterViewExtension.swift b/Sources/Extensions/CharacterViewExtension.swift index b112318..10e53d9 100644 --- a/Sources/Extensions/CharacterViewExtension.swift +++ b/Sources/Extensions/CharacterViewExtension.swift @@ -15,7 +15,7 @@ public extension String.CharacterView { /// - Returns: A random character from the `CharacterView` or `nil` if empty. public var sample: Character? { if !self.isEmpty { - let randomIndex = self.startIndex.advancedBy(Int(randomBelow: self.count)) + let randomIndex = self.index(self.startIndex, offsetBy: Int(randomBelow: self.count)) return self[randomIndex] } @@ -27,7 +27,7 @@ public extension String.CharacterView { /// - Parameters: /// - size: The number of random characters wanted. /// - Returns: A `CharacterView` with the given number of random characters or `nil` if empty. - public func sample(size size: Int) -> String.CharacterView? { + public func sample(size: Int) -> String.CharacterView? { if !self.isEmpty { var sampleElements: String.CharacterView = String.CharacterView() diff --git a/Sources/Extensions/ColorExtension.swift b/Sources/Extensions/ColorExtension.swift index a9cb91c..6733fdf 100644 --- a/Sources/Extensions/ColorExtension.swift +++ b/Sources/Extensions/ColorExtension.swift @@ -23,7 +23,7 @@ /// - Brightness: The brightness color part of HSB & alpha. /// public enum ChangeableAttribute { - case Red, Green, Blue, Hue, Saturation, Brightness, Alpha + case red, green, blue, hue, saturation, brightness, alpha } // MARK: - Computed Properties @@ -53,28 +53,28 @@ /// - attribute: The attribute to change. /// - by: The addition to be added to the current value of the attribute. /// - Returns: The resulting new `UIColor` with the specified change applied. - public func change(attribute: ChangeableAttribute, by addition: CGFloat) -> UIColor { + public func change(_ attribute: ChangeableAttribute, by addition: CGFloat) -> UIColor { switch attribute { - case .Red: + case .red: return self.change(attribute, to: self.rgba.red + addition) - case .Green: + case .green: return self.change(attribute, to: self.rgba.green + addition) - case .Blue: + case .blue: return self.change(attribute, to: self.rgba.blue + addition) - case .Alpha: + case .alpha: return self.change(attribute, to: self.rgba.alpha + addition) - case .Hue: + case .hue: return self.change(attribute, to: self.hsba.hue + addition) - case .Saturation: + case .saturation: return self.change(attribute, to: self.hsba.saturation + addition) - case .Brightness: + case .brightness: return self.change(attribute, to: self.hsba.brightness + addition) } @@ -86,23 +86,23 @@ /// - attribute: The attribute to change. /// - to: The new value to be set for the attribute. /// - Returns: The resulting new `UIColor` with the specified change applied. - public func change(attribute: ChangeableAttribute, to newValue: CGFloat) -> UIColor { // swiftlint:disable:this cyclomatic_complexity + public func change(_ attribute: ChangeableAttribute, to newValue: CGFloat) -> UIColor { // swiftlint:disable:this cyclomatic_complexity switch attribute { - case .Red, .Green, .Blue, .Alpha: + case .red, .green, .blue, .alpha: var newRgba = self.rgba switch attribute { - case .Red: + case .red: newRgba.red = newValue - case .Green: + case .green: newRgba.green = newValue - case .Blue: + case .blue: newRgba.blue = newValue - case .Alpha: + case .alpha: newRgba.alpha = newValue default: break @@ -110,17 +110,17 @@ return UIColor(red: newRgba.red, green: newRgba.green, blue: newRgba.blue, alpha: newRgba.alpha) - case .Hue, .Saturation, .Brightness: + case .hue, .saturation, .brightness: var newHsba = self.hsba switch attribute { - case .Hue: + case .hue: newHsba.hue = newValue - case .Saturation: + case .saturation: newHsba.saturation = newValue - case .Brightness: + case .brightness: newHsba.brightness = newValue default: break diff --git a/Sources/Extensions/CoreGraphicsExtensions.swift b/Sources/Extensions/CoreGraphicsExtensions.swift index d55bfe9..33d4de1 100644 --- a/Sources/Extensions/CoreGraphicsExtensions.swift +++ b/Sources/Extensions/CoreGraphicsExtensions.swift @@ -17,14 +17,14 @@ import Foundation /// Returns a new CGSize object with the width and height converted to true pixels on the main screen. public var inPixels: CGSize { - return inPixels(UIScreen.mainScreen()) + return inPixels(UIScreen.main) } /// Returns a new CGSize object with the width and height converted to true pixels on the given screen. /// /// - Parameters: /// - screen: The target screen to convert to pixels for. - public func inPixels(screen: UIScreen) -> CGSize { + public func inPixels(_ screen: UIScreen) -> CGSize { return CGSize(width: width / screen.scale, height: height / screen.scale) } @@ -37,7 +37,7 @@ import Foundation /// Returns a new CGPoint object with the x and y converted to true pixels on the main screen. public var inPixels: CGPoint { - return inPixels(UIScreen.mainScreen()) + return inPixels(UIScreen.main) } @@ -45,7 +45,7 @@ import Foundation /// /// - Parameters: /// - screen: The target screen to convert to pixels for. - public func inPixels(screen: UIScreen) -> CGPoint { + public func inPixels(_ screen: UIScreen) -> CGPoint { return CGPoint(x: x / screen.scale, y: y / screen.scale) } @@ -58,14 +58,14 @@ import Foundation /// Returns a new CGRect object with the origin and size converted to true pixels on the main screen. public var inPixels: CGRect { - return inPixels(UIScreen.mainScreen()) + return inPixels(UIScreen.main) } /// Returns a new CGRect object with the origin and size converted to true pixels on the given screen. /// /// - Parameters: /// - screen: The target screen to convert to pixels for. - public func inPixels(screen: UIScreen) -> CGRect { + public func inPixels(_ screen: UIScreen) -> CGRect { return CGRect(origin: origin.inPixels(screen), size: size.inPixels(screen)) } @@ -90,7 +90,7 @@ import Foundation /// /// - Parameters: /// - screen: The target screen to convert to pixels for. - public func inPixels(screen: NSScreen) -> CGSize { + public func inPixels(_ screen: NSScreen) -> CGSize { return CGSize(width: width / screen.backingScaleFactor, height: height / screen.backingScaleFactor) } @@ -112,7 +112,7 @@ import Foundation /// /// - Parameters: /// - screen: The target screen to convert to pixels for. - public func inPixels(screen: NSScreen) -> CGPoint { + public func inPixels(_ screen: NSScreen) -> CGPoint { return CGPoint(x: x / screen.backingScaleFactor, y: y / screen.backingScaleFactor) } @@ -136,7 +136,7 @@ import Foundation /// /// - Parameters: /// - screen: The target screen to convert to pixels for. - public func inPixels(screen: NSScreen) -> CGRect { + public func inPixels(_ screen: NSScreen) -> CGRect { return CGRect(origin: origin.inPixels(screen), size: size.inPixels(screen)) } diff --git a/Sources/Extensions/DictionaryExtension.swift b/Sources/Extensions/DictionaryExtension.swift index 72ccc4a..e1f5f5f 100644 --- a/Sources/Extensions/DictionaryExtension.swift +++ b/Sources/Extensions/DictionaryExtension.swift @@ -23,7 +23,7 @@ extension Dictionary { self.init() - for (index, key) in keys.enumerate() { + for (index, key) in keys.enumerated() { self[key] = values[index] } @@ -33,7 +33,7 @@ extension Dictionary { /// /// - Parameters: /// - otherDictionary: The other `Dictionary` to merge into this `Dictionary`. - public mutating func merge(otherDictionary: [Key: Value]) { + public mutating func merge(_ otherDictionary: [Key: Value]) { for (key, value) in otherDictionary { self[key] = value } @@ -45,7 +45,7 @@ extension Dictionary { /// - Parameters: /// - otherDictionary: The other `Dictionary` to merge into this `Dictionary`. /// - Returns: The new Dictionary with merged keys and values from this and the other `Dictionary`. - public func mergedWith(otherDictionary: [Key: Value]) -> [Key: Value] { + public func mergedWith(_ otherDictionary: [Key: Value]) -> [Key: Value] { var mergedDict: [Key: Value] = [:] diff --git a/Sources/Extensions/IntegerTypeExtension.swift b/Sources/Extensions/IntegerTypeExtension.swift index f7a12b8..cb6726a 100644 --- a/Sources/Extensions/IntegerTypeExtension.swift +++ b/Sources/Extensions/IntegerTypeExtension.swift @@ -8,13 +8,13 @@ import Foundation -extension IntegerType { +extension Int { /// Runs the code passed as a closure the specified number of times. /// /// - Parameters: /// - closure: The code to be run multiple times. - public func times(closure: () -> Void) { + public func times(_ closure: () -> Void) { guard self > 0 else { return } for _ in 1...self { diff --git a/Sources/Extensions/StringExtension.swift b/Sources/Extensions/StringExtension.swift index 04fe657..9c2f0f9 100644 --- a/Sources/Extensions/StringExtension.swift +++ b/Sources/Extensions/StringExtension.swift @@ -14,7 +14,7 @@ public extension String { /// /// - Returns: The string stripped by whitespace characters from beginning and end. public var strip: String { - return self.stringByTrimmingCharactersInSet(.whitespaceCharacterSet()) + return self.trimmingCharacters(in: CharacterSet.whitespaces) } /// Checks if contains any characters other than whitespace characters. @@ -31,10 +31,10 @@ public extension String { /// - AlphaNumeric: Allow both numbers and alphabetic characters ignoring case. /// - AllCharactersIn: Allow all characters appearing within the specified String. public enum AllowedCharacters { - case Numeric - case Alphabetic - case AlphaNumeric - case AllCharactersIn(String) + case numeric + case alphabetic + case alphaNumeric + case allCharactersIn(String) } /// Create new instance with random numeric/alphabetic/alphanumeric String of given length. @@ -46,13 +46,13 @@ public extension String { let allowedCharsString: String = { switch allowedCharactersType { - case .Numeric: + case .numeric: return "0123456789" - case .Alphabetic: + case .alphabetic: return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - case .AlphaNumeric: + case .alphaNumeric: return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - case .AllCharactersIn(let allowedCharactersString): + case .allCharactersIn(let allowedCharactersString): return allowedCharactersString } }() diff --git a/Sources/Globals.swift b/Sources/Globals.swift index 33c84e4..0650a84 100644 --- a/Sources/Globals.swift +++ b/Sources/Globals.swift @@ -10,28 +10,28 @@ import Foundation /// The Main thread level or QOS classes as an enum. public enum DispatchLevel { - case Main - case UserInteractive - case UserInitiated - case Utility - case Background + case main + case userInteractive + case userInitiated + case utility + case background - var dispatchQueue: OS_dispatch_queue { + var dispatchQueue: DispatchQueue { switch self { - case .Main: - return dispatch_get_main_queue() + case .main: + return DispatchQueue.main - case .UserInteractive: - return dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0) + case .userInteractive: + return DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive) - case .UserInitiated: - return dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0) + case .userInitiated: + return DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated) - case .Utility: - return dispatch_get_global_queue(QOS_CLASS_UTILITY, 0) + case .utility: + return DispatchQueue.global(qos: DispatchQoS.QoSClass.utility) - case .Background: - return dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) + case .background: + return DispatchQueue.global(qos: DispatchQoS.QoSClass.background) } } } @@ -42,7 +42,7 @@ public enum DispatchLevel { /// - bySeconds: The delay in seconds. /// - dispatchLevel: The level that defines the thread type. /// - closure: The closure to run with delay. -public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .Main, closure: () -> Void) { - let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))) - dispatch_after(dispatchTime, dispatchLevel.dispatchQueue, closure) +public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .main, closure: () -> Void) { + let dispatchTime = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) + dispatchLevel.dispatchQueue.asyncAfter(deadline: dispatchTime, execute: closure) } diff --git a/Sources/Structs/FrequencyTable.swift b/Sources/Structs/FrequencyTable.swift index bfeeb38..f034333 100644 --- a/Sources/Structs/FrequencyTable.swift +++ b/Sources/Structs/FrequencyTable.swift @@ -47,7 +47,7 @@ public struct FrequencyTable { /// - size: The size of the resulting array of random values. /// /// - Returns: An array of random values or nil if values empty. - public func sample(size size: Int) -> [T]? { + public func sample(size: Int) -> [T]? { if !self.frequentValues.isEmpty { var sampleElements: [T] = [] diff --git a/Sources/Structs/SortedArray.swift b/Sources/Structs/SortedArray.swift index 6e8702b..cb8cc53 100644 --- a/Sources/Structs/SortedArray.swift +++ b/Sources/Structs/SortedArray.swift @@ -37,7 +37,7 @@ public struct SortedArray { if preSorted { self.internalArray = array } else { - self.internalArray = array.sort() + self.internalArray = array.sorted() } } @@ -51,7 +51,7 @@ public struct SortedArray { /// - Parameters: /// - predicate: The boolean predicate to match the elements with. /// - Returns: The index of the left most matching element. - public func firstMatchingIndex(predicate: Element -> Bool) -> Array.Index? { + public func firstMatchingIndex(_ predicate: (Element) -> Bool) -> Array.Index? { // check if all elements match if let firstElement = self.array.first { @@ -75,13 +75,13 @@ public struct SortedArray { while lowerIndex != upperIndex { - let middleIndex = lowerIndex.advancedBy(lowerIndex.distanceTo(upperIndex) / 2) + let middleIndex = lowerIndex.advanced(by: lowerIndex.distance(to: upperIndex) / 2) if predicate(self.array[middleIndex]) { upperIndex = middleIndex predicateMatched = true } else { - lowerIndex = middleIndex.advancedBy(1) + lowerIndex = middleIndex.advanced(by: 1) } } @@ -118,7 +118,7 @@ public struct SortedArray { /// - Returns: A new SortedArray instance including all elements starting at the specified index. public func subArray(fromIndex startIndex: Array.Index) -> SortedArray { - let range = startIndex.. { /// - Parameters: /// - atIndex: The index of the element to remove from the sorted array. public mutating func remove(atIndex index: Array.Index) { - self.internalArray.removeAtIndex(index) + self.internalArray.remove(at: index) } } diff --git a/Tests/Extensions/ColorExtensionTests.swift b/Tests/Extensions/ColorExtensionTests.swift index ef0ca50..06a1e90 100644 --- a/Tests/Extensions/ColorExtensionTests.swift +++ b/Tests/Extensions/ColorExtensionTests.swift @@ -40,7 +40,7 @@ import XCTest let rgbaColor = UIColor(red: 0.1, green: 0.2, blue: 0.3, alpha: 0.4) - let changedRgbaColor = rgbaColor.change(.Red, by: 0.1).change(.Green, by: 0.1).change(.Blue, by: 0.1).change(.Alpha, by: 0.1) + let changedRgbaColor = rgbaColor.change(.red, by: 0.1).change(.green, by: 0.1).change(.blue, by: 0.1).change(.alpha, by: 0.1) XCTAssertEqualWithAccuracy(changedRgbaColor.rgba.red, 0.2, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedRgbaColor.rgba.green, 0.3, accuracy: 0.001) @@ -50,7 +50,7 @@ import XCTest let hsbaColor = UIColor(hue: 0.1, saturation: 0.2, brightness: 0.3, alpha: 0.4) - let changedHsbaColor = hsbaColor.change(.Hue, by: 0.1).change(.Saturation, by: 0.1).change(.Brightness, by: 0.1).change(.Alpha, by: 0.1) + let changedHsbaColor = hsbaColor.change(.hue, by: 0.1).change(.saturation, by: 0.1).change(.brightness, by: 0.1).change(.alpha, by: 0.1) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.hue, 0.2, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.saturation, 0.3, accuracy: 0.001) @@ -63,7 +63,7 @@ import XCTest let rgbaColor = UIColor(red: 0.1, green: 0.2, blue: 0.3, alpha: 0.4) - let changedRgbaColor = rgbaColor.change(.Red, to: 1.0).change(.Green, to: 0.9).change(.Blue, to: 0.8).change(.Alpha, to: 0.7) + let changedRgbaColor = rgbaColor.change(.red, to: 1.0).change(.green, to: 0.9).change(.blue, to: 0.8).change(.alpha, to: 0.7) XCTAssertEqualWithAccuracy(changedRgbaColor.rgba.red, 1.0, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedRgbaColor.rgba.green, 0.9, accuracy: 0.001) @@ -73,7 +73,7 @@ import XCTest let hsbaColor = UIColor(hue: 0.1, saturation: 0.2, brightness: 0.3, alpha: 0.4) - let changedHsbaColor = hsbaColor.change(.Hue, to: 1.0).change(.Saturation, to: 0.9).change(.Brightness, to: 0.8).change(.Alpha, to: 0.7) + let changedHsbaColor = hsbaColor.change(.hue, to: 1.0).change(.saturation, to: 0.9).change(.brightness, to: 0.8).change(.alpha, to: 0.7) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.hue, 1.0, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.saturation, 0.9, accuracy: 0.001) @@ -84,4 +84,4 @@ import XCTest } -#endif \ No newline at end of file +#endif diff --git a/Tests/Extensions/CoreGraphicsExtensionsTests.swift b/Tests/Extensions/CoreGraphicsExtensionsTests.swift index 15cb66f..e67230f 100644 --- a/Tests/Extensions/CoreGraphicsExtensionsTests.swift +++ b/Tests/Extensions/CoreGraphicsExtensionsTests.swift @@ -20,9 +20,9 @@ class CoreGraphicsExtensionsTests: XCTestCase { let testSize = CGSize(width: size, height: size) #if UIKIT - let expectedPixelSize = size / UIScreen.mainScreen().scale + let expectedPixelSize = size / UIScreen.main.scale #else - let expectedPixelSize = size / NSScreen.mainScreen()!.backingScaleFactor + let expectedPixelSize = size / NSScreen.main.backingScaleFactor #endif let testSizeInPixels = testSize.inPixels @@ -52,7 +52,7 @@ class CoreGraphicsExtensionsTests: XCTestCase { let testPoint = CGPoint(x: size, y: size) #if UIKIT - let expectedPixelPointSizes = size / UIScreen.mainScreen().scale + let expectedPixelPointSizes = size / UIScreen.main.scale #else let expectedPixelPointSizes = size / NSScreen.mainScreen()!.backingScaleFactor #endif @@ -84,7 +84,7 @@ class CoreGraphicsExtensionsTests: XCTestCase { let testRect = CGRect(x: size, y: size, width: size, height: size) #if UIKIT - let expectedPixelRectSizes = size / UIScreen.mainScreen().scale + let expectedPixelRectSizes = size / UIScreen.main.scale let testRectInPixels = testRect.inPixels #else let expectedPixelRectSizes = size / NSScreen.mainScreen()!.backingScaleFactor diff --git a/Tests/Extensions/DictionaryExtensionTests.swift b/Tests/Extensions/DictionaryExtensionTests.swift index fec68b7..b84cc35 100644 --- a/Tests/Extensions/DictionaryExtensionTests.swift +++ b/Tests/Extensions/DictionaryExtensionTests.swift @@ -15,7 +15,7 @@ class DictionaryExtensionTests: XCTestCase { func testInitWithSameCountKeysAndValues() { let keys = Array(0..<100) - let values = Array(0.stride(to: 10*100, by: 10)) + let values = Array(stride(from: 0, to: 10*100, by: 10)) let dict = Dictionary(keys: keys, values: values) XCTAssertNotNil(dict) @@ -32,7 +32,7 @@ class DictionaryExtensionTests: XCTestCase { func testInitWithDifferentCountKeysAndValues() { let keys = Array(0..<50) - let values = Array(10.stride(to: 10*100, by: 10)) + let values = Array(stride(from: 10, to: 10*100, by: 10)) let dict = Dictionary(keys: keys, values: values) XCTAssertNil(dict) diff --git a/Tests/Extensions/StringExtensionTests.swift b/Tests/Extensions/StringExtensionTests.swift index e610e1c..7591f35 100644 --- a/Tests/Extensions/StringExtensionTests.swift +++ b/Tests/Extensions/StringExtensionTests.swift @@ -36,11 +36,11 @@ class StringExtensionTests: XCTestCase { 10.times { - XCTAssertEqual(String(randomWithLength: 5, allowedCharactersType: .Numeric).characters.count, 5) - XCTAssertFalse(String(randomWithLength: 5, allowedCharactersType: .Numeric).characters.contains("a")) + XCTAssertEqual(String(randomWithLength: 5, allowedCharactersType: .numeric).characters.count, 5) + XCTAssertFalse(String(randomWithLength: 5, allowedCharactersType: .numeric).characters.contains("a")) - XCTAssertEqual(String(randomWithLength: 8, allowedCharactersType: .AlphaNumeric).characters.count, 8) - XCTAssertFalse(String(randomWithLength: 8, allowedCharactersType: .Numeric).characters.contains(".")) + XCTAssertEqual(String(randomWithLength: 8, allowedCharactersType: .alphaNumeric).characters.count, 8) + XCTAssertFalse(String(randomWithLength: 8, allowedCharactersType: .numeric).characters.contains(".")) } diff --git a/Tests/GlobalsTests.swift b/Tests/GlobalsTests.swift index ab25074..cf7e8c4 100644 --- a/Tests/GlobalsTests.swift +++ b/Tests/GlobalsTests.swift @@ -14,16 +14,16 @@ class GlobalsTests: XCTestCase { func testDelayed() { - let expectation = expectationWithDescription("Wait for delay.") + let expectation = self.expectation(description: "Wait for delay.") - let callDate = NSDate() + let callDate = Date() let delaySeconds = 1.5 delay(bySeconds: delaySeconds) { XCTAssertEqualWithAccuracy(callDate.timeIntervalSince1970 + delaySeconds, NSDate().timeIntervalSince1970, accuracy: 0.25) expectation.fulfill() } - waitForExpectationsWithTimeout(delaySeconds + 1.0, handler: nil) + waitForExpectations(timeout: delaySeconds + 1.0, handler: nil) } diff --git a/Tests/Structs/FrequencyTableTests.swift b/Tests/Structs/FrequencyTableTests.swift index c6610d2..49f9b37 100644 --- a/Tests/Structs/FrequencyTableTests.swift +++ b/Tests/Structs/FrequencyTableTests.swift @@ -15,7 +15,7 @@ class FrequencyTableTests: XCTestCase { func testSample() { let values = ["Harry", "Hermione", "Ronald"] - let frequencyTable = FrequencyTable(values: values){ [5, 10, 1][values.indexOf($0)!] } + let frequencyTable = FrequencyTable(values: values){ [5, 10, 1][values.index(of: $0)!] } var allSamples: [String] = [] @@ -40,7 +40,7 @@ class FrequencyTableTests: XCTestCase { func testSampleWithSize() { let values = ["Harry", "Hermione", "Ronald"] - let frequencyTable = FrequencyTable(values: values){ [5, 10, 1][values.indexOf($0)!] } + let frequencyTable = FrequencyTable(values: values){ [5, 10, 1][values.index(of: $0)!] } let allSamples: [String] = frequencyTable.sample(size: 16_000)! From 459f88868852ff7ca993bb7fed62b8cd110bdd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sun, 18 Sep 2016 12:53:55 +0200 Subject: [PATCH 5/7] Update code manually to work with Swift 3 (conventions) --- HandySwift.xcodeproj/project.pbxproj | 25 ++++++- .../xcschemes/HandySwift OSX.xcscheme | 2 +- .../xcschemes/HandySwift iOS.xcscheme | 2 +- .../xcschemes/HandySwift tvOS.xcscheme | 2 +- Sources/Extensions/ArrayExtension.swift | 8 +-- .../Extensions/CharacterViewExtension.swift | 10 +-- Sources/Extensions/ColorExtension.swift | 18 ++--- .../Extensions/CoreGraphicsExtensions.swift | 70 ------------------- Sources/Extensions/StringExtension.swift | 4 +- Sources/Globals.swift | 10 +-- Sources/Structs/FrequencyTable.swift | 14 ++-- Sources/Structs/SortedArray.swift | 4 +- Tests/Extensions/ArrayExtensionTests.swift | 6 +- .../CharacterViewExtensionTests.swift | 6 +- Tests/Extensions/ColorExtensionTests.swift | 2 +- .../CoreGraphicsExtensionsTests.swift | 24 ++++--- Tests/Structs/FrequencyTableTests.swift | 2 +- UsageExamples.playground/Contents.swift | 30 ++++---- .../contents.xcplayground | 2 +- 19 files changed, 99 insertions(+), 142 deletions(-) diff --git a/HandySwift.xcodeproj/project.pbxproj b/HandySwift.xcodeproj/project.pbxproj index 59a4c04..ea3ecd0 100644 --- a/HandySwift.xcodeproj/project.pbxproj +++ b/HandySwift.xcodeproj/project.pbxproj @@ -139,6 +139,7 @@ 82CAE2971C2EE95200F934A7 /* ArrayExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayExtensionTests.swift; sourceTree = ""; }; 82F22E551C26434900E784A2 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; 82F967F31C67A65E0003F12A /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .swiftlint.yml; sourceTree = ""; }; + A16B85EB1D8EA9A200B39055 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -193,6 +194,7 @@ 823B2B271C24AAB6007B3CDD = { isa = PBXGroup; children = ( + A16B85EB1D8EA9A200B39055 /* README.md */, 82F967F31C67A65E0003F12A /* .swiftlint.yml */, 82F22E551C26434900E784A2 /* Package.swift */, 825EFDE11C3333CE00558497 /* Sources */, @@ -449,7 +451,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = Flinesoft; TargetAttributes = { 823B2B301C24AAB6007B3CDD = { @@ -462,15 +464,19 @@ }; 825EFDC91C3333B000558497 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 825EFDD21C3333B000558497 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 825EFDE81C33351200558497 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 825EFDF11C33351300558497 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; }; }; @@ -729,8 +735,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -778,8 +786,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; @@ -799,6 +809,7 @@ MTL_ENABLE_DEBUG_INFO = NO; PRODUCT_MODULE_NAME = HandySwift; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; TVOS_DEPLOYMENT_TARGET = 9.0; VALIDATE_PRODUCT = YES; @@ -813,6 +824,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -836,6 +848,7 @@ APPLICATION_EXTENSION_API_ONLY = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -897,6 +910,7 @@ PRODUCT_NAME = HandySwift; SDKROOT = macosx; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -919,6 +933,7 @@ PRODUCT_NAME = HandySwift; SDKROOT = macosx; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -935,6 +950,7 @@ PRODUCT_MODULE_NAME = HandySwift_OSX_Tests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -951,6 +967,7 @@ PRODUCT_MODULE_NAME = HandySwift_OSX_Tests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = macosx; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -958,6 +975,7 @@ isa = XCBuildConfiguration; buildSettings = { APPLICATION_EXTENSION_API_ONLY = YES; + "CODE_SIGN_IDENTITY[sdk=appletvos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -970,6 +988,7 @@ PRODUCT_NAME = HandySwift; SDKROOT = appletvos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = 3; TVOS_DEPLOYMENT_TARGET = 9.0; }; @@ -979,6 +998,7 @@ isa = XCBuildConfiguration; buildSettings = { APPLICATION_EXTENSION_API_ONLY = YES; + "CODE_SIGN_IDENTITY[sdk=appletvos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -991,6 +1011,7 @@ PRODUCT_NAME = HandySwift; SDKROOT = appletvos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = 3; TVOS_DEPLOYMENT_TARGET = 9.0; }; @@ -1006,6 +1027,7 @@ PRODUCT_MODULE_NAME = HandySwift_tvOS_Tests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = appletvos; + SWIFT_VERSION = 3.0; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Debug; @@ -1020,6 +1042,7 @@ PRODUCT_MODULE_NAME = HandySwift_tvOS_Tests; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = appletvos; + SWIFT_VERSION = 3.0; TVOS_DEPLOYMENT_TARGET = 9.1; }; name = Release; diff --git a/HandySwift.xcodeproj/xcshareddata/xcschemes/HandySwift OSX.xcscheme b/HandySwift.xcodeproj/xcshareddata/xcschemes/HandySwift OSX.xcscheme index 3c93897..a860288 100644 --- a/HandySwift.xcodeproj/xcshareddata/xcschemes/HandySwift OSX.xcscheme +++ b/HandySwift.xcodeproj/xcshareddata/xcschemes/HandySwift OSX.xcscheme @@ -1,6 +1,6 @@ Element? { if !self.isEmpty { - let randomIndex = self.startIndex.advanced(by: Int(randomBelow: self.count)) + let randomIndex = startIndex.advanced(by: Int(randomBelow: self.count)) return self[randomIndex] } @@ -29,11 +29,11 @@ public extension Array { /// - Returns: An array with the given number of random elements or `nil` if empty. public func sample(size: Int) -> [Element]? { - if !self.isEmpty { + if !isEmpty { var sampleElements: [Element] = [] size.times { - sampleElements.append(self.sample!) + sampleElements.append(self.sample()!) } return sampleElements diff --git a/Sources/Extensions/CharacterViewExtension.swift b/Sources/Extensions/CharacterViewExtension.swift index 10e53d9..bde29f0 100644 --- a/Sources/Extensions/CharacterViewExtension.swift +++ b/Sources/Extensions/CharacterViewExtension.swift @@ -13,9 +13,9 @@ public extension String.CharacterView { /// Returns a random character from the `ChracterView`. /// /// - Returns: A random character from the `CharacterView` or `nil` if empty. - public var sample: Character? { + public func sample() -> Character? { if !self.isEmpty { - let randomIndex = self.index(self.startIndex, offsetBy: Int(randomBelow: self.count)) + let randomIndex = index(startIndex, offsetBy: Int(randomBelow: count)) return self[randomIndex] } @@ -29,11 +29,11 @@ public extension String.CharacterView { /// - Returns: A `CharacterView` with the given number of random characters or `nil` if empty. public func sample(size: Int) -> String.CharacterView? { - if !self.isEmpty { - var sampleElements: String.CharacterView = String.CharacterView() + if !isEmpty { + var sampleElements = String.CharacterView() size.times { - sampleElements.append(self.sample!) + sampleElements.append(sample()!) } return sampleElements diff --git a/Sources/Extensions/ColorExtension.swift b/Sources/Extensions/ColorExtension.swift index 6733fdf..db32d13 100644 --- a/Sources/Extensions/ColorExtension.swift +++ b/Sources/Extensions/ColorExtension.swift @@ -32,7 +32,7 @@ public var hsba: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) { var hsba: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) = (0, 0, 0, 0) - self.getHue(&(hsba.hue), saturation: &(hsba.saturation), brightness: &(hsba.brightness), alpha: &(hsba.alpha)) + getHue(&(hsba.hue), saturation: &(hsba.saturation), brightness: &(hsba.brightness), alpha: &(hsba.alpha)) return hsba } @@ -40,7 +40,7 @@ public var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) = (0, 0, 0, 0) - self.getRed(&rgba.red, green: &rgba.green, blue: &rgba.blue, alpha: &rgba.alpha) + getRed(&rgba.red, green: &rgba.green, blue: &rgba.blue, alpha: &rgba.alpha) return rgba } @@ -57,25 +57,25 @@ switch attribute { case .red: - return self.change(attribute, to: self.rgba.red + addition) + return change(attribute, to: rgba.red + addition) case .green: - return self.change(attribute, to: self.rgba.green + addition) + return change(attribute, to: rgba.green + addition) case .blue: - return self.change(attribute, to: self.rgba.blue + addition) + return change(attribute, to: rgba.blue + addition) case .alpha: - return self.change(attribute, to: self.rgba.alpha + addition) + return change(attribute, to: rgba.alpha + addition) case .hue: - return self.change(attribute, to: self.hsba.hue + addition) + return change(attribute, to: hsba.hue + addition) case .saturation: - return self.change(attribute, to: self.hsba.saturation + addition) + return change(attribute, to: hsba.saturation + addition) case .brightness: - return self.change(attribute, to: self.hsba.brightness + addition) + return change(attribute, to: hsba.brightness + addition) } } diff --git a/Sources/Extensions/CoreGraphicsExtensions.swift b/Sources/Extensions/CoreGraphicsExtensions.swift index 33d4de1..8daaab3 100644 --- a/Sources/Extensions/CoreGraphicsExtensions.swift +++ b/Sources/Extensions/CoreGraphicsExtensions.swift @@ -71,76 +71,6 @@ import Foundation } -#else - import AppKit - - // MARK: - OSX CGSize Extension - - extension CGSize { - - /// Returns a new CGSize object with the width and height converted to true pixels on the main screen. - public var inPixels: CGSize { - guard let mainScreen = NSScreen.mainScreen() else { - return CGSize.zero - } - return inPixels(mainScreen) - } - - /// Returns a new CGSize object with the width and height converted to true pixels on the given screen. - /// - /// - Parameters: - /// - screen: The target screen to convert to pixels for. - public func inPixels(_ screen: NSScreen) -> CGSize { - return CGSize(width: width / screen.backingScaleFactor, height: height / screen.backingScaleFactor) - } - - } - - // MARK: - OSX CGPoint Extension - - extension CGPoint { - - /// Returns a new CGPoint object with the x and y converted to true pixels on the main screen. - public var inPixels: CGPoint { - guard let mainScreen = NSScreen.mainScreen() else { - return CGPoint.zero - } - return inPixels(mainScreen) - } - - /// Returns a new CGPoint object with the x and y converted to true pixels on the given screen. - /// - /// - Parameters: - /// - screen: The target screen to convert to pixels for. - public func inPixels(_ screen: NSScreen) -> CGPoint { - return CGPoint(x: x / screen.backingScaleFactor, y: y / screen.backingScaleFactor) - } - - } - - - // MARK: - OSX CGRect Extension - - extension CGRect { - - /// Returns a new CGRect object with the origin and size converted to true pixels on the main screen or - /// returns `nil` if no main screen found. - public var inPixels: CGRect? { - guard let mainScreen = NSScreen.mainScreen() else { - return nil - } - return inPixels(mainScreen) - } - - /// Returns a new CGRect object with the origin and size converted to true pixels on the given screen. - /// - /// - Parameters: - /// - screen: The target screen to convert to pixels for. - public func inPixels(_ screen: NSScreen) -> CGRect { - return CGRect(origin: origin.inPixels(screen), size: size.inPixels(screen)) - } - - } #endif diff --git a/Sources/Extensions/StringExtension.swift b/Sources/Extensions/StringExtension.swift index 9c2f0f9..6f07808 100644 --- a/Sources/Extensions/StringExtension.swift +++ b/Sources/Extensions/StringExtension.swift @@ -14,14 +14,14 @@ public extension String { /// /// - Returns: The string stripped by whitespace characters from beginning and end. public var strip: String { - return self.trimmingCharacters(in: CharacterSet.whitespaces) + return trimmingCharacters(in: CharacterSet.whitespaces) } /// Checks if contains any characters other than whitespace characters. /// /// - Returns: `true` if contains any cahracters other than whitespace characters. public var isBlank: Bool { - return self.strip.isEmpty + return strip.isEmpty } /// The type of allowed characters. diff --git a/Sources/Globals.swift b/Sources/Globals.swift index 0650a84..157a8b1 100644 --- a/Sources/Globals.swift +++ b/Sources/Globals.swift @@ -22,16 +22,16 @@ public enum DispatchLevel { return DispatchQueue.main case .userInteractive: - return DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive) + return DispatchQueue.global(qos: .userInteractive) case .userInitiated: - return DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated) + return DispatchQueue.global(qos: .userInitiated) case .utility: - return DispatchQueue.global(qos: DispatchQoS.QoSClass.utility) + return DispatchQueue.global(qos: .utility) case .background: - return DispatchQueue.global(qos: DispatchQoS.QoSClass.background) + return DispatchQueue.global(qos: .background) } } } @@ -42,7 +42,7 @@ public enum DispatchLevel { /// - bySeconds: The delay in seconds. /// - dispatchLevel: The level that defines the thread type. /// - closure: The closure to run with delay. -public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .main, closure: () -> Void) { +public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .main, closure: @escaping () -> Void) { let dispatchTime = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) dispatchLevel.dispatchQueue.asyncAfter(deadline: dispatchTime, execute: closure) } diff --git a/Sources/Structs/FrequencyTable.swift b/Sources/Structs/FrequencyTable.swift index f034333..cce2110 100644 --- a/Sources/Structs/FrequencyTable.swift +++ b/Sources/Structs/FrequencyTable.swift @@ -13,8 +13,8 @@ public struct FrequencyTable { // MARK: - Stored Instance Properties - private let valuesWithFrequencies: [(T, Int)] - private let frequentValues: [T] + fileprivate let valuesWithFrequencies: [(T, Int)] + fileprivate let frequentValues: [T] // MARK: - Initializers @@ -29,16 +29,16 @@ public struct FrequencyTable { self.valuesWithFrequencies = values.map { ($0, frequencyClosure($0)) } self.frequentValues = Array(self.valuesWithFrequencies.map { (value, frequency) -> [T] in return (0.. T? { + return frequentValues.sample() } /// Returns an array of random values taking frequencies into account or nil if values empty. @@ -53,7 +53,7 @@ public struct FrequencyTable { var sampleElements: [T] = [] size.times { - sampleElements.append(self.sample!) + sampleElements.append(self.sample()!) } return sampleElements diff --git a/Sources/Structs/SortedArray.swift b/Sources/Structs/SortedArray.swift index cb8cc53..d95612b 100644 --- a/Sources/Structs/SortedArray.swift +++ b/Sources/Structs/SortedArray.swift @@ -13,7 +13,7 @@ public struct SortedArray { // MARK: - Stored Instance Properties - private var internalArray: Array = [] + fileprivate var internalArray: Array = [] /// Returns the sorted array of elements. public var array: Array { @@ -33,7 +33,7 @@ public struct SortedArray { self.init(array: array, preSorted: false) } - private init(array: [Element], preSorted: Bool) { + fileprivate init(array: [Element], preSorted: Bool) { if preSorted { self.internalArray = array } else { diff --git a/Tests/Extensions/ArrayExtensionTests.swift b/Tests/Extensions/ArrayExtensionTests.swift index 566c28b..baec173 100644 --- a/Tests/Extensions/ArrayExtensionTests.swift +++ b/Tests/Extensions/ArrayExtensionTests.swift @@ -14,9 +14,9 @@ class ArrayExtensionTests: XCTestCase { func testSample() { - XCTAssertNil([].sample) - XCTAssertNotNil([1,2,3].sample) - XCTAssertTrue([1,2,3].contains([1,2,3].sample!)) + XCTAssertNil([].sample()) + XCTAssertNotNil([1,2,3].sample()) + XCTAssertTrue([1,2,3].contains([1,2,3].sample()!)) } diff --git a/Tests/Extensions/CharacterViewExtensionTests.swift b/Tests/Extensions/CharacterViewExtensionTests.swift index f21e8be..20fceba 100644 --- a/Tests/Extensions/CharacterViewExtensionTests.swift +++ b/Tests/Extensions/CharacterViewExtensionTests.swift @@ -14,9 +14,9 @@ class CharacterViewExtensionTests: XCTestCase { func testSample() { - XCTAssertNil("".characters.sample) - XCTAssertNotNil("abc".characters.sample) - XCTAssertTrue("abc".characters.contains("abc".characters.sample!)) + XCTAssertNil("".characters.sample()) + XCTAssertNotNil("abc".characters.sample()) + XCTAssertTrue("abc".characters.contains("abc".characters.sample()!)) } diff --git a/Tests/Extensions/ColorExtensionTests.swift b/Tests/Extensions/ColorExtensionTests.swift index 06a1e90..2762989 100644 --- a/Tests/Extensions/ColorExtensionTests.swift +++ b/Tests/Extensions/ColorExtensionTests.swift @@ -75,7 +75,7 @@ import XCTest let changedHsbaColor = hsbaColor.change(.hue, to: 1.0).change(.saturation, to: 0.9).change(.brightness, to: 0.8).change(.alpha, to: 0.7) - XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.hue, 1.0, accuracy: 0.001) + XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.hue, 0.0, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.saturation, 0.9, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.brightness, 0.8, accuracy: 0.001) XCTAssertEqualWithAccuracy(changedHsbaColor.hsba.alpha, 0.7, accuracy: 0.001) diff --git a/Tests/Extensions/CoreGraphicsExtensionsTests.swift b/Tests/Extensions/CoreGraphicsExtensionsTests.swift index e67230f..dc443fd 100644 --- a/Tests/Extensions/CoreGraphicsExtensionsTests.swift +++ b/Tests/Extensions/CoreGraphicsExtensionsTests.swift @@ -15,14 +15,16 @@ import XCTest class CoreGraphicsExtensionsTests: XCTestCase { let size: CGFloat = 22 - + +#if UIKIT + func testCGSizeInPixels() { let testSize = CGSize(width: size, height: size) #if UIKIT let expectedPixelSize = size / UIScreen.main.scale #else - let expectedPixelSize = size / NSScreen.main.backingScaleFactor + let expectedPixelSize = size / (NSScreen.main()?.backingScaleFactor)! #endif let testSizeInPixels = testSize.inPixels @@ -54,7 +56,7 @@ class CoreGraphicsExtensionsTests: XCTestCase { #if UIKIT let expectedPixelPointSizes = size / UIScreen.main.scale #else - let expectedPixelPointSizes = size / NSScreen.mainScreen()!.backingScaleFactor + let expectedPixelPointSizes = size / NSScreen.main()!.backingScaleFactor #endif let testPointInPixels = testPoint.inPixels @@ -87,7 +89,7 @@ class CoreGraphicsExtensionsTests: XCTestCase { let expectedPixelRectSizes = size / UIScreen.main.scale let testRectInPixels = testRect.inPixels #else - let expectedPixelRectSizes = size / NSScreen.mainScreen()!.backingScaleFactor + let expectedPixelRectSizes = size / NSScreen.main()!.backingScaleFactor let testRectInPixels = testRect.inPixels! #endif @@ -115,13 +117,15 @@ class CoreGraphicsExtensionsTests: XCTestCase { XCTAssertEqualWithAccuracy(testRectInPixels.size.width, expectedPixelRectSizes, accuracy: 0.001) XCTAssertEqualWithAccuracy(testRectInPixels.size.height, expectedPixelRectSizes, accuracy: 0.001) } - + +#endif + func testCGRectInitSize() { let testSize = CGSize(width: size, height: size) let testRect = CGRect(size: testSize) - - XCTAssertEqualWithAccuracy(testRect.origin.x, 0, accuracy: 0.001) - XCTAssertEqualWithAccuracy(testRect.origin.y, 0, accuracy: 0.001) + + XCTAssertEqualWithAccuracy(testRect.origin.x, 0.0, accuracy: 0.001) + XCTAssertEqualWithAccuracy(testRect.origin.y, 0.0, accuracy: 0.001) XCTAssertEqualWithAccuracy(testRect.size.width, testSize.width, accuracy: 0.001) XCTAssertEqualWithAccuracy(testRect.size.height, testSize.height, accuracy: 0.001) } @@ -129,8 +133,8 @@ class CoreGraphicsExtensionsTests: XCTestCase { func testCGRectInitWidthHeight() { let testRect = CGRect(width: size, height: size) - XCTAssertEqualWithAccuracy(testRect.origin.x, 0, accuracy: 0.001) - XCTAssertEqualWithAccuracy(testRect.origin.y, 0, accuracy: 0.001) + XCTAssertEqualWithAccuracy(testRect.origin.x, 0.0, accuracy: 0.001) + XCTAssertEqualWithAccuracy(testRect.origin.y, 0.0, accuracy: 0.001) XCTAssertEqualWithAccuracy(testRect.size.width, size, accuracy: 0.001) XCTAssertEqualWithAccuracy(testRect.size.height, size, accuracy: 0.001) } diff --git a/Tests/Structs/FrequencyTableTests.swift b/Tests/Structs/FrequencyTableTests.swift index 49f9b37..9fd49e5 100644 --- a/Tests/Structs/FrequencyTableTests.swift +++ b/Tests/Structs/FrequencyTableTests.swift @@ -20,7 +20,7 @@ class FrequencyTableTests: XCTestCase { var allSamples: [String] = [] 16_000.times { - allSamples.append(frequencyTable.sample!) + allSamples.append(frequencyTable.sample()!) } let harryCount = allSamples.filter{ $0 == "Harry" }.count diff --git a/UsageExamples.playground/Contents.swift b/UsageExamples.playground/Contents.swift index abfd679..f4805ee 100644 --- a/UsageExamples.playground/Contents.swift +++ b/UsageExamples.playground/Contents.swift @@ -19,10 +19,10 @@ delay(bySeconds: 1.5) { print("Delayed by 1.5 seconds: \(date)") } -delay(bySeconds: 5, dispatchLevel: .UserInteractive) { +delay(bySeconds: 5, dispatchLevel: .userInteractive) { date = NSDate() print("Delayed by 5 seconds: \(date)") - + // Finish up the run of the Playground XCPlaygroundPage.currentPage.finishExecution() } @@ -73,10 +73,10 @@ intArray //: ### init(randomWithLength:allowedCharactersType:) //: Get random numeric/alphabetic/alphanumeric String of given length. -String(randomWithLength: 4, allowedCharactersType: .Numeric) -String(randomWithLength: 6, allowedCharactersType: .Alphabetic) -String(randomWithLength: 8, allowedCharactersType: .AlphaNumeric) -String(randomWithLength: 10, allowedCharactersType: .AllCharactersIn("?!🐲🍏✈️🎎🍜")) +String(randomWithLength: 4, allowedCharactersType: .numeric) +String(randomWithLength: 6, allowedCharactersType: .alphabetic) +String(randomWithLength: 8, allowedCharactersType: .alphaNumeric) +String(randomWithLength: 10, allowedCharactersType: .allCharactersIn("?!🐲🍏✈️🎎🍜")) //: ## DictionaryExtension @@ -109,8 +109,8 @@ mergedDict //: ### .sample //: Returns a random element within the array or nil if array empty. -[1, 2, 3, 4, 5].sample -([] as [Int]).sample +[1, 2, 3, 4, 5].sample() +([] as [Int]).sample() //: ### .sample(size:) //: Returns an array with `size` random elements or nil if array empty. @@ -144,14 +144,14 @@ hsbaColor.hsba.alpha //: Creates a new `UIColor` object with a single attribute changed by a given difference using addition. rgbaColor.rgba.blue -let newRgbaColor = rgbaColor.change(.Blue, by: 0.2) +let newRgbaColor = rgbaColor.change(.blue, by: 0.2) newRgbaColor.rgba.blue //: ### .change(ChangeableAttribute, to:) //: Creates a new `UIColor` object with the value of a single attribute set to a given value. hsbaColor.hsba.brightness -let newHsbaColor = hsbaColor.change(.Brightness, to: 0.8) +let newHsbaColor = hsbaColor.change(.brightness, to: 0.8) newHsbaColor.hsba.brightness //: ## CoreGraphicsExtensions @@ -160,21 +160,21 @@ newHsbaColor.hsba.brightness let size = CGSize(width: 100, height: 50) size.inPixels // test this with a Retina screen target -size.inPixels(UIScreen.screens().last!) // pass a different screen +size.inPixels(UIScreen.screens.last!) // pass a different screen //: ### CGPoint.inPixels / CGPoint.inPixels(screen:) //: Returns a new CGPoint object with the x and y converted to true pixels on screen. let point = CGPoint(x: 100, y: 50) point.inPixels // test this with a Retina screen target -point.inPixels(UIScreen.screens().last!) // pass a different screen +point.inPixels(UIScreen.screens.last!) // pass a different screen //: ### CGRect.inPixels / CGRect.inPixels(screen:) //: Returns a new CGRect object with the origin and size converted to true pixels on screen. let rect = CGRect(x: 10, y: 20, width: 100, height: 50) rect.inPixels // test this with a Retina screen target -rect.inPixels(UIScreen.screens().last!) // pass a different screen +rect.inPixels(UIScreen.screens.last!) // pass a different screen //: ### CGRect.init(size:) / CGRect.init(width:height:) //: Creates a new CGRect object from origin zero with given size. @@ -236,8 +236,8 @@ frequencyTable //: ### .sample //: Returns a random element with frequency-based probability within the array or nil if array empty. -frequencyTable.sample -let randomWord = frequencyTable.sample.map{ $0.word } +frequencyTable.sample() +let randomWord = frequencyTable.sample().map{ $0.word } randomWord //: ### .sample(size:) diff --git a/UsageExamples.playground/contents.xcplayground b/UsageExamples.playground/contents.xcplayground index 3596865..076deff 100644 --- a/UsageExamples.playground/contents.xcplayground +++ b/UsageExamples.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file From 2333757223e3f2ad35d25dc2ff498138a3db82d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sun, 18 Sep 2016 13:01:07 +0200 Subject: [PATCH 6/7] [README] Update to Swift 3 --- README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 302f3a9..9d1dce9 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ Version: 1.2.0 - Swift: 2.2 + Swift: 3 Platforms: iOS | tvOS | OS X @@ -91,7 +91,7 @@ var date = NSDate() // Without delay: 2016-06-07 05:38:03 +0000 delay(bySeconds: 1.5) { // Runs in Main thread by default date = NSDate() // Delayed by 1.5 seconds: 2016-06-07 05:38:05 +0000 } -delay(bySeconds: 5, dispatchLevel: .UserInteractive) { +delay(bySeconds: 5, dispatchLevel: .userInteractive) { date = NSDate() // Delayed by 5 seconds: 2016-06-07 05:38:08 +0000 } ``` @@ -149,10 +149,10 @@ Checks if String contains any characters other than whitespace characters. Get random numeric/alphabetic/alphanumeric String of given length. ``` Swift -String(randomWithLength: 4, allowedCharactersType: .Numeric) // => "8503" -String(randomWithLength: 6, allowedCharactersType: .Alphabetic) // => "ysTUzU" -String(randomWithLength: 8, allowedCharactersType: .AlphaNumeric) // => "2TgM5sUG" -String(randomWithLength: 10, allowedCharactersType: .AllCharactersIn("?!🐲🍏✈️🎎🍜")) +String(randomWithLength: 4, allowedCharactersType: .numeric) // => "8503" +String(randomWithLength: 6, allowedCharactersType: .alphabetic) // => "ysTUzU" +String(randomWithLength: 8, allowedCharactersType: .alphaNumeric) // => "2TgM5sUG" +String(randomWithLength: 10, allowedCharactersType: .allCharactersIn("?!🐲🍏✈️🎎🍜")) // => "!🍏🐲✈️🎎🐲🍜??🍜" ``` @@ -163,8 +163,8 @@ String(randomWithLength: 10, allowedCharactersType: .AllCharactersIn("?!🐲🍏 Returns a random element within the array or nil if array empty. ``` Swift -[1, 2, 3, 4, 5].sample // => 4 -([] as [Int]).sample // => nil +[1, 2, 3, 4, 5].sample() // => 4 +([] as [Int]).sample() // => nil ``` #### .sample(size:) @@ -241,7 +241,7 @@ Creates a new `UIColor` object with a single attribute changed by a given differ ``` Swift rgbaColor.rgba.blue // => 0.3 -let newRgbaColor = rgbaColor.change(.Blue, by: 0.2) +let newRgbaColor = rgbaColor.change(.blue, by: 0.2) newRgbaColor.rgba.blue // => 0.5 ``` @@ -250,11 +250,11 @@ Creates a new `UIColor` object with the value of a single attribute set to a giv ``` Swift hsbaColor.hsba.brightness // => 0.3 -let newHsbaColor = hsbaColor.change(.Brightness, to: 0.8) +let newHsbaColor = hsbaColor.change(.brightness, to: 0.8) newHsbaColor.hsba.brightness // => 0.8 ``` -### CoreGraphicsExtensions +### CoreGraphicsExtensions (partly iOS & tvOS only) #### CGSize.inPixels / CGSize.inPixels(screen:) Returns a new CGSize object with the width and height converted to true pixels on screen. @@ -263,7 +263,7 @@ Returns a new CGSize object with the width and height converted to true pixels o let size = CGSize(width: 100, height: 50) size.inPixels // test this with a Retina screen target // => {w 50 h 25} -size.inPixels(UIScreen.screens().last!) // pass a different screen +size.inPixels(UIScreen.screens.last!) // pass a different screen // => {w 100 h 50} ``` @@ -274,7 +274,7 @@ Returns a new CGPoint object with the x and y converted to true pixels on screen let point = CGPoint(x: 100, y: 50) point.inPixels // test this with a Retina screen target // => {x 50 y 25} -let someScreen = UIScreen.screens().last! +let someScreen = UIScreen.screens.last! point.inPixels(someScreen) // pass a different screen // => {x 100 y 50} ``` @@ -286,7 +286,7 @@ Returns a new CGRect object with the origin and size converted to true pixels on let rect = CGRect(x: 10, y: 20, width: 100, height: 50) rect.inPixels // test this with a Retina screen target // => {x 5 y 10 w 50 h 25} -let someScreen = UIScreen.screens().last! +let someScreen = UIScreen.screens.last! rect.inPixels(someScreen) // pass a different screen // => {x 10 y 20 w 100 h 50} ``` @@ -363,8 +363,8 @@ let frequencyTable = FrequencyTable(values: wordFrequencies){ $0.frequency } Returns a random element with frequency-based probability within the array or nil if array empty. ``` Swift -frequencyTable.sample -let randomWord = frequencyTable.sample.map{ $0.word } +frequencyTable.sample() +let randomWord = frequencyTable.sample().map{ $0.word } // => "Harry" ``` From b3f0d9e4dd9217f84751cddf2bde45f74c77f385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Sun, 18 Sep 2016 13:02:48 +0200 Subject: [PATCH 7/7] Bump version num to 1.3.0 --- HandySwift.podspec | 4 ++-- README.md | 4 ++-- Sources/Supporting Files/Info.plist | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/HandySwift.podspec b/HandySwift.podspec index e2ae63a..9f59a53 100644 --- a/HandySwift.podspec +++ b/HandySwift.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "HandySwift" - s.version = "1.2.0" + s.version = "1.3.0" s.summary = "Handy Swift features that didn't make it into the Swift standard library" s.description = <<-DESC @@ -20,7 +20,7 @@ Pod::Spec.new do |s| s.osx.deployment_target = "10.10" s.tvos.deployment_target = "9.0" - s.source = { :git => "https://github.com/Flinesoft/HandySwift.git", :tag => "1.2.0" } + s.source = { :git => "https://github.com/Flinesoft/HandySwift.git", :tag => "1.3.0" } s.source_files = "Sources", "Sources/**/*.swift" s.framework = "Foundation" s.osx.framework = "AppKit" diff --git a/README.md b/README.md index 9d1dce9..eac2642 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ alt="codebeat badge"> - Version: 1.2.0 + Version: 1.3.0 Swift: 3 diff --git a/Sources/Supporting Files/Info.plist b/Sources/Supporting Files/Info.plist index 331a2da..9f4a4aa 100644 --- a/Sources/Supporting Files/Info.plist +++ b/Sources/Supporting Files/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.2.0 + 1.3.0 CFBundleSignature ???? CFBundleVersion