Skip to content

Instantly share code, notes, and snippets.

@yuchuanfeng
Last active December 26, 2017 03:25
Show Gist options
  • Save yuchuanfeng/7851ec62d62a6fc0e02af0c9b76968c0 to your computer and use it in GitHub Desktop.
Save yuchuanfeng/7851ec62d62a6fc0e02af0c9b76968c0 to your computer and use it in GitHub Desktop.
extension Character {
func isHanzi() -> Bool {
if ("\u{4E00}" <= self && self <= "\u{9FA5}") {
return true
}else {
return false
}
}
}
extension String {
func characterCount() -> Int{ // 字符数:一个汉字 为 两个字符
if count == 0 {
return 0
}
var cCount = 0
for (_, c) in enumerated() {
if c.isHanzi() {
cCount += 2
}else {
cCount += 1
}
}
return cCount
}
// 根据字符数 裁剪
func subString(count: Int)-> String {
var remainCount = count
var cCount = 0
for (_, c) in enumerated(){
if c.isHanzi(){
remainCount -= 2
}else {
remainCount -= 1
}
if remainCount >= 0 {
cCount += 1
}else {
break
}
}
return String(prefix(cCount))
}
}
extension Dictionary{
func getJSONString() -> String {
guard let data = getJSONData() else { return "" }
guard let jsonString = String.init(data: data, encoding: .utf8) else { return ""}
return jsonString
}
func getJSONData() -> Data? {
if (!JSONSerialization.isValidJSONObject(self)) {
print("无法解析出JSONString")
return nil
}
let data = try? JSONSerialization.data(withJSONObject: self, options: []) as Data
return data
}
}
extension Array{
func getJSONData() -> Data? {
if (!JSONSerialization.isValidJSONObject(self)) {
print("无法解析出JSONString")
return nil
}
let data = try? JSONSerialization.data(withJSONObject: self, options: []) as Data
return data
}
func getJSONString() -> String {
guard let data = getJSONData() else { return ""}
guard let jsonString = String.init(data: data, encoding: .utf8) else { return ""}
return jsonString
}
}
extension Int{
var yuan: String { // 将 分 转换 为 元
if self % 100 == 0 {
return String(self / 100)
}
if self % 10 == 0 {
return String(self / 100) + "." + String(self/10 % 10)
}
return String(self / 100) + "." + String(self/10 % 10) + String(self % 10)
}
}
extension UIViewController{
// 以下判断 只在 viewWillAppear 中有效
func isSwipeBack() -> Bool { // 滑动返回
guard let itemCount = navigationController?.navigationBar.items?.count else { return false }
guard let vcCount = navigationController?.viewControllers.count else { return false }
return vcCount < itemCount && navigationController?.navigationBar.items?.last == navigationItem
}
func isPopBack() -> Bool { // pop返回
guard let itemCount = navigationController?.navigationBar.items?.count else { return false }
guard let vcCount = navigationController?.viewControllers.count else { return false }
return vcCount == itemCount && navigationController?.navigationBar.items?.last == navigationItem
}
func isPushin() -> Bool { // push
guard let itemCount = navigationController?.navigationBar.items?.count else { return false }
guard let vcCount = navigationController?.viewControllers.count else { return false }
return vcCount > itemCount && navigationController?.navigationBar.items?.last != navigationItem
}
/**************************/
}
extension UITableView {
func scrollToBottom(_ animated: Bool) {
let roews = self.numberOfRows(inSection: 0)
if roews == 0{
return
}
self.scrollToRow(at: IndexPath.init(row: roews-1, section: 0), at: .bottom, animated: animated)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment