#import "ViewController.h"
#import "Publish.h"
#import "Author.h"
@interface ViewController ()
@property (nonatomic, strong) Author *author;
@property (nonatomic, strong) Publish *publish;
@end
@implementation ViewController
- (
void
)viewDidLoad {
[super viewDidLoad];
self.publish = [[Publish alloc] init];
self.author = [[Author alloc] init];
[self setValue];
[self getValue];
}
- (
void
)setValue
{
[self.author setValue:@
"calleng"
forKey:@
"name"
];
[self.author setValue:@(29) forKey:@
"age"
];
[self.publish setValue:@(20231005) forKeyPath:@
"publishTime"
];
[self.author setValue:@
"KVC example"
forKeyPath:@
"publish.title"
];
NSLog(@
"\n\n作者名字 - %@, 作者年纪 - %ld\n"
,
self.author.name,
(
long
)self.author.age);
NSLog(@
"\n\n作者发布的书名 self.author.publish.title = %@, self.author.publish.publishTime = %f\n"
,
self.author.publish.title,
self.author.publish.publishTime
);
}
- (
void
)getValue
{
NSString *authorName = [self.author valueForKey:@
"name"
];
NSInteger authorAge = [[self.author valueForKeyPath:@
"age"
]integerValue];
Publish *publish = [self.author valueForKey:@
"publish"
];
NSString *publishTitle1 = [publish valueForKeyPath:@
"title"
];
NSString *publishTitle2 = [self.author valueForKeyPath:@
"publish.title"
];
double
publishTime1 = [[publish valueForKey:@
"publishTime"
] doubleValue];
double
publishTime2 = [[self.author valueForKeyPath:@
"publish.publishTime"
] doubleValue];
NSLog(@
"\n\n 作者名字 : %@, 年纪: %lu \n"
, authorName, authorAge);
NSLog(@
"\n\n 发布的书名: \n publishTitle1 = %@ publishTitle2 = %@, \n 发布的时间: \n publishTime1 = %f publishTime2 = %f"
,
publishTitle1, publishTitle2,
publishTime1, publishTime2
);
}
@end