From 97403a4d1e54046e0f308d9ce809da4d808e8c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E6=8C=AF=E5=8D=8E?= Date: Tue, 2 Aug 2022 20:42:07 +0800 Subject: [PATCH] feat: migrate to swift 5.0 --- GB2260/GB2260.xcodeproj/project.pbxproj | 3 + GB2260/GB2260/Division.swift | 76 +++---- GB2260/GB2260/GB2260.swift | 214 +++++++++--------- GB2260/GB2260/Revision.swift | 2 +- GB2260/GB2260Tests/GB2260Tests.swift | 16 +- .../GB2260Example.xcodeproj/project.pbxproj | 3 + 6 files changed, 160 insertions(+), 154 deletions(-) diff --git a/GB2260/GB2260.xcodeproj/project.pbxproj b/GB2260/GB2260.xcodeproj/project.pbxproj index 2a895bd..5889a2e 100644 --- a/GB2260/GB2260.xcodeproj/project.pbxproj +++ b/GB2260/GB2260.xcodeproj/project.pbxproj @@ -171,6 +171,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, ); mainGroup = 9CE0DA281CC3E65D001E7B62; @@ -275,6 +276,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -316,6 +318,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.3; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; diff --git a/GB2260/GB2260/Division.swift b/GB2260/GB2260/Division.swift index 2fbcf25..14f73ac 100644 --- a/GB2260/GB2260/Division.swift +++ b/GB2260/GB2260/Division.swift @@ -6,67 +6,67 @@ // public struct Division { - typealias LazyEvaluation = () -> Division? - - /// The name of this `Division` - public let name: String - /// The code of this `Division` - public let code: String - /// The GB2206 revision of this `Division` - public let revision: String - /// The possible province this `Division` belongs to - public var province: Division? { - return getProvince() - } - /// The possible prefecture this `Division` belongs to - public var prefecture: Division? { - return getPrefecture() - } - - let getProvince: LazyEvaluation - let getPrefecture: LazyEvaluation + typealias LazyEvaluation = () -> Division? + + /// The name of this `Division` + public let name: String + /// The code of this `Division` + public let code: String + /// The GB2206 revision of this `Division` + public let revision: String + /// The possible province this `Division` belongs to + public var province: Division? { + return getProvince() + } + /// The possible prefecture this `Division` belongs to + public var prefecture: Division? { + return getPrefecture() + } + + let getProvince: LazyEvaluation + let getPrefecture: LazyEvaluation } extension Division: Equatable { } public func ==(lhs: Division, rhs: Division) -> Bool { - return lhs.code == rhs.code && lhs.revision == rhs.revision + return lhs.code == rhs.code && lhs.revision == rhs.revision } extension Division: Comparable { } public func <(lhs: Division, rhs: Division) -> Bool { - return lhs.code < rhs.code + return lhs.code < rhs.code } public func <=(lhs: Division, rhs: Division) -> Bool { - return lhs.code <= rhs.code + return lhs.code <= rhs.code } public func >=(lhs: Division, rhs: Division) -> Bool { - return lhs.code >= rhs.code + return lhs.code >= rhs.code } public func >(lhs: Division, rhs: Division) -> Bool { - return lhs.code > rhs.code + return lhs.code > rhs.code } extension Division: CustomStringConvertible { - public var description: String { - return [ - province?.name ?? "", - prefecture?.name ?? "", - name - ].filter { !$0.isEmpty }.joinWithSeparator(" ") - } + public var description: String { + return [ + province?.name ?? "", + prefecture?.name ?? "", + name + ].filter { !$0.isEmpty }.joined(separator: " ") + } } extension Division: CustomDebugStringConvertible { - public var debugDescription: String { - return [ - "", - code, - description - ].joinWithSeparator(" ") - } + public var debugDescription: String { + return [ + "", + code, + description + ].joined(separator: " ") + } } diff --git a/GB2260/GB2260/GB2260.swift b/GB2260/GB2260/GB2260.swift index 27b76cd..9f7f093 100644 --- a/GB2260/GB2260/GB2260.swift +++ b/GB2260/GB2260/GB2260.swift @@ -7,124 +7,124 @@ /// An implementation for looking up Chinese administrative divisions(GB/T 2260 codes). public class GB2260 { - /// The revision of current loaded GB2260 dataset. - public let revision: Revision - - private let data: [String: String] - - /** - Creates an instance that loads given revision GB2260 dataset; - it may failed to load when the specific revision dataset doesn't exist, and return `nil`. - - - Parameter revision: the specific `GB2260.Revision` - */ - public init?(revision: Revision = .V201410) { - self.revision = revision - guard let path = revision.path, - let data = (NSDictionary(contentsOfFile: path)?.objectForKey("data") as? [String: String]) - else { - return nil + /// The revision of current loaded GB2260 dataset. + public let revision: Revision + + private let data: [String: String] + + /** + Creates an instance that loads given revision GB2260 dataset; + it may failed to load when the specific revision dataset doesn't exist, and return `nil`. + + - Parameter revision: the specific `GB2260.Revision` + */ + public init?(revision: Revision = .V201410) { + self.revision = revision + guard let path = revision.path, + let data = (NSDictionary(contentsOfFile: path)?.object(forKey: "data") as? [String: String]) + else { + return nil + } + + self.data = data } - - self.data = data - } - + } private extension String { - var isProvince: Bool { - return hasSuffix("0000") - } - - var isPrefecture: Bool { - return hasSuffix("00") - } - - var provinceCode: String { - return substringToIndex(startIndex.advancedBy(2)) + "0000" - } - - var prefectureCode: String { - return substringToIndex(startIndex.advancedBy(4)) + "00" - } + var isProvince: Bool { + return hasSuffix("0000") + } + + var isPrefecture: Bool { + return hasSuffix("00") + } + + var provinceCode: String { + return String(prefix(2)) + "0000" + } + + var prefectureCode: String { + return String(prefix(4)) + "00" + } } extension GB2260 { - /** - A `List` of provinces in `Division` under current loaded GB2260 revision. - */ - public var provinces: [Division] { - return data.filter { $0.0.isProvince } - .flatMap { self[$0.0] } - .sort(<) - } - - /** - Looking up for prefectures under a given province zipcode. - - - Parameter code: a valid GB2260 zipcode. - - - Returns: a list of prefecture cities in `Division` if it has one; - returns empty list otherwise. - */ - public func prefectures(of code: String) -> [Division] { - guard let province = self[code] else { - return [] + /** + A `List` of provinces in `Division` under current loaded GB2260 revision. + */ + public var provinces: [Division] { + return data.filter { $0.0.isProvince } + .compactMap { self[$0.0] } + .sorted(by: <) } - return data.filter { - $0.0.isPrefecture && self[$0.0]?.province == province - }.flatMap { self[$0.0] }.sort(<) - } - - /** - Looking up for counties under a given prefecture code. - - - Parameter code: a valid GB2260 zipcode. - - - Returns: a list of counties in `Division` if it has one; - returns empty list otherwise. - */ - public func counties(of code: String) -> [Division] { - guard let prefecture = self[code] else { return [] } - return data.filter { - !$0.0.isProvince && - !$0.0.isPrefecture && - self[$0.0]?.prefecture == prefecture - }.flatMap { self[$0.0] }.sort(<) - } - + + /** + Looking up for prefectures under a given province zipcode. + + - Parameter code: a valid GB2260 zipcode. + + - Returns: a list of prefecture cities in `Division` if it has one; + returns empty list otherwise. + */ + public func prefectures(of code: String) -> [Division] { + guard let province = self[code] else { + return [] + } + return data.filter { + $0.0.isPrefecture && self[$0.0]?.province == province + }.compactMap { self[$0.0] }.sorted(by: <) + } + + /** + Looking up for counties under a given prefecture code. + + - Parameter code: a valid GB2260 zipcode. + + - Returns: a list of counties in `Division` if it has one; + returns empty list otherwise. + */ + public func counties(of code: String) -> [Division] { + guard let prefecture = self[code] else { return [] } + return data.filter { + !$0.0.isProvince && + !$0.0.isPrefecture && + self[$0.0]?.prefecture == prefecture + }.compactMap { self[$0.0] }.sorted(by: <) + } + } extension GB2260 { - func province(of code: String) -> Division.LazyEvaluation { - return { - return code.isProvince ? nil : self[code.provinceCode] + func province(of code: String) -> Division.LazyEvaluation { + return { + return code.isProvince ? nil : self[code.provinceCode] + } } - } - - func prefecture(of code: String) -> Division.LazyEvaluation { - return { - return code.isPrefecture ? nil : self[code.prefectureCode] + + func prefecture(of code: String) -> Division.LazyEvaluation { + return { + return code.isPrefecture ? nil : self[code.prefectureCode] + } } - } - - /// Returns the correspond division of `code` if database has one; - /// returns `nil` otherwise. - public func division(of code: String) -> Division? { - guard let name = data[code] else { return nil } - return Division(name: name, - code: code, - revision: revision.rawValue, - getProvince: province(of: code), - getPrefecture: prefecture(of: code)) - } - - /// Accesses the correspond division of `code` if database has one; - /// returns `nil` otherwise. - /// - /// - SeeAlso: `division(of:)`. - public subscript(code: String) -> Division? { - return division(of: code) - } - + + /// Returns the correspond division of `code` if database has one; + /// returns `nil` otherwise. + public func division(of code: String) -> Division? { + guard let name = data[code] else { return nil } + return Division(name: name, + code: code, + revision: revision.rawValue, + getProvince: province(of: code), + getPrefecture: prefecture(of: code)) + } + + /// Accesses the correspond division of `code` if database has one; + /// returns `nil` otherwise. + /// + /// - SeeAlso: `division(of:)`. + public subscript(code: String) -> Division? { + return division(of: code) + } + } diff --git a/GB2260/GB2260/Revision.swift b/GB2260/GB2260/Revision.swift index ac3d8fa..ab105ac 100644 --- a/GB2260/GB2260/Revision.swift +++ b/GB2260/GB2260/Revision.swift @@ -30,6 +30,6 @@ public enum Revision: String { case V200212 = "200212" var path: String? { - return NSBundle(forClass: GB2260.self).pathForResource(rawValue, ofType: "plist", inDirectory: "data") + return Bundle(for: GB2260.self).path(forResource: rawValue, ofType: "plist", inDirectory: "data") } } diff --git a/GB2260/GB2260Tests/GB2260Tests.swift b/GB2260/GB2260Tests/GB2260Tests.swift index 308bcef..72ed1c7 100644 --- a/GB2260/GB2260Tests/GB2260Tests.swift +++ b/GB2260/GB2260Tests/GB2260Tests.swift @@ -34,8 +34,8 @@ class GB2260Tests: XCTestCase { func testProvince() { let division = db["110000"] XCTAssertNotNil(division) - XCTAssertEqual(division!.name, .Some("北京市")) - XCTAssertEqual(division!.description, .Some("北京市")) + XCTAssertEqual(division!.name, "北京市") + XCTAssertEqual(division!.description, "北京市") XCTAssertEqual(division!.revision, db.revision.rawValue) XCTAssertEqual("110000", division!.code) } @@ -43,18 +43,18 @@ class GB2260Tests: XCTestCase { func testPrefecture() { let division = db["110100"] XCTAssertNotNil(division) - XCTAssertEqual(division!.province?.name, .Some("北京市")) + XCTAssertEqual(division!.province?.name, "北京市") XCTAssertEqual(division!.name, "市辖区") - XCTAssertEqual(division!.description, .Some("北京市 市辖区")) + XCTAssertEqual(division!.description, "北京市 市辖区") } func testCountry() { let division = db["110101"] XCTAssertNotNil(division) - XCTAssertEqual(division!.province?.name, .Some("北京市")) - XCTAssertEqual(division!.prefecture?.name, .Some("市辖区")) + XCTAssertEqual(division!.province?.name, "北京市") + XCTAssertEqual(division!.prefecture?.name, "市辖区") XCTAssertEqual(division!.name, "东城区") - XCTAssertEqual(division!.description, .Some("北京市 市辖区 东城区")) + XCTAssertEqual(division!.description, "北京市 市辖区 东城区") } func testFailedQuery() { @@ -87,7 +87,7 @@ class GB2260Tests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock { + self.measure { // Put the code you want to measure the time of here. } } diff --git a/GB2260Example/GB2260Example.xcodeproj/project.pbxproj b/GB2260Example/GB2260Example.xcodeproj/project.pbxproj index 1254be8..aff87fa 100644 --- a/GB2260Example/GB2260Example.xcodeproj/project.pbxproj +++ b/GB2260Example/GB2260Example.xcodeproj/project.pbxproj @@ -263,6 +263,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, Base, ); @@ -437,6 +438,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -474,6 +476,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.3; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 5.0; VALIDATE_PRODUCT = YES; }; name = Release;