kvc

Key Value Coding vs accessor methods in iOS

丶灬走出姿态 提交于 2019-12-22 09:47:31
问题 I'm trying to understand some theory part in Objective C related to KVC. Following is the example I've done. I'm having class call Cookie and it has a property like below @property NSString *name; Next, I have another class call Person and it has following property @property Cookie *cookie; Inside Person implementation file #import "Cookie.h" - (id)init { self = [super init]; if (self) { _cookie = [[Cookie alloc] init]; } return self; } In my ViewContrtoller I can write following two options

Swift optional property using KVC causes crash

不问归期 提交于 2019-12-14 03:36:40
问题 I found that using KVC in Swift causes many problems, especially with optional properties. Here is my specific problem: Here is a Class named Person . It has a normal property called age ,and a Optional(Int) property called ageOptional . class Person: NSObject { var age: Int var ageOptional: Int? override init(age: Int){ self.age = 0 } } Now, I use KVC in Person's instance: //new a instance var person = Person() //kvc for normal property: it work well person.setValue(28, forKeyPath: "age") /

How do I change a UIButton's title using KVC?

≡放荡痞女 提交于 2019-12-13 18:35:04
问题 I have an array of UIButtons and want to set all their titles to a specific value at once, without looping through the array. The only solution I've found is by means of Key-Value Coding, i.e. something like this: [self.board setValue:@"X" forKeyPath:@"titleLabel.text"]; However, the button's titleLabel property is readonly and cannot be changed. I also tried using the button's title property as the keypath, but it doesn't work either. I've done this before by changing the "enabled" property

Format string for NSPredicate with @avg collection operator

别等时光非礼了梦想. 提交于 2019-12-11 04:23:55
问题 Is there a way to construct an NSPredicate so the following array can be filtered by average score larger than, say, 5? NSArray *objs = @[ @{@"scores":@[@3, @5, @2]}, @{@"scores":@[@5, @2, @8, @9]}, @{@"scores":@[@7, @1, @4]} ]; I have tried various combinations, of which this one seemed the most promising (considering that the key path @avg.self works to obtain the average value of numbers in an array through normal KVC): NSPredicate *pred = [NSPredicate predicateWithFormat:@"scores.@avg

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];

Filter Array of Objects with NSPredicate based on NSInteger property in super class

流过昼夜 提交于 2019-12-10 16:49:13
问题 I've got the following setup: @interface Item : NSObject { NSInteger *item_id; NSString *title; UIImage *item_icon; } @property (nonatomic, copy) NSString *title; @property (nonatomic, assign) NSInteger *item_id; @property (nonatomic, strong) UIImage *item_icon; - (NSString *)path; - (id)initWithDictionary:(NSDictionary *)dictionairy; @end And #import <Foundation/Foundation.h> #import "Item.h" @interface Category : Item { } - (NSString *)path; @end I've got an array with Category instances

Access property via it's keyPath in Javascript?

谁都会走 提交于 2019-12-10 10:13:20
问题 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 way, like: numberOrText = data[memberName+'.'+propertyName]; Is there any lightweight library, or snippet you can suggest? This is - https://github.com/martinvl/KVCObject - so cool, but a bit overhead for this. 回答1: You can easily resolve keypath with reduce function , without using any library.

Core Data setValue for Multiple Attributes at once

浪尽此生 提交于 2019-12-09 14:14:26
问题 I have a Book entity in Core Data and a Book class acting as the model layer. My Book model has multiple attributes. When comes the time to persist the books in Core Data, I would like to store them without having to do bookObject.setValue(book.title , forKey: "name") bookObject.setValue(book.author , forKey: "author") bookObject.setValue(book.datePublished , forKey: "datePublished") etc... Is there a method akin to setValue for setting multiple CoreData Entity attributes at once ? 回答1: If

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

北城余情 提交于 2019-12-07 01:43:52
问题 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 forKey:@"fontSize"] My Swift object ( textObject ) has a custom setter/getter for this property. var fontSize: CGFloat? { get { return internalTextGraphic?.fontSize } set { internalTextGraphic?.fontSize = newValue } } However, if I set a breakpoint in the set ,

Key Value Coding vs accessor methods in iOS

房东的猫 提交于 2019-12-06 00:12:56
I'm trying to understand some theory part in Objective C related to KVC. Following is the example I've done. I'm having class call Cookie and it has a property like below @property NSString *name; Next, I have another class call Person and it has following property @property Cookie *cookie; Inside Person implementation file #import "Cookie.h" - (id)init { self = [super init]; if (self) { _cookie = [[Cookie alloc] init]; } return self; } In my ViewContrtoller I can write following two options to get the same result. Using KVC : [me valueForKeyPath:@"cookie.name"] Using accessor methods : [[me