• Swift小技巧(五)


    利用谓语语法和正则表达式,判断字符串是否符合特定格式
     //利用谓语语法,判断一个字符串是否是邮箱。
    func isValidEmail(testStr: String) -> Bool {
        //可以根据自己的需要改成别的正则表达式
        let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"
        let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
        return emailTest.evaluate(with: testStr)
    }
    isValidEmail(testStr: "213@12.com")//true
    
    如何判断app运行在真机还是模拟器上面
     #if (arch(i386) || arch(x86_64)) && os(iOS)
        //如果是模拟器,则会运行,真机上面则不会执行
        print("模拟器")
    #endif
        print("正常代码")
    
    OC的respondsToSelector方法
     //使用如下方式代替
    if let delegate = delegate {//判断是否存在代理
        if let theMethod = delegate.theOptionalProtocolMethod? {//判断代理是否实现方法
            theMethod()
            return
        }
    }
    
    获得某个date的年月日时分秒等
     let date = Date()
    //获得当天的日历对象
    let calendar = Calendar.current
    //获得小时
    let hour = calendar.component(.hour, from: date)
    //获得分钟
    let minutes = calendar.component(.minute, from: date)
    
    给UITextView添加placeholder占位符
     //定义了一个全局的常量
    let placeholderText = "请输入内容!"
    //1.给textview扩展如下方法:
    extension UITextView {
        func addPlaceholderText(text: String) {
            self.text = text
            self.textColor = .lightGray
        }
    }
    //2.创建出textview
            textView = UITextView(frame: view.frame)
            textView.delegate = self
            textView.addPlaceholderText(text: placeholderText)
            view.addSubview(textView)
    //3.在textview的下列代理方法中,实现如下两个方法,添加如下代码
        func textViewDidBeginEditing(_ textView: UITextView) {
            if textView.textColor == .lightGray {
                textView.text = nil
                textView.textColor = .black
            }
        }
        
        func textViewDidEndEditing(_ textView: UITextView) {
            if textView.text.isEmpty {
                textView.addPlaceholderText(text: placeholderText)
            }
        }
    

    只需要以上操作,textview就可以跟textfield一样,拥有一个placeholder占位符提示语

    格式化输出Int
     //方法一:
    let str = String(format: "%02d-%03d", 1,2)//01-002
    
    //方法二
    let str2 = String(format: "%02d-%03d", arguments: [1,2])//01-002
    
    //方法三
    let formatter = NumberFormatter()
    formatter.minimumIntegerDigits = 2
    let str3 = formatter.string(from: 1)//01
    
    搜索指定的文件夹路劲
     let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
    获得app设置中version版本号
     let verson = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
    print(version)//1.0.1
    
    设置tableview的分割线separator的颜色,宽度等
    tableView.separatorColor = .red//分割线颜色
    tableView.separatorStyle = .singleLineEtched//分割线样式
    //        tableView.separatorEffect//效果
    tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)//分割线内边距,都设置为0,则会出现一条宽等于屏幕的完整的分割线
    
    如何安全的使用array,如果下标越界等情况发生时,不会崩溃
     //给collection扩展一个下标方法
    extension Collection where Indices.Iterator.Element == Index {
        subscript (safe index: Index) -> Generator.Element? {
            return indices.contains(index) ? self[index] : nil
        }
    }
    //使用
    let arr = [1]
    arr[safe: 2]//nil,会得到一个nil,而不会崩溃
    
    iOS自带的tableview刷新控件
     //创建出刷新控件
            let refreshControl = UIRefreshControl()
            refreshControl.attributedTitle = NSAttributedString(string: "开始刷新")
            refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)
            //添加上去即可
            tableView.addSubview(refreshControl)
    
    //实现刷新函数
        func refresh(sender: AnyObject) {
            //做刷新要做的事情
            print("开始刷新")
        }
            
            //结束刷新,需要结束的时候调用
            refreshControl.endRefreshing()
    
    如何获得一个view的快照,并生成image
     extension UIView {
        func takeSnapshot() -> UIImage {
            UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
            drawHierarchy(in: self.bounds, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            return image
        }
    }
    
    快速比较,两个date之间的差距,比如相差多少年月日等
     extension Date {
        //返回相差多少年
        func years(from date: Date) -> Int {
            return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
        }
        //返回差多少月
        func months(from date: Date) -> Int {
            return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
        }
        //返回差多少周
        func weeks(from date: Date) -> Int {
            return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
        }
        //返回差多少天
        func days(from date: Date) -> Int {
            return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
        }
        //返回差多少小时
        func hours(from date: Date) -> Int {
            return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
        }
        //返回差多少分钟
        func minutes(from date: Date) -> Int {
            return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
        }
        //返回差多少秒
        func seconds(from date: Date) -> Int {
            return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
        }
        //返回差距的描述
        func offset(from date: Date) -> String {
            if years(from: date)   > 0 { return "(years(from: date))y"   }
            if months(from: date)  > 0 { return "(months(from: date))M"  }
            if weeks(from: date)   > 0 { return "(weeks(from: date))w"   }
            if days(from: date)    > 0 { return "(days(from: date))d"    }
            if hours(from: date)   > 0 { return "(hours(from: date))h"   }
            if minutes(from: date) > 0 { return "(minutes(from: date))m" }
            if seconds(from: date) > 0 { return "(seconds(from: date))s" }
            return ""
        }
    }
    //使用
    let date1 = DateComponents(calendar: .current, year: 2014, month: 11, day: 28, hour: 5, minute: 9).date!
    let date2 = DateComponents(calendar: .current, year: 2015, month: 8, day: 28, hour: 5, minute: 9).date!
    
    let years = date2.years(from: date1)     // 0
    let months = date2.months(from: date1)   // 9
    let weeks = date2.weeks(from: date1)     // 39
    let days = date2.days(from: date1)       // 273
    let hours = date2.hours(from: date1)     // 6,553
    let minutes = date2.minutes(from: date1) // 393,180
    let seconds = date2.seconds(from: date1) // 23,590,800
    
    let timeOffset = date2.offset(from: date1) // "9M"
    
    let date3 = DateComponents(calendar: .current, year: 2014, month: 11, day: 28, hour: 5, minute: 9).date!
    let date4 = DateComponents(calendar: .current, year: 2015, month: 11, day: 28, hour: 5, minute: 9).date!
    
    let timeOffset2 = date4.offset(from: date3) // "1y"
    
    let date5 = DateComponents(calendar: .current, year: 2017, month: 4, day: 28).date!
    let now = Date()
    let timeOffset3 = now.offset(from: date5) // "1w"
    
    //或者使用date components formatter
    let dateComponentsFormatter = DateComponentsFormatter()
    dateComponentsFormatter.allowedUnits = [.year,.month,.weekOfYear,.day,.hour,.minute,.second]
    dateComponentsFormatter.maximumUnitCount = 1
    dateComponentsFormatter.unitsStyle = .full
    dateComponentsFormatter.string(from: Date(), to: Date(timeIntervalSinceNow: 4000000))  // "1 month"
    
  • 相关阅读:
    9.堆排序
    8.全排列
    37.微信跳一跳辅助开发(C语言+EasyX)
    7.图形化实现快速排序法
    codeforces 632A A. Grandma Laura and Apples(暴力)
    codeforces 633D D. Fibonacci-ish(dfs+暴力+map)
    codeforces 633B B. A Trivial Problem(数论)
    codeforces 633A A. Ebony and Ivory(暴力)
    codeforces 622B B. The Time
    codeforces 622D D. Optimal Number Permutation(找规律)
  • 原文地址:https://www.cnblogs.com/jiafei/p/13793616.html
Copyright © 2020-2023  润新知