Latest 1.0.2
Homepage https://github.com/yscode001/YSInit
License MIT
Platforms ios 9.0
Frameworks UIKit
Authors

初始化的语法糖

环境

swift4.2、iOS9.0

安装使用

1、导入框架

pod 'YSInit'

2、导入命名空间

import YSInit

简单介绍

// class的then语法糖:设置自身属性、或对自身操作,返回自己
// 默认NSObject遵守了YSThen_class协议
extension NSObject: YSThen_class{}
private lazy var testV:UIView = UIView().ysThen{ $0.backgroundColor = UIColor.white }

// 如果自定义的类要想实现then语法糖,只需要遵守YSThen_class协议即可
class ViewController: UIViewController {
    private lazy var dog:Dog = Dog().ysThen{ $0.name = "辛巴" }
}

class Dog {
    var name = ""
}

// 遵守YSThen_class协议
extension Dog: YSThen_class{}
// struct的then语法糖:会复制一份Copy,然后对Copy设置属性、或操作,返回Copy
// 默认以下struct遵守了YSThen_struct协议
extension CGRect: YSThen_struct{}
extension CGPoint: YSThen_struct {}
extension CGSize: YSThen_struct {}
extension CGVector: YSThen_struct {}
extension UIEdgeInsets: YSThen_struct {}
extension UIOffset: YSThen_struct {}
extension UIRectEdge: YSThen_struct {}

// 如果自定义的struct要想实现then语法糖,只需要遵守YSThen_struct协议即可
/*
为方便起见,以下常用类实现了一些自定义的构造方法,当然完全可以使用then语法替代
UIAlertAction、UIBarButtonItem、UIButton、UICollectionView、UIColor、
UIImage、UIImageView、UILabel、UIPasteboard、UIProgressView、
UIScrollView、UISwitch、UITableView、URL
*/
方法调用: 类名.ys.create
示例:
let lbl = UILabel.ys.create(UIColor.red, text: "你好")
let url = URL.ys.create("https://www.baidu.com/")
// UITableView、UICollectionView创建空白Cell

/// 注意,一定要保证注册了identifier = "ysCellID_nothing"
let cell = UITableView().ys_createCell_nothing(indexPath)

/// 注意,一定要保证注册了identifier = "ysCellID_nothing"
let cell = UICollectionView().ys_createCell_nothing(indexPath)

感谢

最后感谢devxoul大神,then语法糖是根据devxoul大神的Then库转化来的,详见:https://github.com/devxoul/Then

Latest podspec

{
    "name": "YSInit",
    "version": "1.0.2",
    "summary": "u521du59cbu5316u7684u8bedu6cd5u7cd6",
    "homepage": "https://github.com/yscode001/YSInit",
    "license": "MIT",
    "authors": {
        "ys": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/yscode001/YSInit.git",
        "tag": "1.0.2"
    },
    "source_files": "YSInit/YSInit/YSInit/**/*",
    "frameworks": "UIKit"
}

Pin It on Pinterest

Share This