Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit b0ed585

Browse files
committed
Make new Swift rfc3339DateFormatter a static let instead of func
It didn't work in Objective-C, but in Swift it's possible to do it and it makes more sense to have it implemented as `static let` so it's initialized only once.
1 parent b0e6dcf commit b0ed585

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

WordPressKit/NSDate+RFC3339.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import Foundation
22

33
extension NSDate {
44

5-
// TODO: Make `static let` after conversion since it's not used outside WordPressKit
65
@objc
7-
static func rfc3339DateFormatter() -> DateFormatter {
8-
DateFormatter.rfc3339Formatter
9-
}
6+
static let rfc3339DateFormatter = DateFormatter.rfc3339Formatter
107

118
/// Parses a date string
129
///
@@ -20,12 +17,12 @@ extension NSDate {
2017
// Needs to be `public` because of the usages in the Objective-C code.
2118
@objc(dateWithWordPressComJSONString:)
2219
public static func with(wordPressComJSONString jsonString: String) -> Date? {
23-
self.rfc3339DateFormatter().date(from: jsonString)
20+
self.rfc3339DateFormatter.date(from: jsonString)
2421
}
2522

2623
@objc(WordPressComJSONString)
2724
public func wordPressComJSONString() -> String {
28-
NSDate.rfc3339DateFormatter().string(from: self as Date)
25+
NSDate.rfc3339DateFormatter.string(from: self as Date)
2926
}
3027
}
3128

WordPressKit/PostServiceRemoteREST+Extended.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private func decodePost(from object: AnyObject) async throws -> RemotePost {
4040

4141
private func makeParameters<T: Encodable>(from value: T) throws -> [String: AnyObject] {
4242
let encoder = JSONEncoder()
43-
encoder.dateEncodingStrategy = .formatted(NSDate.rfc3339DateFormatter())
43+
encoder.dateEncodingStrategy = .formatted(NSDate.rfc3339DateFormatter)
4444
let data = try encoder.encode(value)
4545
let object = try JSONSerialization.jsonObject(with: data)
4646
guard let dictionary = object as? [String: AnyObject] else {

WordPressKitTests/NSDate+RFC3339Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010
class NSDateRFC3339Tests: XCTestCase {
1111

1212
func testDateFormatterConfiguration() throws {
13-
let rfc3339Formatter = try XCTUnwrap(NSDate.rfc3339DateFormatter())
13+
let rfc3339Formatter = try XCTUnwrap(NSDate.rfc3339DateFormatter)
1414

1515
XCTAssertEqual(rfc3339Formatter.timeZone, TimeZone(secondsFromGMT: 0))
1616
XCTAssertEqual(rfc3339Formatter.locale, Locale(identifier: "en_US_POSIX"))

0 commit comments

Comments
 (0)