Questions tagged [kvc]

Key Value Coding is a mechanism for accessing an object’s properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables.

References

193 questions
14
votes
3 answers

Why is NSUserDefaults unwilling to save my NSDictionary?

I'm using KVC to serialize an NSObject and attempt to save it to NSUserDefaults, which is giving me an Attempt to insert non-property value when I try to store my NSDictionary. Following are the properties of the object in question,…
Thomson Comer
  • 3,919
  • 3
  • 30
  • 32
13
votes
3 answers

KVC vs fast enumeration

Which of the following is faster and why? CGFloat sum = 0; for (UIView *v in self.subviews) sum += v.frame.size.height; or CGFloat sum = [[self.subviews valueForKeyPath:@"@sum.frame.size.height"] floatValue];
L_Sonic
  • 594
  • 6
  • 22
13
votes
1 answer

Interface Builder's User Defined Runtime Attributes not accepting floats?

I created an NSView subclass that has a float property and I'd like to set it in Interface Builder. In the User Defined Runtime Attributes section the only suitable Type is Number. But if I want to enter a decimal number (either using . or , as…
DrummerB
  • 39,814
  • 12
  • 105
  • 142
11
votes
6 answers

Objective-C & KeyValueCoding: How to avoid an exception with valueForKeyPath:?

I've got an object of type id and would like to know if it contains a value for a given keyPath: [myObject valueForKeyPath:myKeyPath]; Now, I wrap it into a @try{ } @catch{} block to avoid exceptions when the given keypath isn't found. Is there a…
swalkner
  • 16,679
  • 31
  • 123
  • 210
10
votes
1 answer

NSUnknownKeyException : was sent to an object that is not KVC-compliant for the "player" property

I have updated my code to swift 4 in Xcode 9. Before that it was working fine. But now AVplayer is crashing at observers below is the code where it is crashing. addObserver(self, forKeyPath: "player.currentItem.duration", options: [.new, .initial],…
Shuja Ud Din
  • 434
  • 5
  • 17
10
votes
1 answer

Does adding a KVO observer to self cause a memory leak?

In a nsobject you have a property "keyPath" you want to observe itself and you use [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:nil]; Does the above line cause a retain cycle? I present this question…
Yogurt
  • 2,913
  • 2
  • 32
  • 63
8
votes
2 answers

Can the non-string "property name" passed to #keyPath() be saved independently?

I was very happy to find Swift 3's implementation of #keyPath(), which will eliminate typos and enforce at compile time that the key path actually exists. Much better than manually typing…
pkamb
  • 33,281
  • 23
  • 160
  • 191
8
votes
8 answers

Access property via it's keyPath in Javascript?

I have data = { 'first': { 'number': 1, 'text': 'Ya.' }, 'second': { 'number': 10, 'text': 'Da.' } }; And I really want to access it like: number = data['first.number']; Actually in a more flexible…
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
7
votes
3 answers

Apple's Key Value Coding - can somebody explain to a C# developer why I would need this and what it does?

Before I switched to iOS development via Monotouch I played around a bit with ObjectiveC. Ever since one thing has been on my mind "Key Value Coding" (KVC). I have never understood what it is good for and what the magic behind it is. For my…
Krumelur
  • 32,180
  • 27
  • 124
  • 263
7
votes
1 answer

Why can I not use KVC from an Objective-C object to a Swift Property?

My team has decided that new files should be written in swift, and I am seeing an odd problem with using KVC in an Objective-C object to set a property on a Swift object. My Objective-C sets a property like so: [textObject setValue:0.0…
A O
  • 5,516
  • 3
  • 33
  • 68
7
votes
1 answer

Core Data: setPrimitiveValue:forKey: behaves really weirdly

This is a mystery: I'm invoking setPrimitiveValue:forKey: on an NSManagedObject. The key is a legit, persistent, modeled attribute of the object. However, setPrimitiveValue:forKey: fails, often setting the value for a different, arbitrary attribute.…
Steveo
  • 2,238
  • 1
  • 21
  • 34
7
votes
1 answer

iOS key value coding

Let's assume we have an object of type person, which has a property called name. Person *p; [p setValue:@"John" forKey:@"name"]; This works very nicely. What I want to do is dig deeper. Say the class person has another property called address which…
Teo
  • 3,394
  • 11
  • 43
  • 73
7
votes
1 answer

Key-Value Coding @UnionOfObjects

I can't figure out what @UnionOfObjects offers that a simple valueForKey: or valueForKeyPath: can't do. Apple docs: The @unionOfObjects operator returns an array containing the distinct objects in the property specified by the key path to the…
Jesse Gumpo
  • 4,777
  • 1
  • 20
  • 29
6
votes
2 answers

How to learn about KVC with swift 4.2

I'm using Xcode 10.0 with swift 4.2 to learn about Key Value Coding from "Cocoa programming for OSX" I'm asked to create a simple class, which is a subclass of NSObject. The codes below: import Cocoa class Student: NSObject { var name: String =…
hoboBob
  • 832
  • 1
  • 17
  • 37
6
votes
3 answers

How to pass values using KVO?

I am learning how to use KVO. I created two classes, Truck and Driver as shown below. the Truck class has a textfield and a button, the text should contain the current truck speed, and when the button is pressed, prepareForSegue should be called and…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
1
2 3
12 13