KVC change all Private

简介: KVC change all Private
  • 基本知识就不再介绍


  • 一.说一下: KVC可以随意修改一个对象的属性或者成员变量(并且私有的也可以修改)


@property(nonatomic,copy) NSString *name;


它有三种表达方式(结果都一样)


Person *person = [[Person alloc]init];
1.person.name = @"小红";
2.[person setValue:@"小红" forKey:@"name"];
3.[person setValue:@"小红" forKey:@"_name"];
4.[person setValue:@"小红" forKeyPath:@"_name"];
  • 二.下面私有的定义变量又该如何


{
   @private
   double _hight;//私有的变量,只有
}

设置值的方式应该是


[person setValue:@12.3 forKey:@"_hight"];

而为什么不带下划线也一样呢

[person setValue:@12.3 forKey:@"hight"];

原因:系统会查找hight相同的属性,如果找不到就会查找带下划线的_hight


  • 三.具体的说一下 forKey与forKeyPath的区别(通过给狗起名字来看看)建议以后都是用forKeyPath


  • 声明两个类:Person和Dog类

在Person里面声明两个属性
   @property(nonatomic,copy) NSString *name;
   @property(nonatomic,strong) Dog *dog;

在Dog里面声明一个属性

@property(nonatomic,copy) NSString *name;
    //四者没什么区别(取值直接用 [person valueForKeyPath:@" "])
    //1.person.dog.name = @"汪汪";
    //2.[person.dog setValue:@"汪汪" forKey:@"name"];
    //3.[person.dog setValue:@"汪汪" forKeyPath:@"name"];
    //4.[person setValue:@"汪汪" forKeyPath:@"dog.name"];
    但是第4种如果这种写就有区别了(forKey只寻找,person里面的属性,这里给dog设置名字是错误的,path是路径,需要一层一层的寻找)
    [person setValue:@"汪汪" forKey:@"dog.name"];//错误的写法
  • forKeyPath 包含了forKey的功能,以后使用keyPath就可以了
    forKeyPath中可以利用.运算符,就可以一层一层往下查找对象的属性(再深也不怕,一级一级往下用点属性找)


  • 区别总结:forKey只寻找person里面的属性,这里给dog设置名字是错误的,path是路径,需要一层一层的寻找


  • 四.Cocoa的大招是kvc(下面说一下关于数组的用法,kvc很强大)


在人的属性里面设置一个属性


@property(nonatomic,strong) NSArray *books;


再写一个Book类(声明两个属性,再进行一些属性设置)


@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)double price;
  Person *person = [[Person alloc]init];
  Book *book1 = [[Book alloc]init];
  book1.name = @"迷茫";
  book1.price = 21.5;
  Book *book2 = [[Book alloc]init];
  book2.name = @"奋斗";
  book2.price = 20.5;
  Book *book3 = [[Book alloc]init];
  book3.name = @"努力";
  book3.price = 24.5;
  Book *book4 = [[Book alloc]init];
  book4.name = @"成功";
  book4.price = 28.5;
  person.books = @[book1,book2,book3];
  //需求把所有的书名放到一个数组里面
  //1.for循环放值
  NSMutableArray *array = [[NSMutableArray alloc]init];
 for (Book *book in person.books) {
      [array addObject:book];
  }
NSLog(@"%@",array);
  //用kvc搞定
  //@"books.name" 取出数组元素的每一个元素的name属性的值,放到一个新的数组里面进行返回
 // NSArray *name = [person valueForKeyPath:@"books.name"];
  NSArray *price = [person valueForKeyPath:@"books.price"];
  //书本加起来一共多少钱???、
  //1.种for循环,定义数组,去数组元素
  double sum = 0;
  for (Book *book in person.books) {
      sum += book.price;
 }
  double sum = [[person valueForKeyPath:@"books.@sum.price"]doubleValue];
  NSLog(@"%f",sum);


目录
相关文章
|
6月前
使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation
使用EventBus 3.0 报 Subscriber class com.example.test.MainActivity and its super classes have no public methods with the @Subscribe annotation
122 5
|
6月前
|
JavaScript
const和readonly的区别
const和readonly的区别
44 0
VS2005 error C2864 only static const integral data members can be initialized within a class
VS2005 error C2864 only static const integral data members can be initialized within a class
|
程序员 iOS开发
NSNotification、Delegate、Block和KVO的区别是什么
NSNotification、Delegate、Block和KVO的区别是什么
142 0
|
JavaScript
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property b
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: vue项目示例,请参考甄佰 单向数据流所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。
3401 0
Object C学习笔记5-ARC forbids explicit message* 编译错误
  在学习Object C的过程中XCode 编译器经常出现 "ARC forbids explicit message send of release" 的错误提示。   以上问题主要出现在release,retain 等操作的时候,这是因为你在新建工程的时候使用ARC功能.
790 0