Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created November 5, 2016 01:43
Show Gist options
  • Save wh1pch81n/2f25c215e5c8f78853a141abff6b96cc to your computer and use it in GitHub Desktop.
Save wh1pch81n/2f25c215e5c8f78853a141abff6b96cc to your computer and use it in GitHub Desktop.
Swift has no access modifier "Protected". Below is the closest thing to protected. You can call this semi-protected.
import UIKit
// discourage directly setting property m by using private(set). Anyone can still set the value of the property via setValue(_ :forKey:)
public class Abe: NSObject {
public private(set) var m: [String] = ["abc"]
}
public class Billy: Abe {
override init() {
super.init()
setValue(["abc", "def", "ghi"], forKey: "m")
}
}
Abe().m // ["abc"]
Billy().m // ["abc", "def", "ghi"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment