didset

What if I want to assign a property to itself?

二次信任 提交于 2019-12-18 12:50:27
问题 If I attempt to run the following code: photographer = photographer I get the error: Assigning a property to itself. I want to assign the property to itself to force the photographer didSet block to run. Here's a real-life example: In the "16. Segues and Text Fields" lecture of the Winter 2013 Stanford iOS course (13:20), the professor recommends writing code similar to the following: @IBOutlet weak var photographerLabel: UILabel! var photographer: Photographer? { didSet { self.title =

color of cgpoint changes all lines and should only change new lines

ⅰ亾dé卋堺 提交于 2019-12-11 14:29:45
问题 My code uses to classes. When the function dizzy is called it changes the color of all the lines in the uiview. What I want it to do is only change lines colors that are drawn after the function is called. It should not change the color of the lines that are already drawn like it does now. class ViewController: UIViewController { @objc func dizzy() { canvas.strokeColor = .gray } var canvas = Canvas() } class Canvas: UIView { var strokeColor = UIColor.green { didSet { self.setNeedsDisplay() }

How to implement the “didset of swift” in objective-c?

蓝咒 提交于 2019-12-10 12:31:51
问题 Swift (from the book 《iOS Animations by Tutorials:Chapter 12》 released by http://www.raywenderlich.com/): let photoLayer = CALayer() @IBInspectable var image: UIImage! { didSet { photoLayer.contents = image.CGImage } } How can I implement the above syntax in objective-c? I know only to set the property of photoLayer and image like below: @property (strong, nonatomic) CALayer *photoLayer; @property (strong, nonatomic) IBInspectable UIImage *image; But i do not know how to implement didset{...}

Swift: Overriding didSet results in a recursion

核能气质少年 提交于 2019-12-04 10:36:53
问题 When overriding the didSet observer of a property results in recursion, why? class TwiceInt { var value:Int = 0 { didSet { value *= 2 } } } class QuadInt : TwiceInt { override var value:Int { didSet { value *= 4 } } } let t = TwiceInt() t.value = 5 // this works fine let q = QuadInt() q.value = 5 // this ends up in recursion If I update the QuadInt with class QuadInt : TwiceInt { override var value:Int { didSet { super.value *= 4 } } } q.value = 5 // q.value = 80 So I guess the call to be

When/How - Outlet -> didSet

♀尐吖头ヾ 提交于 2019-12-01 01:39:12
I'm wondering, when/how does the didSet on an outlet actually trigger? For example : @IBOutlet weak var modifyButton: UIButton! { didSet { modifyButton.layer.cornerRadius = 9 } } Outlet properties initialized as nil when class just initialized. They will have values later, when objects will be initialized from nib. First step, when you can be sure that all properly configured outlet properties have nonnil values is viewDidLoad . So, didSet observer on this properties will be called just before viewDidLoad . Because all of this you need to be careful with outlet properties: for example you will

Why no Infinite loop in didSet?

送分小仙女□ 提交于 2019-11-30 22:12:59
In my FirstViewController I have a button directing to my SecondViewController, passing data to a property in the SecondViewController. This property has a property observer, creating a new instance of the SecondViewController when set. While it's working as I want, I wonder why it's not getting stuck in an infinite loop, creating an instance of the SecondViewController forever. And is it good practice to do it this way? FirstViewController: class FirstViewController: UIViewController { @IBAction func something(sender: UIButton) { let destination = storyboard?

In Swift, does resetting the property inside didSet trigger another didSet?

心已入冬 提交于 2019-11-30 17:32:05
I'm testing this and it appears that if you change the value within didSet , you do not get another call to didSet . var x: Int = 0 { didSet { if x == 9 { x = 10 } } } Can I rely on this? Is it documented somewhere? I don't see it in the Swift Programming Language document. I also thought, that this is not possible (maybe it wasn't in Swift 2), but I tested it and found an example where Apple uses this. (At "Querying and Setting Type Properties") struct AudioChannel { static let thresholdLevel = 10 static var maxInputLevelForAllChannels = 0 var currentLevel: Int = 0 { didSet { if currentLevel

Why no Infinite loop in didSet?

懵懂的女人 提交于 2019-11-30 17:31:16
问题 In my FirstViewController I have a button directing to my SecondViewController, passing data to a property in the SecondViewController. This property has a property observer, creating a new instance of the SecondViewController when set. While it's working as I want, I wonder why it's not getting stuck in an infinite loop, creating an instance of the SecondViewController forever. And is it good practice to do it this way? FirstViewController: class FirstViewController: UIViewController {

Swift: how to change a property's value without calling its didSet function

限于喜欢 提交于 2019-11-30 11:04:54
How can you set a property's value in Swift, without calling its didSet() function outside of an initialization context? The code below was a failed experiment to achieve this within the classes' noside() function class Test { var toggle : Bool = 0 var hoodwink : Int = 0 { didSet(hoodwink) { toggle = !toggle } } // failed attempt to set without a side effect func noside(newValue : Int) { hoodwink = newValue println("hoodwink: \(hoodwink) state: \(toggle)") } func withside(newValue : Int) { self.hoodwink = newValue println("hoodwink: \(hoodwink) state: \(toggle)") } } It is quite trivial to do

What if I want to assign a property to itself?

不羁的心 提交于 2019-11-30 08:25:43
If I attempt to run the following code: photographer = photographer I get the error: Assigning a property to itself. I want to assign the property to itself to force the photographer didSet block to run. Here's a real-life example: In the "16. Segues and Text Fields" lecture of the Winter 2013 Stanford iOS course (13:20), the professor recommends writing code similar to the following: @IBOutlet weak var photographerLabel: UILabel! var photographer: Photographer? { didSet { self.title = photographer.name if isViewLoaded() { reload() } } } override func viewDidLoad() { super.viewDidLoad() reload