#import <Foundation/Foundation.h>
#import "Person.h"
#import "Boar.h"
#import "Hunter.h"
#import "Forest.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Person *person=[[Person alloc]init];
// KVC:键值编码(Key Value Codeing )
// 设置属性值,Key是键,键的名字是我们的类属性的名字,当我们设置的键不存在时会报错:
// 例如: [person setValue:@"jack" forKey:@"jack"];//没有键名为jack
//报错: this class is not key value coding-compliant for the key jack.
person.name=@"jack";
[person setName:@"jack"];
//提高安全性,在之后的设计模式体现更为明显
// setValue: forKey://设置属性值
// valueForKey:(NSString *)//获取属性值
// setValue: forKeyPath://通过键路径设置属性
// valueForKeyPath://通过键路径获取值
[person setValue:@"jack" forKey:@"_name"];//当我们设置的类属性的键是不存在的但是与原有键的属性名形式也是正确的
//猎人打猎,扑获野猪。
//实例化猎人对象
Hunter *hunter=[[Hunter alloc]init];
// 设置猎人属性,用KVC的方法
[hunter setValue:@"xiaoMing" forKey:@"name"];
[hunter setValue:@"12" forKey:@"age"];
//实例化野猪对象
Boar *boar=[[Boar alloc]init];
//设置野猪的属性值
[boar setValue:@"littleBoar" forKey:@"name"];
[boar setValue:@"200" forKey:@"weight"];
//设置猎人猎到的野猪
[hunter setValue:boar forKey:@"boar"];
//valueForKey:获取值的方法。后面添加的参数是键名,参数是字符串形式的
NSLog(@"%@",[hunter valueForKey:@"name"]);
//显示出猎人和猎物的信息
NSLog(@"年纪是%@的猎人%@猎到的%@野猪重量为%@",[hunter valueForKey:@"age"],[hunter valueForKey:@"name"],[boar valueForKey:@"name"], [boar valueForKey:@"weight"]);
//这是键路径方法
NSLog(@"%@" ,[hunter valueForKeyPath:@"boar.weight"]);
// setValue: forKey:可以在字典里使用
NSMutableDictionary *dict=[[NSMutableDictionary alloc]initWithCapacity:1];
[dict setValue:@"12" forKey:@"33"];
NSLog(@"%@",dict);
//------练习:猎野猪
//猎人去森林猎野猪,森林里有三只野猪:120公斤的littleBoar,300公斤的fatherBoar和200公斤的motherBoar猎人只猎200公斤的野猪,当抓到野猪会查看重量不是目标重量就说:“太轻(重)了,继续打猎”
//创建三个野猪对象
Boar *boarI=[[Boar alloc]init];
Boar *boarII=[[Boar alloc]init];
Boar *boarIII=[[Boar alloc]init];
//利用KVC的方法设置属性
[boarI setValue:@"littleBoar" forKey:@"name"];
[boarI setValue:@"120" forKey:@"weight"];
[boarII setValue:@"fatherBoar" forKey:@"name"];
[boarII setValue:@"300" forKey:@"weight"];
[boarIII setValue:@"motherBoar" forKey:@"name"];
[boarIII setValue:@"200" forKey:@"weight"];
//建立森林数组
NSArray *forest=@[boarI,boarII,boarIII,boarI];
//实例化Forest类对象
Forest *bigForest=[[Forest alloc]init];
//规划森林范围有小森林
[bigForest setValue:forest forKey:@"smallForest"];
//设置猎人的目标
[hunter setValue:@"200" forKey:@"target"];
//出去猎人目标值
int targe=[[hunter valueForKey:@"target"] intValue];
// 猎人在森林里猎取猎物 count 是获取长度的意思
for (int i=0; i<[[bigForest valueForKey:@"smallForest"] count]; i++) {
//转换类型
Boar *boarHunt=[bigForest valueForKey:@"smallForest"][i];
//判断是否为目标
if ([[boarHunt valueForKey:@"weight"] intValue] > targe ) {
NSLog(@"太重了,再猎");
}
else if ([[boarHunt valueForKey:@"weight"] intValue] < targe){
NSLog(@"太轻了,继续");
}
else if ([[boarHunt valueForKey:@"weight"] intValue] == targe){
NSLog(@"猎到了");
break;//当猎到所需猎物结束狩猎
}
}
//如果森林是一个类,该怎么做?
//建立一个森林类,将已有的小森林扩进大森林里
//KVC 除了传值还可以计算:针对集合----->@max @min @sum @avg @count
NSLog(@"森林里野猪的头数:%@",[bigForest valueForKeyPath:@"smallForest.@count" ]);//用来计数@count
NSLog(@"森林里野猪的总重:%@",[bigForest valueForKeyPath:@"[email protected]" ]);//@sum用来计算总和
//练习:找出最重的和最轻的重量
NSLog(@"森林里最重的野猪体重为%@",[bigForest valueForKeyPath:@"[email protected]"]);
NSLog(@"森林里最轻的野猪体重为%@",[bigForest valueForKeyPath:@"[email protected]"]);
}
return 0;
}