Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 4 #457

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions EZSwiftExtensions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,11 @@
};
B5DC86A81C0ED06700972D0A = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
B5DC86B21C0ED06700972D0A = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
Expand Down Expand Up @@ -1498,7 +1498,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensions;
PRODUCT_NAME = EZSwiftExtensions;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -1518,7 +1519,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensions;
PRODUCT_NAME = EZSwiftExtensions;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -1530,7 +1532,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensionsTests;
PRODUCT_NAME = EZSwiftExtensionsTest;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TEST_HOST = "";
};
name = Debug;
Expand All @@ -1542,7 +1545,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensionsTests;
PRODUCT_NAME = EZSwiftExtensionsTest;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TEST_HOST = "";
};
name = Release;
Expand Down
5 changes: 3 additions & 2 deletions EZSwiftExtensionsTests/ArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ class ArrayTests: XCTestCase {
XCTAssertEqual(totalIndexes, 10)
XCTAssertEqual(totalNumbers, 20)

emptyArray.forEachEnumerated { _ in XCTFail() }
emptyArray.forEachEnumerated { _,_ in XCTFail() }
let copyArray = someArray
copyArray.forEachEnumerated { XCTAssertTrue(someArray[$0.0] == $0.1) }
// copyArray.forEachEnumerated { XCTAssertTrue(someArray[$0.0] == $0.1) }
XCTFail()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets put TODOs here and everywhere else we have interjected a deliberate Fail.

}

func testUnion() {
Expand Down
8 changes: 4 additions & 4 deletions EZSwiftExtensionsTests/DictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ class DictionaryTests: XCTestCase {
}

func testFilter() {
let secondFiltered = secondDic.filter { key, value in key != "five" }

XCTAssertTrue(secondFiltered.has("four"))
XCTAssertEqual(secondFiltered.count, 1)
// let secondFiltered = secondDic.filter { key, value in key != "five" }
XCTFail()
// XCTAssertTrue(secondFiltered.has("four"))
// XCTAssertEqual(secondFiltered.count, 1)

}

Expand Down
12 changes: 6 additions & 6 deletions EZSwiftExtensionsTests/NSAttributedStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import EZSwiftExtensions
class NSAttributedStringTests: XCTestCase {

let testAttributedString = NSAttributedString(string: "Swift Attributed String",
attributes: [String:Any]())
attributes: [:])
override func setUp() {
super.setUp()
}
Expand All @@ -30,7 +30,7 @@ class NSAttributedStringTests: XCTestCase {
let boldString = testAttributedString.bold()
let newAttributesSeen = boldString.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, boldString.length))

XCTAssertEqual(newAttributesSeen[NSFontAttributeName] as! UIFont, UIFont.boldSystemFont(ofSize: UIFont.systemFontSize))
XCTAssertEqual(newAttributesSeen[NSAttributedStringKey.font] as! UIFont, UIFont.boldSystemFont(ofSize: UIFont.systemFontSize))
}

#endif
Expand All @@ -41,7 +41,7 @@ class NSAttributedStringTests: XCTestCase {
let underLineString = testAttributedString.underline()
let newAttributesSeen = underLineString.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, underLineString.length))

XCTAssertEqual(newAttributesSeen[NSUnderlineStyleAttributeName] as! Int, NSUnderlineStyle.styleSingle.rawValue)
XCTAssertEqual(newAttributesSeen[NSAttributedStringKey.underlineStyle] as! Int, NSUnderlineStyle.styleSingle.rawValue)
}

#endif
Expand All @@ -52,14 +52,14 @@ class NSAttributedStringTests: XCTestCase {
let italicString = testAttributedString.italic()
let newAttributesSeen = italicString.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, italicString.length))

XCTAssertEqual(newAttributesSeen[NSFontAttributeName] as! UIFont, UIFont.italicSystemFont(ofSize: UIFont.systemFontSize))
XCTAssertEqual(newAttributesSeen[NSAttributedStringKey.font] as! UIFont, UIFont.italicSystemFont(ofSize: UIFont.systemFontSize))
}

func testStrikethrough() {
let strikeThroughString = testAttributedString.strikethrough()
let newAttributesSeen = strikeThroughString.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, strikeThroughString.length))

XCTAssertEqual(newAttributesSeen[NSStrikethroughStyleAttributeName] as! NSNumber, NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int))
XCTAssertEqual(newAttributesSeen[NSAttributedStringKey.strikethroughStyle] as! NSNumber, NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int))
}

#endif
Expand All @@ -71,7 +71,7 @@ class NSAttributedStringTests: XCTestCase {
let coloredString = testAttributedString.color(grayColor)
let newAttributesSeen = coloredString.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, coloredString.length))

XCTAssertEqual(newAttributesSeen[NSForegroundColorAttributeName] as! UIColor, grayColor)
XCTAssertEqual(newAttributesSeen[NSAttributedStringKey.foregroundColor] as! UIColor, grayColor)
}

func testAppending() {
Expand Down
19 changes: 10 additions & 9 deletions EZSwiftExtensionsTests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ class StringTests: XCTestCase {
let testString = "H3yH0L3tsG0"
let expectedResult = ["3","0","3","0"]

XCTAssertEqual(testString.matchesForRegexInText("[0-9]"), expectedResult)
// XCTAssertEqual(testString.matchesForRegexInText("[0-9]"), expectedResult)
XCTFail()
}

func testIsNumber() {
Expand All @@ -371,16 +372,16 @@ class StringTests: XCTestCase {
let testString = "meh"
let testString2 = "✅"

let boldResult = NSAttributedString(string: testString, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
let boldResult2 = NSAttributedString(string: testString2, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
let underlineResult = NSAttributedString(string: testString, attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
let underlineResult2 = NSAttributedString(string: testString2, attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
let boldResult = NSAttributedString(string: testString, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a new lint thing? Or did you interject these spaces accidentally?

let boldResult2 = NSAttributedString(string: testString2, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
let underlineResult = NSAttributedString(string: testString, attributes: [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
let underlineResult2 = NSAttributedString(string: testString2, attributes: [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])

let italicResult = NSAttributedString(string: testString, attributes: [NSFontAttributeName: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
let italicResult2 = NSAttributedString(string: testString2, attributes: [NSFontAttributeName: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
let italicResult = NSAttributedString(string: testString, attributes: [NSAttributedStringKey.font: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
let italicResult2 = NSAttributedString(string: testString2, attributes: [NSAttributedStringKey.font: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])

let colorResult = NSAttributedString(string: testString, attributes: [NSForegroundColorAttributeName: UIColor.green])
let colorResult2 = NSAttributedString(string: testString2, attributes: [NSForegroundColorAttributeName: UIColor.green])
let colorResult = NSAttributedString(string: testString, attributes: [NSAttributedStringKey.foregroundColor: UIColor.green])
let colorResult2 = NSAttributedString(string: testString2, attributes: [NSAttributedStringKey.foregroundColor: UIColor.green])

XCTAssertEqual(testString.bold(), boldResult)
XCTAssertEqual(testString.underline(), underlineResult)
Expand Down
4 changes: 2 additions & 2 deletions Sources/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension Array {
var j: Int
for i in 0..<(count-2) {
j = Int(arc4random_uniform(UInt32(count - i)))
if i != i+j { swap(&self[i], &self[i+j]) }
if i != i+j { self.swapAt(i, i+j) }
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ extension Array where Element: Hashable {
extension Collection where Indices.Iterator.Element == Index {

/// Returns the element at the specified index if it is within bounds, otherwise nil.
public subscript (safe index: Index) -> Generator.Element? {
public subscript (safe index: Index) -> Iterator.Element? {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the difference?

return indices.contains(index) ? self[index] : nil
}
}
Expand Down
10 changes: 7 additions & 3 deletions Sources/DictionaryExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ extension Dictionary {
public func union(_ dictionaries: Dictionary...) -> Dictionary {
var result = self
dictionaries.forEach { (dictionary) -> Void in
dictionary.forEach { (key, value) -> Void in
dictionary.forEach { (arg) -> Void in

let (key, value) = arg
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary in swift 4 syntax? IMO the earlier version looked cleaner.

result[key] = value
}
}
Expand All @@ -38,9 +40,11 @@ extension Dictionary {
}

// Intersection
return filtered.filter { (key: K, value: V) -> Bool in
return filtered.filter { (arg: (key: K, value: V)) -> Bool in
// check for [key: value] in all the dictionaries
dictionaries.testAll { $0.has(key) && $0[key] == value }

let (key, value) = arg
return dictionaries.testAll { $0.has(key) && $0[key] == value }
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/FloatingPointExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension FloatingPoint {
public func rounded(toPlaces places: Int) -> Self {
guard places >= 0 else { return self }
var divisor: Self = 1
for _ in 0..<places { divisor.multiply(by: 10) }
for _ in 0..<places { divisor * 10 }
return (self * divisor).rounded() / divisor
}

Expand All @@ -27,7 +27,7 @@ extension FloatingPoint {
public func ceiled(toPlaces places: Int) -> Self {
guard places >= 0 else { return self }
var divisor: Self = 1
for _ in 0..<places { divisor.multiply(by: 10) }
for _ in 0..<places { divisor * 10 }
return (self * divisor).rounded(.up) / divisor
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/NSAttributedStringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension NSAttributedString {
guard let copy = self.mutableCopy() as? NSMutableAttributedString else { return self }

let range = (self.string as NSString).range(of: self.string)
copy.addAttributes([NSFontAttributeName: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)], range: range)
copy.addAttributes([NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)], range: range)
return copy
}

Expand All @@ -28,7 +28,7 @@ extension NSAttributedString {
guard let copy = self.mutableCopy() as? NSMutableAttributedString else { return self }

let range = (self.string as NSString).range(of: self.string)
copy.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue], range: range)
copy.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue], range: range)
return copy
}

Expand All @@ -39,7 +39,7 @@ extension NSAttributedString {
guard let copy = self.mutableCopy() as? NSMutableAttributedString else { return self }

let range = (self.string as NSString).range(of: self.string)
copy.addAttributes([NSFontAttributeName: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)], range: range)
copy.addAttributes([NSAttributedStringKey.font: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)], range: range)
return copy
}

Expand All @@ -49,7 +49,7 @@ extension NSAttributedString {

let range = (self.string as NSString).range(of: self.string)
let attributes = [
NSStrikethroughStyleAttributeName: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int)]
NSAttributedStringKey.strikethroughStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int)]
copy.addAttributes(attributes, range: range)

return copy
Expand All @@ -62,7 +62,7 @@ extension NSAttributedString {
guard let copy = self.mutableCopy() as? NSMutableAttributedString else { return self }

let range = (self.string as NSString).range(of: self.string)
copy.addAttributes([NSForegroundColorAttributeName: color], range: range)
copy.addAttributes([NSAttributedStringKey.foregroundColor: color], range: range)
return copy
}
}
Expand Down
60 changes: 30 additions & 30 deletions Sources/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension String {
public subscript(integerRange: Range<Int>) -> String {
let start = characters.index(startIndex, offsetBy: integerRange.lowerBound)
let end = characters.index(startIndex, offsetBy: integerRange.upperBound)
return self[start..<end]
return String(self[start..<end])
}

/// EZSE: Cut string from closedrange
Expand Down Expand Up @@ -257,22 +257,22 @@ extension String {
return (regex?.numberOfMatches(in: str, options: NSRegularExpression.MatchingOptions(), range: NSRange(location:0, length: str.length)) ?? -1) + 1
}

internal func rangeFromNSRange(_ nsRange: NSRange) -> Range<String.Index>? {
let from16 = utf16.startIndex.advanced(by: nsRange.location)
let to16 = from16.advanced(by: nsRange.length)
if let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self) {
return from ..< to
}
return nil
}
// internal func rangeFromNSRange(_ nsRange: NSRange) -> Range<String.Index>? {
// let from16 = utf16.startIndex + nsRange.location
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put TODO here and the next method that they will be uncommented later.

// let to16 = from16 + nsRange.length
// if let from = String.Index(from16, within: self),
// let to = String.Index(to16, within: self) {
// return from ..< to
// }
// return nil
// }

/// EZSE: Find matches of regular expression in string
public func matchesForRegexInText(_ regex: String!) -> [String] {
let regex = try? NSRegularExpression(pattern: regex, options: [])
let results = regex?.matches(in: self, options: [], range: NSRange(location: 0, length: self.length)) ?? []
return results.map { self.substring(with: self.rangeFromNSRange($0.range)!) }
}
// public func matchesForRegexInText(_ regex: String!) -> [String] {
// let regex = try? NSRegularExpression(pattern: regex, options: [])
// let results = regex?.matches(in: self, options: [], range: NSRange(location: 0, length: self.length)) ?? []
// return results.map { self.substring(with: self.rangeFromNSRange($0.range)!) }
// }

/// EZSE: Checks if String contains Email
public var isEmail: Bool {
Expand Down Expand Up @@ -368,23 +368,23 @@ extension String {

///EZSE: Returns bold NSAttributedString
public func bold() -> NSAttributedString {
let boldString = NSMutableAttributedString(string: self, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
let boldString = NSMutableAttributedString(string: self, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)])
return boldString
}

#endif

///EZSE: Returns underlined NSAttributedString
public func underline() -> NSAttributedString {
let underlineString = NSAttributedString(string: self, attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
let underlineString = NSAttributedString(string: self, attributes: [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
return underlineString
}

#if os(iOS)

///EZSE: Returns italic NSAttributedString
public func italic() -> NSAttributedString {
let italicString = NSMutableAttributedString(string: self, attributes: [NSFontAttributeName: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
let italicString = NSMutableAttributedString(string: self, attributes: [NSAttributedStringKey.font: UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)])
return italicString
}

Expand All @@ -393,24 +393,24 @@ extension String {
#if os(iOS)

///EZSE: Returns hight of rendered string
public func height(_ width: CGFloat, font: UIFont, lineBreakMode: NSLineBreakMode?) -> CGFloat {
var attrib: [String: AnyObject] = [NSFontAttributeName: font]
if lineBreakMode != nil {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = lineBreakMode!
attrib.updateValue(paragraphStyle, forKey: NSParagraphStyleAttributeName)
}
let size = CGSize(width: width, height: CGFloat(Double.greatestFiniteMagnitude))
return ceil((self as NSString).boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes:attrib, context: nil).height)
}
// public func height(_ width: CGFloat, font: UIFont, lineBreakMode: NSLineBreakMode?) -> CGFloat {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put TODO here to uncomment it later on.

// var attrib: [String: AnyObject] = [NSAttributedStringKey.font.rawValue: font]
// if lineBreakMode != nil {
// let paragraphStyle = NSMutableParagraphStyle()
// paragraphStyle.lineBreakMode = lineBreakMode!
// attrib.updateValue(paragraphStyle, forKey: NSAttributedStringKey.paragraphStyle.rawValue)
// }
// let size = CGSize(width: width, height: CGFloat(Double.greatestFiniteMagnitude))
// return ceil((self as NSString).boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes:attrib, context: nil).height)
// }

#endif

#if os(iOS) || os(tvOS)

///EZSE: Returns NSAttributedString
public func color(_ color: UIColor) -> NSAttributedString {
let colorString = NSMutableAttributedString(string: self, attributes: [NSForegroundColorAttributeName: color])
let colorString = NSMutableAttributedString(string: self, attributes: [NSAttributedStringKey.foregroundColor: color])
return colorString
}

Expand All @@ -429,7 +429,7 @@ extension String {
}
let attrText = NSMutableAttributedString(string: self)
for range in ranges {
attrText.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
attrText.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
}
return attrText
}
Expand Down
Loading