### Convert Chinese String to Pinyin (Swift) Source: https://github.com/yangentao/swiftpinyin/blob/master/tests.txt This example shows how to convert an entire Chinese string to its Pinyin representation using the `PinYin.findAll` method. The method returns an array of Pinyin strings, one for each character in the input. Dependencies: UIKit, SwiftPinYin. ```swift import UIKit import SwiftPinYin class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() tick { let s = PinYin.findAll(s: "杨恩涛") print(s) } } // ... other methods } func tick(_ block: () -> Void) { let start = Date().timeIntervalSince1970 block() let end = Date().timeIntervalSince1970 let delta = end - start print("Tick: ", delta) } ``` -------------------------------- ### Convert Set of Characters to Pinyin (Swift) Source: https://github.com/yangentao/swiftpinyin/blob/master/tests.txt This snippet illustrates converting a set of characters to their Pinyin equivalents using `PinYin.findAll` with a `Set`. It utilizes a closure for formatting the output Pinyin. Dependencies: UIKit, SwiftPinYin. ```swift import UIKit import SwiftPinYin class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() tick { let s = PinYin.findAll(set: Set("杨恩涛2c")) { String($0) } print(s) } } // ... other methods } func tick(_ block: () -> Void) { let start = Date().timeIntervalSince1970 block() let end = Date().timeIntervalSince1970 let delta = end - start print("Tick: ", delta) } ``` -------------------------------- ### Install SwiftPinYin via CocoaPods Source: https://github.com/yangentao/swiftpinyin/blob/master/README.md Provides the dependency declaration for integrating the SwiftPinYin library into an iOS or macOS project using the CocoaPods package manager. ```ruby pod 'SwiftPinYin' ``` -------------------------------- ### Convert Single Chinese Character to Pinyin (Swift) Source: https://github.com/yangentao/swiftpinyin/blob/master/tests.txt This snippet demonstrates how to convert a single Chinese character to its Pinyin using the `PinYin.findOne` method from the SwiftPinYin library. It takes a closure to format the output Pinyin string. Dependencies: UIKit, SwiftPinYin. ```swift import UIKit import SwiftPinYin class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() tick { let s = PinYin.findOne(c: "臷") { String($0) } print("臷:[" + s + "]") } } // ... other methods } func tick(_ block: () -> Void) { let start = Date().timeIntervalSince1970 block() let end = Date().timeIntervalSince1970 let delta = end - start print("Tick: ", delta) } ``` -------------------------------- ### Convert Chinese characters to Pinyin using SwiftPinYin Source: https://github.com/yangentao/swiftpinyin/blob/master/README.md Demonstrates how to use the SwiftPinYin library to look up Pinyin for a single character, extract Pinyin from a string, or process a set of characters. The library handles character mappings and returns Pinyin strings including tone indices. ```swift import SwiftPinYin let s = PinYin.findOne(c: "臷") { String($0) } print("臷:[" + s + "]") let s = PinYin.findAll(s: "杨恩涛1a") print(s) let s = PinYin.findAll(set: Set("杨恩涛2c")) { String($0) } print(s) ``` -------------------------------- ### Preload SwiftPinYin Data Cache Source: https://context7.com/yangentao/swiftpinyin/llms.txt Explicitly loads the PinYin database into memory to control loading times. The library automatically loads data on first use, but preloading can be used to ensure faster lookups during critical operations like app startup or batch processing. ```swift import SwiftPinYin // Preload at application startup func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Load PinYin data during app initialization PinYin.preLoad() // Now all subsequent lookups will be fast return true } // Or preload before intensive processing func processBatchOfNames() { // Ensure data is loaded before processing PinYin.preLoad() let names = ["张三", "李四", "王五", "赵六"] for name in names { let pinyin = PinYin.findAll(s: name) print("\(name): \(pinyin)") } // All lookups complete in microseconds } // Explicit preload with timing let startTime = Date() PinYin.preLoad() let loadTime = Date().timeIntervalSince(startTime) print("Data loaded in \(loadTime) seconds") // Output: "Data loaded in 0.15 seconds" (first time only) // Subsequent calls to preLoad() do nothing PinYin.preLoad() // Returns immediately, data already loaded PinYin.preLoad() // Returns immediately, data already loaded ``` -------------------------------- ### Find Pinyin for Character Set (Swift) Source: https://context7.com/yangentao/swiftpinyin/llms.txt Processes a set of characters and returns a dictionary with PinYin mappings. This is efficient for handling unique characters from multiple sources or when avoiding duplicate lookups. Supports fallback handlers for non-Chinese characters. ```swift import SwiftPinYin // Using a character set directly let chars = Set(["你", "好", "世", "界"]) let result = PinYin.findAll(set: chars) print(result) // Output: ["你": "ni3", "好": "hao3 hao4", "世": "shi4", "界": "jie4"] // With fallback handler for non-Chinese characters let mixedChars = Set("杨恩涛2c") let resultWithFallback = PinYin.findAll(set: mixedChars) { char in return String(char) } print(resultWithFallback) // Output: ["恩": "en1", "涛": "tao1", "c": "c", "杨": "yang2", "2": "2"] // Processing unique characters from multiple strings let text1 = "北京" let text2 = "南京" let uniqueChars = Set(text1 + text2) let uniquePinyin = PinYin.findAll(set: uniqueChars) for (char, pinyin) in uniquePinyin { print("(char) -> (pinyin)") } // Output: // 北 -> bei3 // 京 -> jing1 // 南 -> nan2 ``` -------------------------------- ### Find Pinyin for Multiple Characters (String) (Swift) Source: https://context7.com/yangentao/swiftpinyin/llms.txt Processes all unique characters in a string and returns a dictionary mapping each character to its Pinyin representation. Non-Chinese characters are excluded unless a fallback handler is provided. Useful for converting sentences or names. ```swift import SwiftPinYin // Convert string to PinYin dictionary let text = "杨恩涛1a" let result = PinYin.findAll(s: text) print(result) // Output: ["恩": "en1", "涛": "tao1", "杨": "yang2"] // Note: Numbers and English letters are excluded // Full name processing let name = "李明华" let namePinyin = PinYin.findAll(s: name) for (char, pinyin) in namePinyin.sorted(by: { $0.key < $1.key }) { print("(char): (pinyin)") } // Output: // 华: hua2 hua4 hua1 // 李: li3 // 明: ming2 // Processing mixed content let mixed = "今天天气很好123" let mixedResult = PinYin.findAll(s: mixed) print(mixedResult.count) // Output: 5 (only Chinese characters counted) ``` -------------------------------- ### Find Pinyin for a Single Chinese Character (Swift) Source: https://context7.com/yangentao/swiftpinyin/llms.txt Retrieves the Pinyin representation for a single Chinese character. Supports multiple pronunciations and customizable fallback behavior for characters not found in the database. Returns an optional string. ```swift import SwiftPinYin // Basic lookup - returns optional string let pinyin = PinYin.findOne(c: "臷") print(pinyin ?? "Not found") // Output: "zhi2 die2" // Lookup with fallback handler let pinyinWithFallback = PinYin.findOne(c: "臷") { char in return String(char) } print("臷: [(pinyinWithFallback)]") // Output: "臷: [zhi2 die2]" // Character not in database let unknown = PinYin.findOne(c: "A") { char in return String(char).uppercased() } print(unknown) // Output: "A" // Simple character lookup let simple = PinYin.findOne(c: "你") print(simple ?? "nil") // Output: "ni3" ``` -------------------------------- ### Find Pinyin for Character Array (Swift) Source: https://context7.com/yangentao/swiftpinyin/llms.txt Converts an array of characters to a PinYin dictionary. Internally, it converts the array to a set, automatically deduplicating characters. This method is useful for processing sequences of characters, such as those obtained from filtering strings. ```swift import SwiftPinYin // Array of characters let charArray: [Character] = ["中", "国", "人", "民", "中"] let result = PinYin.findAll(arr: charArray) print(result) // Output: ["中": "zhong1 zhong4", "国": "guo2", "人": "ren2", "民": "min2"] // Note: Duplicate "中" is only looked up once // Processing characters from iteration var chars: [Character] = [] for char in "春夏秋冬" { chars.append(char) } let seasons = PinYin.findAll(arr: chars) print(seasons.count) // Output: 4 // Extract characters from filtered source let sentence = "2023年是好年" let chineseOnly = sentence.filter { return "\u{4E00}" <= char && char <= "\u{9FFF}" } let filtered = PinYin.findAll(arr: Array(chineseOnly)) print(filtered) // Output: ["年": "nian2", "是": "shi4", "好": "hao3 hao4"] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.