/**
testLoadPersonAsync ?????????:
???????????????:??????????????????!
1.????е?:
1.1 ?? setUp --->
??? test1
?? test2
?? test3 ......
?? tearDown ???????????????????.
*/
#pragma mark - testLoadPersonAsync ?????????
- (void)testLoadPersonAsync {
//Xcode6.0??????????:
//???????"???"expectation:
XCTestExpectation *expectation = [self expectationWithDescription:@"?????? Person"] ;
[Person loadPersonAsync:^(Person *person) {
NSLog(@"-_- --> %@ <-- -_-" ?? person.name) ;
//????????:
[expectation fulfill] ;
}] ;
//????????:
//1.0f'????????????????????:
[self waitForExpectationsWithTimeout:1.0f handler:^(NSError * _Nullable error) {
NSLog(@"?????? --> error = %@ <-- ??????" ?? error) ;
}] ;
}
@end
????Person.h ???
#import <Foundation/Foundation.h>
@interface Person : NSObject
//name:
@property (nonatomic?? copy) NSString *name ;
//age:
@property (nonatomic) NSInteger age ;
//????????:
+ (instancetype)personWithDictionary:(NSDictionary *)dictionary ;
//???????????:?????????????????????? block!
//??д block?????????: void (^) (Person *person)completion ;
+ (void)loadPersonAsync:(void (^) (Person *person))completion ;
@end
????Person.m ???
#import "Person.h"
@implementation Person
+ (instancetype)personWithDictionary:(NSDictionary *)dictionary {
//[[self alloc] init] ;?????? self ?????????????????????????? person ?????????????д?????????!
Person *obj = [[self alloc] init] ;
//KVC ????:
[obj setValuesForKeysWithDictionary:dictionary] ;
//???????????????????:
if (obj.age <= 0 || obj.age > 100) {
obj.age = 0 ;
}
return obj ;
}
//д????????:???????? title : boss ???????????:
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
+ (void)loadPersonAsync:(void (^)(Person *))completion {
dispatch_async(dispatch_get_global_queue(0?? 0)?? ^{
[NSThread sleepForTimeInterval:1.0f] ;
Person *person = [Person personWithDictionary:@{@"name":@"lisi" ?? @"age":@20}] ;
dispatch_async(dispatch_get_main_queue()?? ^{
//?? OC ??д????????????????д?????????????ж???????????????????????:
if (completion != nil) {
//???????person ??????????:
completion(person) ;
}
}) ;
}) ;
}
@end