forked from joecannatti/Objective-C-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AboutARC.m
103 lines (83 loc) · 2.82 KB
/
AboutARC.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// AboutAssertions.m
// ObjectiveCKoans
//
// Created by Curtis Schofield
// Copyright 2011 BlazingCloud, Curtis J Schofield
//
#if __has_feature(objc_arc)
// Extending NSObject
#import <Foundation/Foundation.h>
// Make reference to a Person and PhoneNumber before they exist
@class Person;
@class PhoneNumber;
@interface Person : NSObject
// Example : create the property thingy with 'strong' ARC property
//
// ARC Notes : http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html
//
// More on ARC from the compiler team : http://clang.llvm.org/docs/AutomaticReferenceCounting.html
//
// @property (nonatomic,strong) NSString *thingy;
- (void) setPhoneNumber:(PhoneNumber *)phoneNumber;
//@property PhoneNumber *phoneNumber;
@end
@interface PhoneNumber : NSObject
// We would explicitly tell the ARC system that we want a weak reference to Person
// ie: don't keep the Person around if it's only retained reference is weak
@property (nonatomic, weak) Person *owner;
@property NSString *areaCode;
@property NSString *digits;
@property NSString *countryCode;
- (id)initWithCountryCode:(NSString *)countryCode areaCode:(NSString *)areaCode digits:(NSString *)digits;
@end
@implementation Person
- (void) setPhoneNumber:(PhoneNumber *)phoneNumber {
self.phoneNumber = phoneNumber;
phoneNumber.owner = self;
}
@end
@implementation PhoneNumber
- (id)initWithCountryCode:(NSString *)countryCode areaCode:(NSString *)areaCode digits:(NSString *)digits
{
self = [super init];
if (self) {
self.countryCode = countryCode;
self.areaCode = areaCode;
self.digits = digits;
}
return self;
}
@end
#import "Kiwi.h"
SPEC_BEGIN(AboutARC)
describe(@"About Automatic Reference Counting", ^{
context(@"PhoneNumber",^{
__block PhoneNumber * phoneNumber;
beforeAll(^{
phoneNumber = [[PhoneNumber alloc]
initWithCountryCode:@"1"
areaCode:@"604"
digits:@"334-3244"];
});
it(@"can be initalized with area code and digits and countryCode without an owner", ^{
[[theValue(phoneNumber.areaCode) should] equal:theValue(@"604")];
[[theValue(phoneNumber.digits) should] equal:theValue(@"334-3244")];
[[theValue(phoneNumber.countryCode) should] equal:theValue(@"1")];
[phoneNumber.owner shouldBeNil];
});
it(@"can have an owner set",^{
Person *aPerson = [[Person alloc] init];
[phoneNumber setOwner:aPerson];
[phoneNumber.owner shouldNotBeNil];
[[phoneNumber.owner should] beIdenticalTo:aPerson];
});
it(@"arc disables retain",^{
// [phoneNumber retain];
// bypassing ARC will create memory leaks
//[phoneNumber performSelector:NSSelectorFromString(@"retain")];
});
});
});
SPEC_END
#endif