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

feat: migrate to swift 5.0 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions GB2260/GB2260.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 9CE0DA281CC3E65D001E7B62;
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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";
Expand Down
76 changes: 38 additions & 38 deletions GB2260/GB2260/Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
"<GB/T 2260-\(revision)>",
code,
description
].joinWithSeparator(" ")
}
public var debugDescription: String {
return [
"<GB/T 2260-\(revision)>",
code,
description
].joined(separator: " ")
}
}
214 changes: 107 additions & 107 deletions GB2260/GB2260/GB2260.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
2 changes: 1 addition & 1 deletion GB2260/GB2260/Revision.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading