美文网首页
Swift 泛型函数学习

Swift 泛型函数学习

作者: SoaringHeart | 来源:发表于2019-08-05 15:11 被阅读0次
Swift
/// 两个Int(+-*/)
public func resultByOpt(_ num1: Int, _ num2: Int, result: (Int, Int) -> Int) -> Int {
    return result(num1, num2);
}
    
var result1 = resultByOpt(5, 3){ num1, num2 in
    num1 + num2
}

var result2 = resultByOpt(5, 3){ num1, num2 in
    num1 - num2
}

var result3 = resultByOpt(5, 3){ num1, num2 in
    num1 * num2
}

var result4 = resultByOpt(5, 3){ num1, num2 in
    num1 / num2
}

//日志
[96]:__result1 = 8
[97]:__result2 = 2
[98]:__result3 = 15
[99]:__result4 = 1

// 泛型版本
/// 两个数值(+-*/)
public func resultByOpt<T>(_ num1: T, _ num2: T, result: (T, T) -> T) -> T   {
    return result(num1, num2);
}

Kotlin
public fun resultByOpt(num1 : Int , num2 : Int , result : (Int ,Int) -> Int) : Int{
    return result(num1,num2)
}

var result1 = resultByOpt(1, 2){ num1, num2 ->
    num1 + num2
}

var result2 = resultByOpt(3,4){ num1, num2 ->
    num1 - num2
}

var result3 = resultByOpt(5,6){ num1, num2 ->
    num1 * num2
}

var result4 = resultByOpt(6,3){ num1, num2 ->
    num1 / num2
}
UITableViewCell 复用泛型封装
public extension UITableView{
    /// 泛型复用register cell - Type: "类名.self" (备用默认值 T.self)
    final func register<T: UITableViewCell>(cellType: T.Type, forCellReuseIdentifier identifier: String = String(describing: T.self)){
        register(cellType.self, forCellReuseIdentifier: identifier)
    }
    /// 泛型复用register UITableViewHeaderFooterView - Type: "类名.self" (备用默认值 T.self)
    final func register<T: UITableViewHeaderFooterView>(cellType: T.Type, forHeaderFooterViewReuseIdentifier identifier: String = String(describing: T.self)){
        register(cellType.self, forHeaderFooterViewReuseIdentifier: identifier)
    }
    
    /// 泛型复用cell - cellType: "类名.self" (默认identifier: 类名字符串)
    final func dequeueReusableCell<T: UITableViewCell>(for cellType: T.Type, identifier: String = String(describing: T.self), style: UITableViewCell.CellStyle = .default) -> T{
        //        let identifier = String(describing: T.self)
        var cell = self.dequeueReusableCell(withIdentifier: identifier);
        if cell == nil {
            cell = T.init(style: style, reuseIdentifier: identifier);
        }
        
        cell!.selectionStyle = .none;
        cell!.separatorInset = .zero;
        cell!.layoutMargins = .zero;
        cell!.backgroundColor = .white
        return cell! as! T;
    }
    
    /// 泛型复用cell - aClass: "类名()" (默认identifier: 类名字符串)
    final func dequeueReusableCell<T: UITableViewCell>(for aClass: T, identifier: String = String(describing: T.self), style: UITableViewCell.CellStyle = .default) -> T{
        return dequeueReusableCell(for: T.self, identifier: identifier, style: style)
    }
    
    /// 泛型复用HeaderFooterView - cellType: "类名.self" (备用默认值 T.self)
    final func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(for cellType: T.Type, identifier: String = String(describing: T.self)) -> T{
        var cell = self.dequeueReusableHeaderFooterView(withIdentifier: identifier);
        if cell == nil {
            cell = T.init(reuseIdentifier: identifier);
        }
        cell!.layoutMargins = .zero;
        return cell! as! T;
    }
    
    /// 泛型复用HeaderFooterView - aClass: "类名()" (默认identifier: 类名字符串)
    final func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(for aClass: T, identifier: String = String(describing: T.self)) -> T{
        return dequeueReusableHeaderFooterView(for: T.self, identifier: identifier)
    }
}
UICollectionViewCell 复用泛型封装
public extension UICollectionView{
    
    /// 泛型复用register cell - Type: "类名.self" (备用默认值 T.self)
    final func register<T: UICollectionViewCell>(cellType: T.Type, forCellWithReuseIdentifier identifier: String = String(describing: T.self)){
        register(cellType.self, forCellWithReuseIdentifier: identifier)
    }
    
    /// 泛型复用register supplementaryView - Type: "类名.self" (备用默认值 T.self)
    final func register<T: UICollectionReusableView>(supplementaryViewType: T.Type, ofKind elementKind: String = UICollectionView.elementKindSectionHeader){
        guard elementKind.contains("KindSection"), let kindSuf = elementKind.components(separatedBy: "KindSection").last else {
            return;
        }
        let identifier = String(describing: T.self) + kindSuf
        register(supplementaryViewType.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: identifier)
    }
    
    /// 泛型复用cell - cellType: "类名.self" (默认identifier: 类名字符串)
    final func dequeueReusableCell<T: UICollectionViewCell>(for cellType: T.Type, identifier: String = String(describing: T.self), indexPath: IndexPath) -> T{
        let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
        cell.backgroundColor = .white
        return cell as! T;
    }
    
    /// 泛型复用cell - aClass: "类名()" (默认identifier: 类名字符串)
    final func dequeueReusableCell<T: UICollectionViewCell>(for aClass: T, identifier: String = String(describing: T.self), indexPath: IndexPath) -> T{
        return dequeueReusableCell(for: T.self, identifier: identifier, indexPath: indexPath)
    }
    
    /// 泛型复用SupplementaryView - cellType: "类名.self" (默认identifier: 类名字符串 + Header/Footer)
    final func dequeueReusableSupplementaryView<T: UICollectionReusableView>(for cellType: T.Type, kind: String, indexPath: IndexPath) -> T{
        let kindSuf = kind.components(separatedBy: "KindSection").last;
        let identifier = String(describing: T.self) + kindSuf!;
        let view = self.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath)
        #if DEBUG
        view.backgroundColor = kind == UICollectionView.elementKindSectionHeader ? .green : .yellow;
        #endif
        return view as! T;
    }
    
    /// 泛型复用SupplementaryView - aClass: "类名()" (默认identifier: 类名字符串 + Header/Footer)
    final func dequeueReusableSupplementaryView<T: UICollectionReusableView>(for aClass: T, kind: String, indexPath: IndexPath) -> T{
        return dequeueReusableSupplementaryView(for: T.self, kind: kind, indexPath: indexPath)
    }
    
}

Objective-C 高阶函数实现
iOS 视图子元素动态化重构

相关文章

  • iOS开发 - 「Swift 学习」Swift泛型

    Swift泛型 泛型函数 以上是交换两个任意相同类型值的泛型函数,T是一个占位命名类型,swift不会查找命名为T...

  • swift泛型整理

    swift泛型知识主要包括:泛型函数、泛型协议、泛型类型、泛型约束。 一、泛型函数。 如:一个交换两个变量值的函数...

  • 2021-12-01

    swift5基本语法-泛型函数和泛型类型 Swift中泛型可以将类型参数化,提高代码复用率,减少代码量。 一、泛型...

  • Swift 泛型函数学习

    Swift Kotlin UITableViewCell 复用泛型封装 UICollectionViewCell ...

  • swift泛型函数

    本文转载自http://blog.csdn.net/youshaoduo/article/details/5486...

  • Swift:泛型

    泛型 泛型函数泛型函数和非泛型函数的不同之处在于:泛型函数名(swapTwoValues(::))后面跟着 占位类...

  • iOS__在swift中实现debug隐藏打印日志

    在AppDelegate.swift文件中自定义一个泛型函数如下:

  • 【Swift】泛型常见使用

    1、Swift泛型4种 泛型函数泛型类型泛型协议泛型约束 2、泛型约束3种 继承约束:泛型类型 必须 是某个类的子...

  • V语言学习笔记-14泛型

    目前的泛型主要有这三种:泛型结构体,泛型函数,泛型方法 泛型结构体 泛型函数 判断2个数组是否相等的泛型函数 泛型方法

  • Swift项目开发实用篇:自定义DEBUG下的LOG

    Swift项目中我们怎么自定义LOG.在DEBUG模式下打印RELEASE模式下不打印?我们通过泛型函数实现(泛型...

网友评论

      本文标题:Swift 泛型函数学习

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