Skip to content

Instantly share code, notes, and snippets.

@Grubas7
Last active November 18, 2016 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grubas7/a2f9e04b38db1a6f4cd24c561fe30b02 to your computer and use it in GitHub Desktop.
Save Grubas7/a2f9e04b38db1a6f4cd24c561fe30b02 to your computer and use it in GitHub Desktop.
Class initializer vs. instance initializer
#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