Skip to content

Instantly share code, notes, and snippets.

@wenghengcong
Last active January 6, 2022 08:37
Show Gist options
  • Select an option

  • Save wenghengcong/0b1d03d6050fd6c53fd1c2e67f271392 to your computer and use it in GitHub Desktop.

Select an option

Save wenghengcong/0b1d03d6050fd6c53fd1c2e67f271392 to your computer and use it in GitHub Desktop.
KVC的应用
[textField setValue:[UIFont systemFontOfSize:25.0] forKeyPath:@"_placeholderLabel.font"];
获取空间内部属性:
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([UITextField class], &count);
for (int i = 0; i < count; i++) {
objc_property_t property = properties[i];
const char *name = property_getName(property);
NSLog(@"name:%s",name);
}

@interface Book : NSObject @property (nonatomic,assign) CGFloat price; @end

1.简单集合运算符

  • @avg
  • @count
  • @max
  • @min
  • @sum

NSArray* arrBooks = @[book1,book2,book3,book4]; NSNumber* sum = [arrBooks valueForKeyPath:@"@sum.price"];

2.对象运算符

  • @distinctUnionOfObjects
  • @unionOfObjects

// 获取所有Book的price组成的数组,并且去重 NSArray* arrDistinct = [arrBooks valueForKeyPath:@"@distinctUnionOfObjects.price"];

3.Array和Set操作符(集合中包含集合的情形)

  • @distinctUnionOfArrays
  • @unionOfArrays
  • @distinctUnionOfSets
//当对容器类使用KVC时,valueForKey:将会被传递给容器中的每一个对象,而不是容器本身进行操作。
//结果会被添加进返回的容器中,这样,开发者可以很方便的操作集合来返回另一个集合。
NSArray *arr = @[@"ali",@"bob",@"cydia"];
NSArray *arrCap = [arr valueForKey:@"capitalizedString"];
for (NSString *str in arrCap) {
NSLog(@"%@",str); //Ali\Bob\Cydia
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment