Swift Debug Print

自定义 swift 控制台输出 // MARK: - Debug Print - public func print<T>(_ message: T, fileName: String = #file, methodName: String = #function, line: Int = #line) { #if DEBUG let printInfo = """ \n ================================================================================================================================================= 【File Name】: \(fileName.split("/").last ?? "") 【FunctionName】: \(methodName) 【Fuction Line】: \(line) 【Print Time】: \(Date()) 【 Message 】: \(message) ================================================================================================================================================= \n """ print(printInfo, separator: "", terminator: "") #endif }

2019-08-10 · 1 min · 62 words · Cee Yang

UITextView+Placeholder

Swift UITextView 扩展 Placeholder import UIKit fileprivate var kTextViewPlaceholderLabel : Int = 0x2019_00 fileprivate var kTextViewPlaceholder : Int = 0x2019_01 fileprivate var kTextViewPlaceholderColor : Int = 0x2019_02 fileprivate var kTextViewPlaceholderFont : Int = 0x2019_03 fileprivate var kTextViewPlaceholderKeys : Int = 0x2019_04 extension UITextView { /// 占位符 var x_placeholder: String { get { if let placeholder = objc_getAssociatedObject(self, &kTextViewPlaceholder) as? String { return placeholder } else { return "" } } set { objc_setAssociatedObject(self, &kTextViewPlaceholder, newValue, .OBJC_ASSOCIATION_RETAIN) x_placeholderLabel.text = newValue } } /// 占位符颜色 var x_placeholderColor: UIColor { get { if let placeholderColor = objc_getAssociatedObject(self, &kTextViewPlaceholderColor) as? UIColor { return placeholderColor } else { return UIColor.subTitleColor } } set { objc_setAssociatedObject(self, &kTextViewPlaceholderColor, newValue, .OBJC_ASSOCIATION_RETAIN) x_placeholderLabel.textColor = newValue } } /// 占位符字体 var x_placeholderFont: UIFont { get { if let placeholderFont = objc_getAssociatedObject(self, &kTextViewPlaceholderColor) as? UIFont { return placeholderFont } else { return UIFont.systemFont(ofSize: 12) } } set { objc_setAssociatedObject(self, &kTextViewPlaceholderColor, newValue, .OBJC_ASSOCIATION_RETAIN) x_placeholderLabel.font = newValue } } /// 占位符 标签 @IBInspectable var x_placeholderLabel: UILabel { get { var _placeholderLabel = UILabel(font: self.font ?? UIFont.systemFont(ofSize: 12), color: .subTitleColor, alignment: .left) if let label = objc_getAssociatedObject(self, &kTextViewPlaceholderLabel) as? UILabel { _placeholderLabel = label } else { objc_setAssociatedObject(self, &kTextViewPlaceholderLabel, _placeholderLabel, .OBJC_ASSOCIATION_RETAIN) } addPlaceholderLabelToSuperView(label: _placeholderLabel) return _placeholderLabel } set { objc_setAssociatedObject(self, &kTextViewPlaceholderLabel, newValue, .OBJC_ASSOCIATION_RETAIN) addPlaceholderLabelToSuperView(label: newValue) } } /// 是否需要添加占位符到父视图 fileprivate var x_placeHolderNeedAddToSuperView: Bool { get { if let isAdded = objc_getAssociatedObject(self, &kTextViewPlaceholderKeys) as? Bool { return isAdded } return true } set { objc_setAssociatedObject(self, &kTextViewPlaceholderKeys, newValue, .OBJC_ASSOCIATION_RETAIN) } } /// 添加占位符到父视图 /// /// - Parameter label: 占位符 标签 fileprivate func addPlaceholderLabelToSuperView(label: UILabel) { guard x_placeHolderNeedAddToSuperView else { return } x_placeHolderNeedAddToSuperView = false NotificationCenter.default.addObserver(self, selector: #selector(x_textChange(noti:)), name: UITextView.textDidChangeNotification, object: nil) addSubview(label) label.snp.makeConstraints { (make) in make.edges.equalToSuperview().inset(UIEdgeInsets(top: 7, left: 2, bottom: 0, right: 0)) } } /// 编辑事件 @objc fileprivate func x_textChange(noti: NSNotification) { let isEmpty = text.isEmpty print("text:\(String(describing: text))\nisEmpty:\(isEmpty)") x_placeholderLabel.text = isEmpty ? x_placeholder : "" x_placeholderLabel.isHidden = !isEmpty } }

2019-08-08 · 2 min · 316 words · Cee Yang

Swift 4.2 新特性

private 权限扩大 在 Swift 4 中,extension 可以读取 private 变量了。 Swift 3 中,如果将主体函数的变量定义为 private,则其 extension 无法读取此变量,必须将其改为 filePrivate 才可以。 单向区间 单向区间是一个新的类型,主要分两种:确定上限和确定下限的区间。直接用字面量定义大概可以写成 …6和 2… 例如 let intArr = [0, 1, 2, 3, 4] let arr1 = intArr[...3] // [0, 1, 2, 3] let arr2 = intArr[3...] // [3, 4] 字符串改动 String 操作简化了 String 许多要通过 .characters 进行的操作,可以直接用 String 进行操作了。 例如: let greeting = "Hello, 😜!" // No need to drill down to .characters let n = greeting.count let endOfSentence = greeting.index(of: "!")! 新增 Substring 类型 swift 4 为字符串片段新增了一个叫 Substring 的类型。 当你创建一个字符串的片段时,会产生一个 Substring 实例。Substring 与 String 用法相同, 因为子串和原字符串共享内存,所以对子串的操作快速而且高效。 let greeting = "Hi there! It's nice to meet you! 👋" let endOfSentence = greeting.index(of: "!")! // 产生 Substring 实例 let firstSentence = greeting[...endOfSentence] // firstSentence == "Hi there!" // `Substring` 与 `String` 用法相同 let shoutingSentence = firstSentence.uppercased() // shoutingSentence == "HI THERE!" 但是要注意一个 Substring 保留从其生成的完整的 String值。 当您传递一个看似很小的 Substring 时,这可能导致意外的高内存开销。所以使用 Substring时,最好转化为 String. ...

2017-09-11 · 2 min · 321 words · Cee Yang