美文网首页
学习记录

学习记录

作者: 跬步千里_LenSky | 来源:发表于2018-09-19 18:30 被阅读52次

学习的路是漫长的,一点一点的去适应总能找到适合自己的

swift中字符串函数 函数描述 特殊说明
1、isEmpty 判断字符串是否为空,返回布尔值
2、hasPrefix(prefix: String) 检查字符串是否拥有特定前缀
3、Int(String)。 转换字符串数字为整型 let myString: String = "256" let myInt: Int? = Int(myString)
4、hasSuffix(suffix: String) 检查字符串是否拥有特定后缀
5、String.count 计算字符串的长度
6、+ 、+= 连接操作符两边的字符串并将新字符串赋值给左边的操作符变量
7、== 判断两个字符串是否相等
  • 宏定义
import UIKit

let kScreenHeight = UIScreen.main.bounds.height
let kScreenWidth = UIScreen.main.bounds.width
let KWscale = UIScreen.main.bounds.width/375.0
let KYScale = UIScreen.main.bounds.height/667.0
//颜色 rgb宏定义
func RGBCOLOR(r:CGFloat,_ g:CGFloat,_ b:CGFloat) -> UIColor
{
    return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
}
//按比例缩放X
func KScaleX(aX: CGFloat) -> CGFloat {
    let x = aX / 2.0 * KWscale
    return CGFloat(x)
}

//按比例缩放Y
func KScaleY(aY: CGFloat) -> CGFloat {
    let y = aY / 2.0 * KYScale
    return CGFloat(y)
}

// MARK:- 自定义打印方法
func LGLog<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
    
    #if DEBUG
    
    let fileName = (file as NSString).lastPathComponent
    
    print("\(fileName):(\(lineNum))-\(message)")
    
    #endif
}
  • 16进制字符串转换颜色
//TODO: 颜色分类
extension UIColor{
    class func hexadecimalColor(hexadecimal:String)->UIColor{
        var cstr = hexadecimal.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
        if(cstr.length < 6){
            return UIColor.clear;
        }
        if(cstr.hasPrefix("0X")){
            cstr = cstr.substring(from: 2) as NSString
        }
        if(cstr.hasPrefix("#")){
            cstr = cstr.substring(from: 1) as NSString
        }
        if(cstr.length != 6){
            return UIColor.clear;
        }
        var range = NSRange.init()
        range.location = 0
        range.length = 2
        //r
        let rStr = cstr.substring(with: range);
        //g
        range.location = 2;
        let gStr = cstr.substring(with: range)
        //b
        range.location = 4;
        let bStr = cstr.substring(with: range)
        var r :UInt32 = 0x0;
        var g :UInt32 = 0x0;
        var b :UInt32 = 0x0;
        Scanner.init(string: rStr).scanHexInt32(&r);
        Scanner.init(string: gStr).scanHexInt32(&g);
        Scanner.init(string: bStr).scanHexInt32(&b);
        return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1);
    }
}
  • 懒加载的书写方式
    lazy var tableView = { () -> UITableView in
        let tbv = UITableView(frame: self.view.bounds, style: .grouped)
        return tbv
    }()
  • 注册cell
tableView.register(MyTbleViewCell.self, forCellReuseIdentifier: "cell")
  • 代码示例
import UIKit
class MainViewContoller: UIViewController,UITableViewDelegate,UITableViewDataSource {
    

    
    lazy var tableView = { () -> UITableView in
        let tbv = UITableView(frame: self.view.bounds, style: .grouped)
        return tbv
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        tableView.register(MyTbleViewCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self
        
    }
    //TODO: UITableViewDelegate,UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let  aidentifier = "cell"
        let cell:MyTbleViewCell? = tableView.dequeueReusableCell(withIdentifier: aidentifier) as? MyTbleViewCell
        cell?.nameLabel?.text = "张三李四王五"
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
}

// TODO 自定义cell
class MyTbleViewCell: UITableViewCell {
    //声明变量
    var titleLabel:UILabel?
    var nameLabel:UILabel?
    var lineLabel:UILabel?
    var iconImgV:UIImageView?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置UI
        setUpUI()
        
    }
    func setUpUI() {
        
        //snap 设置约束
        titleLabel = UILabel()
        titleLabel?.font = UIFont.systemFont(ofSize: KScaleX(aX: CGFloat(20)))
        titleLabel?.textColor = UIColor.hexadecimalColor(hexadecimal: "#888888")
        titleLabel?.textAlignment = .left
        addSubview(titleLabel!)
        
        nameLabel = UILabel()
        nameLabel?.font = UIFont.systemFont(ofSize: KScaleX(aX: 20.0))
        nameLabel?.textColor = UIColor.white
        nameLabel?.textAlignment = .left
        addSubview(nameLabel!)
        //自定义打印
        LGLog("输出内容")
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Swift 在 3.0 和4.0的情况下可能还有些区别,有些在4.0以上的第三方不能用只好用3.0 比如 SnapKit 在用SnapKit 4.0.0的时候出现报错的问题

相关文章

网友评论

      本文标题:学习记录

      本文链接:https://www.haomeiwen.com/subject/ymtwnftx.html