Questions tagged [key-value-observing]

Key-value Observing or KVO is a technology for observing changes in object properties.

1201 questions
15
votes
1 answer

KVObserving a protocol in Swift 4

I am struggling to use the new strongly-typed KVO syntax in Swift 4 to observe properties that are only visible through a protocol: import Cocoa @objc protocol Observable: class { var bar: Int { get } } @objc class Foo: NSObject, Observable { …
proxi
  • 1,243
  • 10
  • 18
15
votes
1 answer

Bool Property Cannot be marked dynamic in swift

I'm trying to observe Bool value in swift using KVO and add dynamic modifier like this : dynamic var isRestricted:Bool? and the compiler say Property cannot be marked dynamic because its type canot be represented in Objective-C then what should…
Dennis Farandy
  • 277
  • 1
  • 5
  • 9
15
votes
2 answers

when to use KVO?

I have read many docs on KVO, but I'm still confused as to when to use it. In case objA wants to monitor a certain property of objB, like so: self.objB = [[ObjB alloc] init]; [self.objB addObserver:self forKeyPath:@"address" …
limboy
  • 3,879
  • 7
  • 37
  • 53
15
votes
3 answers

Send Notification When a Property is Changed Using KVO

I had a property named myName in my class, like: @property (nonatomic, strong) NSString *myName; I need to send a notification when the myName property's value is changed. Now I'm doing something like: - (void)setMyName:(NSString *)name { _myName…
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
14
votes
2 answers

NSProxy and Key Value Observing

NSProxy seems to work very well as stand-in objects for those that don't yet exist. For example. - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel { return [self.target methodSignatureForSelector:sel]; } -…
Tony
  • 36,591
  • 10
  • 48
  • 83
14
votes
2 answers

What's wrong with this observeValueForKeyPath:ofObject:change:context: implementation?

In my UIScrollView subclass, I'm observing frame changes: [self addObserver:self forKeyPath:@"frame" options:0 context:NULL]; My observeValueForKeyPath:ofObject:change:context: implementation is as follows: - (void)observeValueForKeyPath:(NSString…
an0
  • 17,191
  • 12
  • 86
  • 136
14
votes
1 answer

iPhone KVO between two classes

I have two classes in my app class A and Class B. Both class A and B are instances of UIViewController. Class A has a button that when pushed pushes class B onto the stack. Class B has a string that class A would like to observe and update it's…
fmcauley
  • 165
  • 1
  • 1
  • 7
14
votes
3 answers

KVO on Swift's computed properties

Is KVO on a computed property possible in Swift? var width = 0 var height = 0 private var area : Double { get { return with * height } } self.addOberser(self, forKeyPath: "area", ...... Would a client code modifying the with or…
David Homes
  • 2,725
  • 8
  • 33
  • 53
14
votes
1 answer

Parameters from observeValueForKeyPath:ofObject:change:context:

I was wondering what the parameters from this method would return. - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change …
Joshua
  • 15,200
  • 21
  • 100
  • 172
12
votes
3 answers

MVC in Cocoa Touch: How do the view and the model interact?

I’ve always thought I understood MVC, but lately, after reading a lot of Stack Overflow posts on the subject, I’ve discovered that the ways in which MVC frameworks do things are slightly different from one another. More specifically, in the way in…
12
votes
4 answers

adding KVO to UITableViewCell

I have a custom UITableViewCell which is displaying various attributes of a Person object (backed by Core Data) ... some labels, images etc. I currently force the whole tableview to reload whenever any property changes, and that's obviously not…
Z S
  • 7,039
  • 12
  • 53
  • 105
12
votes
3 answers

Implement own setter or use KVO?

In short, when the property value changing, I have to update some logic in my code, for example: - (void)setProp:(NSString *)theProp { if (prop != theProp){ [prop release]; prop = [theProp copy]; [self myLogic]; } } or: -…
cxa
  • 4,238
  • 26
  • 40
12
votes
1 answer

Xcode 9 : Block Based KVO Violation for observeValue function

I’ve SwiftLint enabled in project and it throws an warning for below function : override func observeValue(forKeyPath keyPath: String?, of _: Any?, change: [NSKeyValueChangeKey: Any]?, context _: UnsafeMutableRawPointer?) { . . . } Shell…
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
12
votes
1 answer

Key-Value Observe in Swift not showing insertions and removals in arrays

I created a class which contains an array. I added an observer to that array in a view controller and performed some modifications to that array. The problem is that when I print the change dictionary returned by the observeValueForKeyPath() method…
Felipe Ferri
  • 3,488
  • 2
  • 33
  • 48
12
votes
5 answers

What's a good way to bind from a shared utility window and the frontmost document window?

I have an application which allows for multiple NSDocuments to be open. In this application is a single utility window that contains some functionality that I want to apply to the frontmost document. I am trying to use bindings here, so the trick…
danwood
  • 1,512
  • 1
  • 10
  • 27