Swift 部分干货总结

DDLog

/// Debug模式日志打印
/// - Parameters:
///   - message: 内容
///   - file: 文件名
///   - function: 方法名
///   - line: 行号
public func DDLog(_ message: Any?..., file: String = #file, function: String = #function, line: Int = #line){
    #if DEBUG
    let params = message.compactMap{ "\($0)" }.joined(separator: ", ");
    
    let fmt = DateFormatter.format("yyyy-MM-dd HH:mm:ss.SSSSSSZ");
    fmt.locale = Locale(identifier: "zh_CN")
    
    let dateStr = fmt.string(from: Date())
    print(dateStr, "\((file as NSString).lastPathComponent).\(function)[line \(line)]: \(params)")
    #endif
}

UITableViewCell 复用方法

@objc public extension UITableViewCell{
    /// [源]自定义 UITableViewCell 获取方法(兼容OC)
    static func dequeueReusableCell(_ tableView: UITableView, identifier: String, style: UITableViewCell.CellStyle = .default) -> Self {
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier);
        if cell == nil {
            cell = self.init(style: style, reuseIdentifier: identifier);
        }

        cell!.selectionStyle = .none
        cell!.separatorInset = .zero
        cell!.layoutMargins = .zero
        cell!.backgroundColor = .white
        return cell as! Self;
    }
    
    /// [OC简洁方法]自定义 UITableViewCell 获取方法
    static func dequeueReusableCell(_ tableView: UITableView) -> Self {
        return dequeueReusableCell(tableView, identifier: String(describing: self), style: .default)
    }
}

@objc public extension UITableViewHeaderFooterView{
    /// [源]自定义 UITableViewHeaderFooterView 获取方法(兼容OC)
    static func dequeueReusableHeaderFooterView(_ tableView: UITableView, identifier: String) -> Self {
        var view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier)
        if view == nil {
            view = self.init(reuseIdentifier: identifier)
        }
        return view as! Self;
    }
    
    /// [OC简洁方法]自定义 UITableViewHeaderFooterView 获取方法
    static func dequeueReusableHeaderFooterView(_ tableView: UITableView) -> Self {
        return dequeueReusableHeaderFooterView(tableView, identifier: String(describing: self))
    }
}

UICollectionViewCell 复用方法

@objc public extension UICollectionViewCell{
    
    /// [源]自定义 UICollectionViewCell 获取方法(兼容OC)
    static func dequeueReusableCell(_ collectionView: UICollectionView, identifier: String, indexPath: IndexPath) -> Self {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
        cell.backgroundColor = .white
        return cell as! Self
    }

    /// [OC简洁方法]自定义 UITableViewCell 获取方法
    static func dequeueReusableCell(_ collectionView: UICollectionView, indexPath: IndexPath) -> Self {
        return dequeueReusableCell(collectionView, identifier: String(describing: self), indexPath: indexPath)
    }
}

DateFormatter: 日期,字符串,时间戳的相互转化

@objc public extension DateFormatter{
    /// 获取DateFormatter(默认格式)
    static func format(_ formatStr: String = kDateFormat) -> DateFormatter {
        let dic = Thread.current.threadDictionary;
        if let formatter = dic.object(forKey: formatStr) as? DateFormatter {
            return formatter
        }
        
        let fmt = DateFormatter();
        fmt.dateFormat = formatStr;
        fmt.locale = .current;
        fmt.locale = Locale(identifier: "zh_CN");
        fmt.timeZone = formatStr.contains("GMT") ? TimeZone(identifier: "GMT") : TimeZone.current;
        dic.setObject(fmt, forKey: formatStr as NSCopying)
        return fmt;
    }
    
    /// Date -> String
    static func stringFromDate(_ date: Date, fmt: String = kDateFormat) -> String {
        let formatter = DateFormatter.format(fmt);
        return formatter.string(from: date);
    }
    
    /// String -> Date
    static func dateFromString(_ dateStr: String, fmt: String = kDateFormat) -> Date? {
        let formatter = DateFormatter.format(fmt);
        let tmp = dateStr.count <= fmt.count ? dateStr : (dateStr as NSString).substring(to: fmt.count)
        let result = formatter.date(from: tmp);
        return result
    }
    
    /// 时间戳字符串 -> 日期字符串
    static func stringFromInterval(_ interval: String, fmt: String = kDateFormat) -> String {
        let date = Date(timeIntervalSince1970: interval.doubleValue)
        return DateFormatter.stringFromDate(date, fmt: fmt);
    }

    /// 日期字符串 -> 时间戳字符串
    static func intervalFromDateStr(_ dateStr: String, fmt: String = kDateFormat) -> String {
        guard let date = DateFormatter.dateFromString(dateStr, fmt: fmt) else {
            return "0" }
        return "\(date.timeIntervalSince1970)";
    }
}

Data

import UIKit

@objc public extension NSData{
    /// NSData -> 转数组/字典
    var objValue: Any? {
        if JSONSerialization.isValidJSONObject(self) {
           return nil;
        }
        
        do {
            let obj: Any = try JSONSerialization.jsonObject(with: self as Data, options: [])
            return obj;
        } catch {
           print(error)
        }
        return nil;
    }
    /// NSData -> 转字符串
    var stringValue: String {
        if let result = String(data: self as Data, encoding: .utf8) {
            return result
        }
        return ""
    }
    
    ///文件大小
    var fileSize: String {
        let length: CGFloat = CGFloat(self.length)
        
        if length < 1024.0 {
            return String(format: "%.2fB", length*1.0)
        }
        
        if length >= 1024.0 && length < (1024.0*1024.0) {
            return String(format: "%.2fKB", length/1024.0)
        }
                
        if length >= (1024.0*1024.0) && length < (1024.0*1024.0*1024.0) {
            return String(format: "%.2fMB", length/(1024.0*1024.0))
        }
        
        return String(format: "%.2fGB", length/(1024.0*1024.0*1024.0))
    }
}

public extension Data{
    /// 转数组/字典
    var objValue: Any? {
        return (self as NSData).objValue;
    }
    /// NSData -> 转字符串
    var stringValue: String? {
        return (self as NSData).stringValue;
    }
    
    /// NSData -> 转字符串
    var fileSize: String {
        return (self as NSData).fileSize;
    }
}

Array

public extension Array{
    
    /// ->Data
    var jsonData: Data? {
        return (self as NSArray).jsonData;
    }
    
    /// ->String
    var jsonString: String {
        return (self as NSArray).jsonString;
    }
    ///同 subarrayWithRange:
    func subarray(_ range: NSRange) -> Array {
        return self.subarray(range.location, range.length)
    }
    ///同 subarrayWithRange:
    func subarray(_ loc: Int, _ len: Int) -> Array {
        assert((loc + len) <= self.count);
        return Array(self[loc...len]);
    }
    
}

@objc public extension NSArray{

    /// ->Data
    var jsonData: Data? {
        var data: Data?
        do {
            data = try JSONSerialization.data(withJSONObject: self, options: []);
        } catch {
            print(error)
        }
        return data;
    }
    
    /// ->NSString
    var jsonString: String {
        guard let jsonData = self.jsonData as Data?,
        let jsonString = String(data: jsonData, encoding: .utf8) as String?
        else { return "" }
        return jsonString
    }
}

Dictionary

import UIKit
public extension Dictionary{
    
    /// ->Data
    var jsonData: Data? {
        return (self as NSDictionary).jsonData;
    }
    /// ->NSString
    var jsonString: String {
        return (self as NSDictionary).jsonString;
    }
    var plistData: Data?{
        return (self as NSDictionary).plistData;
    }
}

public extension Dictionary where Key == String, Value == String {
    /// 键值翻转
    var reversed: [String : String] {
        var dic = [String : String]()
        for (key, value) in self {
            dic[value] = key
        }
        return dic;
    }
    ///根据键数值排序
    func valuesByKeySorted() -> [String] {
        let values = keys.sorted {
//            return $0.intValue < $1.intValue
            return $0.compare($1) == .orderedAscending

        }.map { self[$0] ?? ""}
        return values
    }
}

@objc public extension NSDictionary{

    /// ->Data
    var jsonData: Data? {
        var data: Data?
        do {
            data = try JSONSerialization.data(withJSONObject: self, options: []);
        } catch {
            print(error)
        }
        return data;
    }
    
    /// ->NSString
    var jsonString: String {
        guard let jsonData = self.jsonData as Data?,
        let jsonString = String(data: jsonData, encoding: .utf8) as String?
        else { return "" }
        return jsonString
    }
    
    var plistData: Data?{
        let result = try? PropertyListSerialization.data(fromPropertyList: self, format: .binary, options: 0)
        return result
    }
}

URL

import UIKit

public extension URLComponents {
    
    ///查询参数
    var queryParameters: [String: String]? {
        guard let queryItems = queryItems else { return nil }

        var dic = [String: String]()
        for item in queryItems {
            dic[item.name] = item.value
        }
        return dic
    }
    ///追加查询参数
    mutating func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        queryItems = (queryItems ?? []) + parameters
            .map { URLQueryItem(name: $0, value: $1) }
        return url!
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return queryItems?
            .first(where: { $0.name == key })?
            .value
    }
}


public extension URL {
    
    ///查询参数
    var queryParameters: [String: String]? {
        guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false)
        else { return nil }
        return components.queryParameters
    }
    ///追加查询参数
    func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)!
        return components.appendingQueryParameters(parameters)
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return URLComponents(string: absoluteString)?.queryValue(for: key)
    }
}

@objc public extension NSURLComponents {
    
    ///查询参数
    var queryParameters: [String: String]? {
        return (self as URLComponents).queryParameters
    }
    ///追加查询参数
    func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        queryItems = (queryItems ?? []) + parameters
            .map { URLQueryItem(name: $0, value: $1) }
        return url!
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return (self as URLComponents).queryValue(for: key)
    }
}

@objc public extension NSURL {
    
    ///查询参数
    var queryParameters: [String: String]? {
        return (self as URL).queryParameters
    }
    ///追加查询参数
    func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        return (self as URL).appendingQueryParameters(parameters)
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return (self as URL).queryValue(for: key)
    }
}

UINavigationController

public extension UINavigationController{
    ///泛型方法: push到特定控制器页面
    final func pushVC<T: UIViewController>(_ type: T.Type, animated: Bool = true, block: ((T) -> Void)? = nil) {
        let controller = type.init()
//        controller.hidesBottomBarWhenPushed = true
        block?(controller)
        pushViewController(controller, animated: animated);
    }
    ///泛型方法: pop到特定控制器页面
    final func popToVC<T: UIViewController>(_ type: T.Type, animated: Bool = true) {
        for e in viewControllers {
            if e.isKind(of: type) {
                popToViewController(e, animated: animated)
                return
            }
        }
        popViewController(animated: animated)
    }
    ///泛型方法: 延迟退出
    func popVC(animated: Bool, delay: Double) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime(floatLiteral: delay)) {
            self.popViewController(animated: animated)
        }
    }
}

UISegmentedControl

@objc public extension UISegmentedControl{
    private struct AssociateKeys {
        static var items   = "UISegmentedControl" + "items"
    }

    /// 控件items
    var items: [String] {
        get {
            if let obj = objc_getAssociatedObject(self, &AssociateKeys.items) as? [String] {
                return obj
            }
            return []
        }
        set {
            objc_setAssociatedObject(self, &AssociateKeys.items, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
            updateItems(newValue)
        }
    }
    
    /// 配置新item数组
    private func updateItems(_ items: [String]) {
        if items.count == 0 {
            return
        }
        
        removeAllSegments()
        for e in items.enumerated() {
            insertSegment(withTitle: e.element, at: e.offset, animated: false)
        }
        selectedSegmentIndex = 0
    }
}

present

@objc public extension UIViewController{
    /// 呈现
    func present(_ animated: Bool = true, completion: (() -> Void)? = nil) {
        guard let keyWindow = UIApplication.shared.keyWindow,
              let rootVC = keyWindow.rootViewController
              else { return }
        self.modalPresentationStyle = .fullScreen

        DispatchQueue.main.async {
            if let alertVC = self as? UIAlertController {
                if alertVC.preferredStyle == .alert {
                    if alertVC.actions.count == 0 {
                        rootVC.present(alertVC, animated: animated, completion: {
                            DispatchQueue.main.after(TimeInterval(kDurationToast), execute: {
                                alertVC.dismiss(animated: animated, completion: completion)
                            })
                        })
                    } else {
                        rootVC.present(alertVC, animated: animated, completion: completion)
                    }
                } else {
                    //防止 ipad 下 sheet 会崩溃的问题
                    if UIDevice.current.userInterfaceIdiom == .pad {
                        if let popoverPresentationController = alertVC.popoverPresentationController {
                            popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
                            popoverPresentationController.sourceView = keyWindow;
                            
                            let isEmpty = popoverPresentationController.sourceRect.equalTo(.null) || popoverPresentationController.sourceRect.equalTo(.zero)
                            if isEmpty {
                                popoverPresentationController.sourceRect = CGRect(x: keyWindow.bounds.midX, y: 64, width: 1, height: 1);
                            }
                        }
                    }
                    rootVC.present(alertVC, animated: animated, completion: completion)
                }
            } else {
                rootVC.present(self, animated: animated, completion: completion)
            }
        }
    }
}

备注:
let list = [1,2,4,3,9,8];
let b = list[2...5];// ArraySlice
ArraySlice不能长期保存,可能会有内存泄漏,附上官方api的截图
Important

Long-term storage of ArraySlice instances is discouraged. A slice holds a reference to the entire storage of a larger array, not just to the portion it presents, even after the original array’s lifetime ends. Long-term storage of a slice may therefore prolong the lifetime of elements that are no longer otherwise accessible, which can appear to be memory and object leakage.

警告:不鼓励/建议长时间的存储ArraySlice实例
由于一个ArraySlice实例呈现的是某个比较大的数组上的一个视图。如果这个原始数组已经结束了其生命周期,存储这个切片实例可能会延长那些不能再被访问的数组元素的存活时间,这就造成了明显的内存和对象泄露。为了防止这个影响的发生,只在临时性的计算时,使用ArraySlice。

//MARK:字典排序
func sortDictiony() -> Void {
    //字典排序
    /*
    字典中$0 来表示闭包的第一个参数,$1 来表示第二个,以此类推,in 也可以省略
    元组 **(key: String, value: String) 第0个是 key,第一个是 value **
    dict.sorted { (<#(key: String, value: String)#>, <#(key: String, value: String)#>) -> Bool in
         <#code#>
    }
    */
        
    let dict = ["27":"w","15":"t","36":"b"]
    let dicSortedByKey = dict.sorted(by: {$0.0 < $1.0})
    let dicSortedByValue = dict.sorted(by: {$0.1 < $1.1})
    print(dicSortedByKey)
    print(dicSortedByValue)
        
    let dicSortedByKeyNew = dict.sorted(by:<)
    let dicSortedByValueNew = dict.sorted(by:>)
    print(dicSortedByKeyNew)
    print(dicSortedByValueNew)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342