From a1f22c8fd8b43ec352c71801719fd41db7c5a3d2 Mon Sep 17 00:00:00 2001 From: Jonathan Crooke Date: Thu, 26 Feb 2015 15:25:44 +0100 Subject: [PATCH] Working --- .../project.pbxproj | 6 +++ .../ObjcAssociatedObjectHelpers.h | 4 +- .../ObjcAssociatedObjectHelpers.m | 2 +- UnitTests/Specs.m | 19 -------- UnitTests/WeakContainer.m | 44 +++++++++++++++++++ 5 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 UnitTests/WeakContainer.m diff --git a/ObjcAssociatedObjectHelpers.xcodeproj/project.pbxproj b/ObjcAssociatedObjectHelpers.xcodeproj/project.pbxproj index f26043f..c4d65c7 100644 --- a/ObjcAssociatedObjectHelpers.xcodeproj/project.pbxproj +++ b/ObjcAssociatedObjectHelpers.xcodeproj/project.pbxproj @@ -10,9 +10,11 @@ 1B75995371FA4BF5AF00B6D6 /* libPods-ObjcAssociatedObjectHelpersTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 510A0A5EEF4A43AEB1DFC61C /* libPods-ObjcAssociatedObjectHelpersTests.a */; }; 4918B9B2168498F000D4EAC4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49AEF0F61619F565009D9F9B /* Foundation.framework */; }; 4918B9C11684991B00D4EAC4 /* NSObject+AssociatedDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 499C34B1161AF68000BA2AC9 /* NSObject+AssociatedDictionary.m */; }; + 4959D45B1A9F5FF300F414FB /* WeakContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 49629DD21A9F5EE600BA8910 /* WeakContainer.m */; }; 495C4D151A59CDDC0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; }; 495C4D161A59D0CE0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; }; 495C4D171A59D0CF0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; }; + 49629DD31A9F5EE600BA8910 /* WeakContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 49629DD21A9F5EE600BA8910 /* WeakContainer.m */; }; 499C34C7161AFD7200BA2AC9 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49AEF0F61619F565009D9F9B /* Foundation.framework */; }; 499E28481A586A68004174F2 /* ObjcAssociatedObjectHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */; }; 49AD1B5716C282A10061BDC1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49AEF0F61619F565009D9F9B /* Foundation.framework */; }; @@ -41,6 +43,7 @@ 15F57830FA80C0B05DFFC53F /* Pods-ObjcAssociatedObjectHelpersLibTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ObjcAssociatedObjectHelpersLibTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ObjcAssociatedObjectHelpersLibTests/Pods-ObjcAssociatedObjectHelpersLibTests.debug.xcconfig"; sourceTree = ""; }; 1CA662592899DE55A0584B20 /* Pods-ObjcAssociatedObjectHelpersLibTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ObjcAssociatedObjectHelpersLibTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-ObjcAssociatedObjectHelpersLibTests/Pods-ObjcAssociatedObjectHelpersLibTests.release.xcconfig"; sourceTree = ""; }; 4918B9AE168498F000D4EAC4 /* ObjcAssociatedObjectHelpersLibTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ObjcAssociatedObjectHelpersLibTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 49629DD21A9F5EE600BA8910 /* WeakContainer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WeakContainer.m; sourceTree = ""; }; 499C34B0161AF68000BA2AC9 /* NSObject+AssociatedDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+AssociatedDictionary.h"; sourceTree = ""; }; 499C34B1161AF68000BA2AC9 /* NSObject+AssociatedDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+AssociatedDictionary.m"; sourceTree = ""; }; 499E28471A586A68004174F2 /* ObjcAssociatedObjectHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjcAssociatedObjectHelpers.m; sourceTree = ""; }; @@ -147,6 +150,7 @@ isa = PBXGroup; children = ( 49DEFFE518B615BC00E10224 /* Specs.m */, + 49629DD21A9F5EE600BA8910 /* WeakContainer.m */, 49DEFFE818B6172E00E10224 /* TestClass.h */, 49DEFFE918B6172E00E10224 /* TestClass.m */, ); @@ -404,6 +408,7 @@ files = ( 495C4D161A59D0CE0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */, 49BC3AB818B61B7C00679CF8 /* TestClass.m in Sources */, + 49629DD31A9F5EE600BA8910 /* WeakContainer.m in Sources */, 49DEFFE618B615BC00E10224 /* Specs.m in Sources */, 4918B9C11684991B00D4EAC4 /* NSObject+AssociatedDictionary.m in Sources */, ); @@ -431,6 +436,7 @@ files = ( 495C4D171A59D0CF0039DB95 /* ObjcAssociatedObjectHelpers.m in Sources */, 49BC3AB918B61B7D00679CF8 /* TestClass.m in Sources */, + 4959D45B1A9F5FF300F414FB /* WeakContainer.m in Sources */, 49DEFFE718B615BC00E10224 /* Specs.m in Sources */, 49BF24A8161B0805000B1590 /* NSObject+AssociatedDictionary.m in Sources */, ); diff --git a/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.h b/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.h index 1a23a68..ad4de8c 100644 --- a/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.h +++ b/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.h @@ -126,7 +126,7 @@ static void* getterName##Key = _OBJC_ASC_QUOTE(getterName); \ #define SYNTHESIZE_ASC_OBJ_WEAK_BLOCK(getterName, setterName, getterBlock, setterBlock) \ static void* getterName##Key = _OBJC_ASC_QUOTE(getterName); \ - (void)setterName:(id)value { \ - id wrapped = [__ObjCAscWeakContainer wrapObject:setterBlock(value)]; \ + id wrapped = [__ObjCAscWeakContainer wrapObject:value]; \ @synchronized(self) { \ _OBJC_ASC_WRAP_KVO_SETTER(getterName, objc_setAssociatedObject(self, getterName##Key, wrapped, OBJC_ASSOCIATION_RETAIN)); \ } \ @@ -136,7 +136,7 @@ static void* getterName##Key = _OBJC_ASC_QUOTE(getterName); \ @synchronized(self) { \ wrapped = objc_getAssociatedObject(self, getterName##Key); \ }; \ - return getterBlock(wrapped._object); \ + return wrapped._object; \ } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.m b/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.m index 52ba0c6..1de6bc7 100644 --- a/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.m +++ b/ObjcAssociatedObjectHelpers/ObjcAssociatedObjectHelpers.m @@ -28,7 +28,7 @@ @implementation __ObjCAscWeakContainer + (instancetype)wrapObject:(id)object { __ObjCAscWeakContainer *wrapper = [[self alloc] init]; -// wrapper._object = object; + wrapper._object = object; return wrapper; } @end diff --git a/UnitTests/Specs.m b/UnitTests/Specs.m index 3869206..94a2dc6 100644 --- a/UnitTests/Specs.m +++ b/UnitTests/Specs.m @@ -265,24 +265,5 @@ }); }); -describe(@"weak container", ^{ - - it(@"should get and set a value as normal", ^{ - NSMutableArray *array = [NSMutableArray arrayWithObject:@"foo"]; - testObject.weakObject = array.firstObject; - expect(testObject.weakObject).notTo.beNil(); - expect(testObject.weakObject == array.firstObject).to.beTruthy(); - }); - - it(@"should nil the weak property", ^{ - NSMutableArray *array = [NSMutableArray arrayWithObject:@"foo"]; - testObject.weakObject = array.firstObject; - expect(testObject.weakObject).notTo.beNil(); - expect(testObject.weakObject == array.firstObject).to.beTruthy(); - [array removeAllObjects]; - expect(testObject.weakObject).to.beNil(); - }); -}); - SpecEnd diff --git a/UnitTests/WeakContainer.m b/UnitTests/WeakContainer.m new file mode 100644 index 0000000..145020c --- /dev/null +++ b/UnitTests/WeakContainer.m @@ -0,0 +1,44 @@ +// +// WeakContainer.m +// ObjcAssociatedObjectHelpers +// +// Created by Jonathan Crooke on 26/02/2015. +// Copyright (c) 2015 jbsoft. All rights reserved. +// + +#import +#import "ObjcAssociatedObjectHelpers.h" +#import "TestClass.h" + +@interface WeakContainer : XCTestCase +@property (nonatomic, strong) TestClass *testObject; +@property (nonatomic, strong) NSArray *strongContainer; +@end + +@implementation WeakContainer + +- (void)testGetAndSetAValueAsNormal { + XCTAssertNotNil(self.testObject.weakObject); + XCTAssertEqual(self.testObject.weakObject, self.strongContainer.firstObject); +} + +- (void)testNilOfWeakProperty { + XCTestExpectation *expectation = [self expectationWithDescription:@"Weak property becomes nil"]; + self.strongContainer = nil; + dispatch_async(dispatch_get_main_queue(), ^{ + XCTAssertNil(self.testObject.weakObject); + [expectation fulfill]; + }); + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + +#pragma mark - + +- (void)setUp { + [super setUp]; + self.testObject = [[TestClass alloc] init]; + self.strongContainer = @[[[NSObject alloc] init]]; + self.testObject.weakObject = self.strongContainer.firstObject; +} + +@end