key-value-coding

Counting how many times a value of a certain key appears in JSON

为君一笑 提交于 2019-12-12 06:22:27
问题 I have an array within my JSON file which looks as follows: { "commands": [ { "user": "Rusty", "user_id": "83738373", "command_name": "TestCommand", "command_reply": "TestReply" } ] } and so on. I want to limit the amount of commands to a certain user (recognized by the user_id ) to 3 commands. I know I need to start by looping through each object but stuck at how to accomplish this part. 回答1: You can do this by using the .reduce() method on the Array prototype. The idea is to go through the

Making a protocol's property KVC compliant

落爺英雄遲暮 提交于 2019-12-12 01:55:10
问题 I'm using Xcode 6.1.1 and the iOS SDK 8.1 I have a class that is derived from NSObject , but its property is defined in a protocol (it is called phoneNumber , which I don't believe to be a problematic name). I'm getting the error message this class is not key value coding-compliant for the key phoneNumber. so I implemented the valueForKey: and setValue:ForKey: methods on my own and it fixed it. Is there a better way of doing making it KVC compliant? 回答1: Auto property synthesis will not

simple app with error “ this class is not key value coding-compliant for the key ”

泄露秘密 提交于 2019-12-11 19:26:21
问题 I create a simple iPhone app with xib file, and add one button to the view. create a IBOutlet to connect with it. each time, I launch it, it will crash. the full error message is as below: 2014-05-03 08:10:19.742 test[1435:a0b] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key txtBtn.' There are many people hitting this question, After reviewing the answers, I think my problem is

Using KVC in NSSortDescriptor

喜你入骨 提交于 2019-12-11 18:09:38
问题 I need to sort a bunch of objects based on an integer that is stored in an NSString . I know this is one solution and it works: NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"from" ascending: YES comparator:^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult) NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult) NSOrderedAscending; } return (NSComparisonResult)

Why is KVO retaining all of NSMutableArray's objects?

落花浮王杯 提交于 2019-12-11 15:34:42
问题 I'm having a real pain trying to figure out why KVO is retaining all values that are being observed. Scenario: I have an NSMutableArray with weak references. (Set up with CFArrayCreateMutable and Callback having NULL for both retain and release). Meaning any object added is never retained / released. I have an NSArrayController observing values of the NSMutableArray. But now, every object of the NSMutableArray is also retained. Why is this happening and how do I stop this? 回答1: How do you

Custom class NSObject not key value coding compliant [duplicate]

守給你的承諾、 提交于 2019-12-11 02:23:49
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Why is my object not key value coding-compliant? I'm having a dictionary and I want to add keys/values to a custom class, but i always get the error, that the class is not KVC compliant, but the Apple documents state that it should be. My code: ContactObject.h: @interface ContactObject : NSObject + (ContactObject *)testAdding; @end ContactObject.m: @implementation ContactObject - (id)init { self = [super init];

Foundation Objective-c: Dictionary with array; dict with dict

早过忘川 提交于 2019-12-10 18:44:22
问题 Suppose I have a NSDictionary with two sub collections of a NSArray and a NSDictionary: NSMutableDictionary *mkDict(void){ NSMutableDictionary *dict=[NSMutableDictionary dictionary]; NSMutableDictionary *sub=[NSMutableDictionary dictionary]; NSMutableArray *array= [NSMutableArray array]; [dict setObject:array forKey:@"array_key"]; [dict setObject:sub forKey:@"dict_key"]; return dict; } There are a multitude of ways to access a single element of the sub-collection, and I choose to time three

KVC setNilValueForKey: recommends calling method and not using property accessor

﹥>﹥吖頭↗ 提交于 2019-12-10 16:27:27
问题 The KVC Documentation says The key-value coding method setNilValueForKey: method is called when you attempt to set an attribute to nil. Sounds good so far ... uses setValue:forKey: to set the new value. This maintains encapsulation of the model and ensures that any additional actions that should occur as a result of setting the value will actually occur. This is considered better practice than calling an accessor method or setting an instance variable directly. Why is it better practice to

RESTKit: Comparing GET object with locally persisted before overwriting

﹥>﹥吖頭↗ 提交于 2019-12-10 10:34:20
问题 I have a saved object (persisted) in Core Data. Lets say below are the values: //Entity: employee objectID: 1111 firstName: @"Jon" lastName: @"D" modified: @"10:45PM" Now, I do a RKManagedObjectRequestOperation *operation request. I set operation.savesToPersistentStore = NO; and start the operation. The object was downloaded and it's been modified to the following: //Entity: employee objectID: 1111 firstName: @"Jonathan" lastName: @"Doe" modified: @"10:55PM" //I have 15 other properties that

Why does valueForKey: on a UITextField throws an exception for UITextInputTraits properties?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 02:37:00
问题 Running this: @try { NSLog(@"1. autocapitalizationType = %d", [self.textField autocapitalizationType]); NSLog(@"2. autocapitalizationType = %@", [self.textField valueForKey:@"autocapitalizationType"]); } @catch (NSException *exception) { NSLog(@"3. %@", exception); } Outputs this: 1. autocapitalizationType = 0 3. [<UITextField 0x6c15df0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key autocapitalizationType. I was expecting: 1. autocapitalizationType = 0 2.