From 3267654d1d24fdb22bfb61aaa62b2d2e511554f0 Mon Sep 17 00:00:00 2001 From: Zac West Date: Tue, 4 Aug 2020 11:12:00 -0700 Subject: [PATCH] Fix compiling in Xcode 12.0b4 This beta added a diagnostic for passing more specific types to `id`-typed values: > Fixed type checking for block parameters using id with protocols. The compiler now emits an error for method calls with a block that uses parameters more specific than arguments with which it will be called. (57980961) --- Source/PINCache.m | 6 ++--- Source/PINDiskCache.m | 38 ++++++++++++++++++++++------- Source/PINMemoryCache.m | 54 ++++++++++++++++++++++++++++++++++------- 3 files changed, 77 insertions(+), 21 deletions(-) diff --git a/Source/PINCache.m b/Source/PINCache.m index 0bfdf8b1..f574e0c0 100644 --- a/Source/PINCache.m +++ b/Source/PINCache.m @@ -118,7 +118,7 @@ - (void)objectForKeyAsync:(NSString *)key completion:(PINCacheObjectBlock)block return; [self.operationQueue scheduleOperation:^{ - [self->_memoryCache objectForKeyAsync:key completion:^(PINMemoryCache *memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + [self->_memoryCache objectForKeyAsync:key completion:^(id memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { if (memoryCacheObject) { // Update file modification date. TODO: make this a separate method? [self->_diskCache fileURLForKeyAsync:memoryCacheKey completion:^(NSString * _Nonnull key, NSURL * _Nullable fileURL) {}]; @@ -271,8 +271,8 @@ - (NSUInteger)diskByteCount { __block NSUInteger byteCount = 0; - [_diskCache synchronouslyLockFileAccessWhileExecutingBlock:^(PINDiskCache *diskCache) { - byteCount = diskCache.byteCount; + [_diskCache synchronouslyLockFileAccessWhileExecutingBlock:^(id diskCache) { + byteCount = ((PINDiskCache *)diskCache).byteCount; }]; return byteCount; diff --git a/Source/PINDiskCache.m b/Source/PINDiskCache.m index 66f03569..40e606d8 100644 --- a/Source/PINDiskCache.m +++ b/Source/PINDiskCache.m @@ -677,7 +677,7 @@ - (BOOL)removeFileAndExecuteBlocksForKey:(NSString *)key return NO; } - PINCacheObjectBlock willRemoveObjectBlock = _willRemoveObjectBlock; + PINDiskCacheObjectBlock willRemoveObjectBlock = _willRemoveObjectBlock; if (willRemoveObjectBlock) { [self unlock]; willRemoveObjectBlock(self, key, nil); @@ -698,7 +698,7 @@ - (BOOL)removeFileAndExecuteBlocksForKey:(NSString *)key [_metadata removeObjectForKey:key]; - PINCacheObjectBlock didRemoveObjectBlock = _didRemoveObjectBlock; + PINDiskCacheObjectBlock didRemoveObjectBlock = _didRemoveObjectBlock; if (didRemoveObjectBlock) { [self unlock]; _didRemoveObjectBlock(self, key, nil); @@ -1207,7 +1207,7 @@ - (void)setObject:(id )object forKey:(NSString *)key withAgeLimit:(NST } [self lockForWriting]; - PINCacheObjectBlock willAddObjectBlock = self->_willAddObjectBlock; + PINDiskCacheObjectBlock willAddObjectBlock = self->_willAddObjectBlock; if (willAddObjectBlock) { [self unlock]; willAddObjectBlock(self, key, object); @@ -1251,7 +1251,7 @@ - (void)setObject:(id )object forKey:(NSString *)key withAgeLimit:(NST fileURL = nil; } - PINCacheObjectBlock didAddObjectBlock = self->_didAddObjectBlock; + PINDiskCacheObjectBlock didAddObjectBlock = self->_didAddObjectBlock; if (didAddObjectBlock) { [self unlock]; didAddObjectBlock(self, key, object); @@ -1663,29 +1663,49 @@ - (void)removeObjectForKey:(NSString *)key block:(nullable PINDiskCacheObjectBlo - (void)trimToDate:(NSDate *)date block:(nullable PINDiskCacheBlock)block { - [self trimToDateAsync:date completion:block]; + [self trimToDateAsync:date completion:^(id diskCache) { + if (block) { + block((PINDiskCache *)diskCache); + } + }]; } - (void)trimToSize:(NSUInteger)byteCount block:(nullable PINDiskCacheBlock)block { - [self trimToSizeAsync:byteCount completion:block]; + [self trimToSizeAsync:byteCount completion:^(id diskCache) { + if (block) { + block((PINDiskCache *)diskCache); + } + }]; } - (void)trimToSizeByDate:(NSUInteger)byteCount block:(nullable PINDiskCacheBlock)block { - [self trimToSizeAsync:byteCount completion:block]; + [self trimToSizeAsync:byteCount completion:^(id diskCache) { + if (block) { + block((PINDiskCache *)diskCache); + } + }]; } - (void)removeAllObjects:(nullable PINDiskCacheBlock)block { - [self removeAllObjectsAsync:block]; + [self removeAllObjectsAsync:^(id diskCache) { + if (block) { + block((PINDiskCache *)diskCache); + } + }]; } - (void)enumerateObjectsWithBlock:(PINDiskCacheFileURLBlock)block completionBlock:(nullable PINDiskCacheBlock)completionBlock { [self enumerateObjectsWithBlockAsync:^(NSString * _Nonnull key, NSURL * _Nullable fileURL, BOOL * _Nonnull stop) { block(key, fileURL); - } completionBlock:completionBlock]; + } completionBlock:^(id diskCache) { + if (completionBlock) { + completionBlock((PINDiskCache *)diskCache); + } + }]; } - (void)setTtlCache:(BOOL)ttlCache diff --git a/Source/PINMemoryCache.m b/Source/PINMemoryCache.m index 725b91ea..9f64c293 100644 --- a/Source/PINMemoryCache.m +++ b/Source/PINMemoryCache.m @@ -833,42 +833,74 @@ - (void)containsObjectForKey:(NSString *)key block:(PINMemoryCacheContainmentBlo - (void)objectForKey:(NSString *)key block:(nullable PINMemoryCacheObjectBlock)block { - [self objectForKeyAsync:key completion:block]; + [self objectForKeyAsync:key completion:^(id memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + if (block) { + block((PINMemoryCache *)memoryCache, memoryCacheKey, memoryCacheObject); + } + }]; } - (void)setObject:(id)object forKey:(NSString *)key block:(nullable PINMemoryCacheObjectBlock)block { - [self setObjectAsync:object forKey:key completion:block]; + [self setObjectAsync:object forKey:key completion:^(id memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + if (block) { + block((PINMemoryCache *)memoryCache, memoryCacheKey, memoryCacheObject); + } + }]; } - (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(nullable PINMemoryCacheObjectBlock)block { - [self setObjectAsync:object forKey:key withCost:cost completion:block]; + [self setObjectAsync:object forKey:key withCost:cost completion:^(id memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + if (block) { + block((PINMemoryCache *)memoryCache, memoryCacheKey, memoryCacheObject); + } + }]; } - (void)removeObjectForKey:(NSString *)key block:(nullable PINMemoryCacheObjectBlock)block { - [self removeObjectForKeyAsync:key completion:block]; + [self removeObjectForKeyAsync:key completion:^(id memoryCache, NSString *memoryCacheKey, id memoryCacheObject) { + if (block) { + block((PINMemoryCache *)memoryCache, memoryCacheKey, memoryCacheObject); + } + }]; } - (void)trimToDate:(NSDate *)date block:(nullable PINMemoryCacheBlock)block { - [self trimToDateAsync:date completion:block]; + [self trimToDateAsync:date completion:^(id memoryCache) { + if (block) { + block((PINMemoryCache *)memoryCache); + } + }]; } - (void)trimToCost:(NSUInteger)cost block:(nullable PINMemoryCacheBlock)block { - [self trimToCostAsync:cost completion:block]; + [self trimToCostAsync:cost completion:^(id memoryCache) { + if (block) { + block((PINMemoryCache *)memoryCache); + } + }]; } - (void)trimToCostByDate:(NSUInteger)cost block:(nullable PINMemoryCacheBlock)block { - [self trimToCostByDateAsync:cost completion:block]; + [self trimToCostByDateAsync:cost completion:^(id memoryCache) { + if (block) { + block((PINMemoryCache *)memoryCache); + } + }]; } - (void)removeAllObjects:(nullable PINMemoryCacheBlock)block { - [self removeAllObjectsAsync:block]; + [self removeAllObjectsAsync:^(id memoryCache) { + if (block) { + block((PINMemoryCache *)memoryCache); + } + }]; } - (void)enumerateObjectsWithBlock:(PINMemoryCacheObjectBlock)block completionBlock:(nullable PINMemoryCacheBlock)completionBlock @@ -878,7 +910,11 @@ - (void)enumerateObjectsWithBlock:(PINMemoryCacheObjectBlock)block completionBlo PINMemoryCache *memoryCache = (PINMemoryCache *)cache; block(memoryCache, key, object); } - } completionBlock:completionBlock]; + } completionBlock:^(id memoryCache) { + if (completionBlock) { + completionBlock((PINMemoryCache *)memoryCache); + } + }]; } - (void)setTtlCache:(BOOL)ttlCache