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 视图子元素动态化重构
网友评论