Swift可重复赋值的闭包属性

问题

开发中肯定有遇到过将外界传入的回调闭包保存起来,待内部的某个异步事件完成后,统一回调并清空这个闭包数组的场景,这里有个简单的事例

swift 复制代码
class Vehicle {}
enum VehicleManager {
 
    private static var loadClosures: [([Vehicle], Bool) -> Void] = [] 
    private static var isLoading = false

    static func loadList(_ completion: (([Vehicle], Bool) -> Void)? = nil) {
        if isLoading {
            loadClosures.appendIfNonNil(completion)
            return
        } 
        isLoading = true 
        loadClosures.appendIfNonNil(completion)

        AIPManger.loadVehicle { res in
            switch res {
            case .success(let models):
                onDidFetch(models, true)
            case .failure: 
                onDidFetch([], false)
            }
        }
    }

    private static func onDidFetch(_ models: [Vehicle], _ success: Bool) {
        isLoading = false
        let closures = loadClosures 
        loadClosures = [] 
        for c in closures { 
            c(models, success) 
        } 
    } 
}

怎么在不借助任何的数据结构的情况下也能保存外部传入的闭包参数呢?

解决方案

以下是一种解决方案

swift 复制代码
class Vehicle {}
enum VehicleManager {
    private static var loadClosure: (([Vehicle], Bool) -> Void)?
    private static var isLoading = false

    private static func stash(_ completion: (([Vehicle], Bool) -> Void)?) {
        guard let cmp = completion else { return }
        if let oldVal = loadClosure {
            loadClosure = { arg1, arg2 in
                oldVal(arg1, arg2)
                cmp(arg1, arg2)
            }
        } else {
            loadClosure = cmp
        }
    }

    static func loadList(_ completion: (([Vehicle], Bool) -> Void)? = nil) {
        stash(completion)
        if isLoading { return }
        isLoading = true
        AIPManger.loadVehicle { res in
            isLoading = false
            let closure = loadClosure
            loadClosure = nil
            switch res {
            case .success(let models):
                closure?(models, true)
            case .failure:
                closure?([], false)
            }
        }
    }
}

仔细观察会发现stash方法是对loadClosure属性的一层包装,而这恰好是PropertyWrapper所要解决的,不太了解PropertyWrapper可以移步Swift 最佳实践之 Property Wrapper

swift 复制代码
@propertyWrapper
public struct Param2Closure<T, U> {
    public typealias Closure = (T, U) -> Void
    private var closure: Closure?
    
    public init() {}
    
    public mutating func reset() {
        closure = nil
    }
    public mutating func callAndReset(_ arg1: T, _ arg2: U) {
        closure?(arg1, arg2)
        closure = nil
    }
    
    public var wrappedValue: Closure? {
        get { closure }
        set {
            guard let val = newValue else { return }
            if let oldVal = closure {
                closure = { arg1, arg2 in
                    oldVal(arg1, arg2)
                    val(arg1, arg2)
                }
            } else {
                closure = val
            }
        }
    }
}

使用该PropertyWrapper后的事例代码

swift 复制代码
class Vehicle {}
enum VehicleManager {
    @Param2Closure
    private static var loadClosure: (([Vehicle], Bool) -> Void)?
    private static var isLoading = false
    static func loadList(_ completion: (([Vehicle], Bool) -> Void)? = nil) {
        loadClosure = completion
        if isLoading { return }
        isLoading = true
        AIPManger.loadVehicle { res in
            switch res {
            case .success(let models):
                _loadClosure.callAndReset(models, true)
            case .failure:
                _loadClosure.callAndReset(models, false)
            }
            isLoading = false
        }
    }
}

简洁明了了很多,后续有类似需求,一个PropertyWrapper就可以搞定。

后话

Param2Closure属性包装器已收录在本人编写的SwifterKnife当中,同时还包括只有一个参数Param1Closure和无参数VoidClosure版本的。

使用Swift开发有两年多时间,该库积累了很多本人在开发当中常用的方法、扩展、工具类、常见问题的解决方案,致力于写出更高效更符合swift风格的代码,日常维护更新是在develop分支,不定期合并到master分支,欢迎读者提出宝贵意见和建议

小小Tips

若有意使用该库,建议

pod 'SwifterKnife', :git => 'https://github.com/CoderLouie/SwifterKnife.git', :branch => 'develop'

相关推荐
初级代码游戏1 小时前
iOS开发 SwitftUI 13:提示、弹窗、上下文菜单
ios·swiftui·swift·弹窗·消息框
zhyongrui4 小时前
托盘删除手势与引导体验修复:滚动冲突、画布消失动画、气泡边框
ios·性能优化·swiftui·swift
zhangfeng11337 小时前
CSDN星图 支持大模型微调 trl axolotl Unsloth 趋动云 LLaMA-Factory Unsloth ms-swift 模型训练
服务器·人工智能·swift
zhyongrui1 天前
SnipTrip 发热优化实战:从 60Hz 到 30Hz 的性能之旅
ios·swiftui·swift
大熊猫侯佩2 天前
Neo-Cupertino 档案:撕开 Actor 的伪装,回归 Non-Sendable 的暴力美学
swift·observable·actor·concurrency·sendable·nonsendable·data race
2501_915921433 天前
在没有源码的前提下,怎么对 Swift 做混淆,IPA 混淆
android·开发语言·ios·小程序·uni-app·iphone·swift
00后程序员张4 天前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
大熊猫侯佩4 天前
星际穿越:SwiftUI 如何让 ForEach 遍历异构数据(Heterogeneous)集合
swiftui·swift·遍历·foreach·any·异构集合·heterogeneous
hjs_deeplearning4 天前
认知篇#15:ms-swift微调中gradient_accumulation_steps和warmup_ratio等参数的意义与设置
开发语言·人工智能·机器学习·swift·vlm
墨瑾轩5 天前
C# PictureBox:5个技巧,从“普通控件“到“图像大师“的蜕变!
开发语言·c#·swift