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

Feature/swift package support (#1) #9

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
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion HanziPinyin/HanziPinyin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ internal struct HanziPinyin {

internal static let sharedInstance = HanziPinyin()
internal fileprivate(set) var unicodeToPinyinTable = [String: String]()
internal fileprivate(set) var duoyinziTable = [String: String]()

init() {
unicodeToPinyinTable = initializeResource()
unicodeToPinyinTable = initPinyinResource()
duoyinziTable = initDuoyinziResource()
}

internal static func pinyinArray(withCharCodePoint charCodePoint: UInt32, outputFormat: PinyinOutputFormat = .default) -> [String] {
Expand Down
17 changes: 13 additions & 4 deletions HanziPinyin/HanziPinyinResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

private struct CacheKeys {
static let unicodeToPinyin = "HanziPinyin.UnicodeToPinyin"
static let duoyinzi = "HanziPinyin.Duoyinzi"
}

internal extension HanziPinyin {
Expand All @@ -19,13 +20,21 @@ internal extension HanziPinyin {
}
return Bundle(url: bundleURL)
}

func initPinyinResource() -> [String: String] {
initializeResource("unicode_to_hanyu_pinyin", resourceCacheKey: CacheKeys.unicodeToPinyin)
}

func initDuoyinziResource() -> [String: String] {
initializeResource("unicode_duoyinzi_pinyin", resourceCacheKey: CacheKeys.duoyinzi)
}

func initializeResource() -> [String: String] {
if let cachedPinyinTable = HanziPinyin.cachedObject(forKey: CacheKeys.unicodeToPinyin) as? [String: String] {
func initializeResource(_ fileName: String ,resourceCacheKey: String) -> [String: String] {
if let cachedPinyinTable = HanziPinyin.cachedObject(forKey: resourceCacheKey) as? [String: String] {
return cachedPinyinTable
} else {
let resourceBundle = podResourceBundle ?? Bundle(for: WhateverClass.self)
guard let resourcePath = resourceBundle.path(forResource: "unicode_to_hanyu_pinyin", ofType: "txt") else {
guard let resourcePath = resourceBundle.path(forResource: fileName, ofType: "txt") else {
return [:]
}

Expand All @@ -42,7 +51,7 @@ internal extension HanziPinyin {
pinyinTable.updateValue(components[1], forKey: components[0])
}

HanziPinyin.cache(pinyinTable, forKey: CacheKeys.unicodeToPinyin)
HanziPinyin.cache(pinyinTable, forKey: resourceCacheKey)
return pinyinTable
} catch _ {
return [:]
Expand Down
Loading