Skip to content

Commit

Permalink
remove the usage of range(at
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jan 8, 2018
1 parent c12c0df commit 46ebe25
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "EVReflection"
s.version = "5.4.3"
s.version = "5.4.4"
s.summary = "Reflection based object mapping. (Dictionary, CKRecord, NSManagedObject, Realm, JSON, XML, Alamofire, Moya, RxSwift, ReactiveSwift)"

s.description = <<-EOS
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,22 @@
<Bucket
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/EVReflection.swift"
timestampString = "537088411.579632"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "956"
endingLineNumber = "956"
landmarkName = "setObjectValue(_:key:theValue:typeInObject:valid:conversionOptions:parents:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
22 changes: 9 additions & 13 deletions Source/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1576,20 +1576,16 @@ extension Date {
public init?(fromDateTimeString: String) {
let pattern = "\\\\?/Date\\((\\d+)(([+-]\\d{2})(\\d{2}))?\\)\\\\?/"
let regex = try! NSRegularExpression(pattern: pattern)
guard let match: NSTextCheckingResult = regex.firstMatch(in: fromDateTimeString, range: NSRange(location: 0, length: fromDateTimeString.utf16.count)) else {
// evPrint("Failed to find a match")
return nil
let match: NSRange = regex.rangeOfFirstMatch(in: fromDateTimeString, range: NSRange(location: 0, length: fromDateTimeString.utf16.count))
var dateString: String = ""
if match.location == NSNotFound {
dateString = fromDateTimeString
} else {
dateString = (fromDateTimeString as NSString).substring(with: match) // Extract milliseconds
}

#if swift(>=4.0)
let dateString = (fromDateTimeString as NSString).substring(with: match.range(at: 1)) // Extract milliseconds
#else
let dateString = (fromDateTimeString as NSString).substring(with: match.rangeAt(1)) // Extract milliseconds
#endif

let timeStamp = Double(dateString)! / 1000.0 // Convert to UNIX timestamp in seconds

self.init(timeIntervalSince1970: timeStamp) // Create Date from timestamp
let substrings = dateString.components(separatedBy: CharacterSet.decimalDigits.inverted)
guard let timeStamp = (substrings.flatMap { Double($0) }.first) else { return nil }
self.init(timeIntervalSince1970: timeStamp / 1000.0) // Create Date from timestamp
}
}

Expand Down
6 changes: 6 additions & 0 deletions UnitTests/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -512,5 +512,11 @@ class EVReflectionTests: XCTestCase {
XCTAssert(nx.beacons.count == 1, "Should also have an object")
}

func testDate() {
let date = Date(fromDateTimeString: "/Date(1268123281843)/")
XCTAssertNotNil(date)
let date2 = Date(fromDateTimeString: "1268123281843")
XCTAssertNotNil(date2)
}
}

0 comments on commit 46ebe25

Please sign in to comment.