This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// UseUITextFieldDelegateToValidate | |
// | |
// Created by Frank.Chen on 2017/1/15. | |
// Copyright © 2017年 Frank.Chen. All rights reserved. | |
// | |
import UIKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor.white | |
// 身份證號 | |
let idLbl: UILabel = UILabel() | |
idLbl.frame = CGRect(x: 0, y: 40, width: self.view.frame.size.width / 3, height: 50) | |
idLbl.font = UIFont.systemFont(ofSize: 20) | |
idLbl.textColor = UIColor.black |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - 字串類別延伸 | |
extension String { | |
// 是否含有注音 | |
func isContainsPhoneticCharacters() -> Bool { | |
for scalar in self.unicodeScalars { | |
if (scalar.value >= 12549 && scalar.value <= 12582) || (scalar.value == 12584 || scalar.value == 12585 || scalar.value == 19968) { | |
return true | |
} | |
} | |
return false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Delegate | |
// --------------------------------------------------------------------- | |
// 關閉瑩幕小鍵盤 | |
func dismissKeyboard() { | |
self.view.endEditing(true) | |
} | |
// 當按下右下角的return鍵時觸發 | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
textField.resignFirstResponder() // 關閉鍵盤 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// onChange事件 | |
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
print("textField.text: \(textField.text!)") | |
print("range: \(range.location)") | |
print("string: \(string)") | |
print("") | |
// 不能輸入空白 | |
if string.isContainsSpaceCharacters() { | |
return false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 建立 serial queue,預設即是 serial queue | |
let serialQueue = DispatchQueue(label: "serialQueue") | |
// 建立非同步的 block,非同步狀態下,才能測試 serial 和 concurrent 的差異 | |
serialQueue.async { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 建立Concurrent Queues,並建立同步和非同步的block | |
let concurrentQueue: DispatchQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent) | |
serialQueue.async { | |
// 非同步化 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 非同步佇列的使用方式與驗證非同步執行的結果 | |
let concurrentQueue1: DispatchQueue = DispatchQueue(label: "concurrentQueue1", attributes: .concurrent) | |
let concurrentQueue2: DispatchQueue = DispatchQueue(label: "concurrentQueue2", attributes: .concurrent) | |
let concurrentQueue3: DispatchQueue = DispatchQueue(label: "concurrentQueue3", attributes: .concurrent) | |
concurrentQueue1.async { | |
for i in 1 ... 10 { | |
print("i: \(i)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DispatchQueue.main.async { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let group: DispatchGroup = DispatchGroup() | |
let queue1 = DispatchQueue(label: "queue1", attributes: .concurrent) | |
queue1.async(group: group) { | |
// 事件A | |
for i in 1 ... 100 { | |
print("i: \(i)") | |
} | |
} | |
let queue2 = DispatchQueue(label: "queue2", attributes: .concurrent) |