Skip to content

Commit

Permalink
Removed solution, added notes and hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Curtis Schofield authored and Curtis Schofield committed Nov 10, 2011
1 parent 7b6d512 commit b9842a8
Showing 1 changed file with 26 additions and 50 deletions.
76 changes: 26 additions & 50 deletions AboutARC.m
@@ -1,68 +1,44 @@
//
// AboutAssertions.m
// ObjectiveCKoans
//
// Created by Curtis Schofield
// Copyright 2011 BlazingCloud, Curtis J Schofield
//

#if __has_feature(objc_arc) #if __has_feature(objc_arc)
#define INIT_FAILED NSLog(@"INIT_FAILED %s:%d", __FILE__, __LINE__);
// Extending NSObject
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>


// Make reference to a Person before it is defined // Make reference to a Person and PhoneNumber before they exist
@class Person; @class Person;
@class PhoneNumber;


@interface PhoneNumber : NSObject @interface Person : NSObject
@property (nonatomic,strong) NSString *countryCode; // Example : create the property thingy with 'strong' ARC property
@property (nonatomic,strong) NSString *areaCode; //
@property (nonatomic,strong) NSString *digits; // ARC Notes : http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html
@property (nonatomic,weak) Person *owner; //
@end // More on ARC from the compiler team : http://clang.llvm.org/docs/AutomaticReferenceCounting.html

//
@implementation PhoneNumber // @property (nonatomic,strong) NSString *thingy;
@synthesize countryCode,areaCode,digits,owner;
-(PhoneNumber*) initWithCountryCode:(NSString *) _countryCode
areaCode: (NSString *) _areaCode
digits: (NSString *) _digits {
self = [super init];
if (self) { // alloc succeeds
self.countryCode = _countryCode;
self.areaCode = _areaCode;
self.digits = _digits;
self.owner = nil;
} else {
INIT_FAILED
}

return self;

}

@end

@interface Person : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSNumber *yearOfBirth;
@property (nonatomic, strong) Person *spouse;
@property (nonatomic, strong) PhoneNumber *phoneNumber;

@end @end


@implementation Person @interface PhoneNumber : NSObject
@synthesize firstName, lastName, yearOfBirth, spouse, phoneNumber; // 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
- (void)setPhoneNumber:(PhoneNumber *) _phoneNumber { @property (nonatomic, weak) Person *owner;
phoneNumber = _phoneNumber;
phoneNumber.owner = self;
}


@end @end


#import "Kiwi.h" #import "Kiwi.h"


SPEC_BEGIN(AboutARC) SPEC_BEGIN(AboutARC)


describe(@"About Automatic Reference Counting", ^{ describe(@"About Automatic Reference Counting", ^{


context(@"PhoneNumber",^{ context(@"PhoneNumber",^{
__block PhoneNumber * phoneNumber; __block PhoneNumber * phoneNumber;

beforeAll(^{ beforeAll(^{
phoneNumber = [[PhoneNumber alloc] phoneNumber = [[PhoneNumber alloc]
initWithCountryCode:@"1" initWithCountryCode:@"1"
Expand Down Expand Up @@ -104,4 +80,4 @@ - (void)setPhoneNumber:(PhoneNumber *) _phoneNumber {
}); });
}); });
SPEC_END SPEC_END
#endif #endif

0 comments on commit b9842a8

Please sign in to comment.