Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to deprecate only the setter of a variable?

I currently have a variable in a class open var deviceIdentifier: String? and would like to deprecate the setter only turning it into open private(set) var deviceIdentifier: String?

Is there any way to do that? I tried to put both lines in but that is obviously a conflict Xcode doesn't like.

@available(*, deprecated)
open var deviceIdentifier: String?

open private(set) var deviceIdentifier: String?

The compiler is happy if I change it to a computed variable but I'd like to avoid that. Is this the only way, and will this work as expected?

private var _deviceIdentifier: String?
open var deviceIdentifier: String? {
    get {
        return _deviceIdentifier
    }
    @available(*, deprecated)
    set(newValue){

    }
}
like image 307
Jeremiah Avatar asked Dec 14 '25 20:12

Jeremiah


1 Answers

It seems the only way to do this right now is with a computed variable, but it does work as expected.

//TODO: put back to non-computed variable when making set private
private var _deviceIdentifier: String?    
open var deviceIdentifier: String? {
    get {
        return _deviceIdentifier
    }
    @available(*, deprecated)
    set(newValue) {
        _deviceIdentifier = newValue
    }
}
like image 93
Jeremiah Avatar answered Dec 16 '25 13:12

Jeremiah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!