Last active
November 18, 2016 12:15
-
-
Save Grubas7/a2f9e04b38db1a6f4cd24c561fe30b02 to your computer and use it in GitHub Desktop.
Class initializer vs. instance initializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - class | |
#import <UIKit/UIKit.h> | |
@interface AGLabel : UILabel | |
+ (instancetype)magicLabel; | |
- (instancetype)initWithMagic; | |
@end | |
@implementation AGLabel | |
+ (instancetype)magicLabel { | |
return [[AGLabel alloc] init]; | |
} | |
- (instancetype)initWithMagic { | |
return [super init]; | |
} | |
@end | |
#pragma mark - tests | |
#import <XCTest/XCTest.h> | |
@interface AGLabelTests : XCTestCase | |
@end | |
- (void)testInstanceInitializer { | |
__weak id testingPointer = nil; | |
AGLabel *label = [[AGLabel alloc] initWithMagic]; | |
@autoreleasepool { | |
testingPointer = label; | |
XCTAssertNotNil(testingPointer, @"This will never happen, since we're still holding it."); | |
label = nil; | |
} | |
XCTAssertNil(label); | |
XCTAssertNil(testingPointer, @"2: Something didn't release %@ when it should have", testingPointer); | |
} | |
- (void)testClassInitializer { | |
__weak id testingPointer = nil; | |
AGLabel *label = [AGLabel magicLabel]; | |
@autoreleasepool { | |
testingPointer = label; | |
XCTAssertNotNil(testingPointer, @"This will never happen, since we're still holding it."); | |
label = nil; | |
} | |
XCTAssertNil(label); | |
XCTAssertNil(testingPointer, @"2: Something didn't release %@ when it should have", testingPointer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment