Skip to content

Commit 527d71b

Browse files
committed
better swift support (nullability, light generics) + disable init metod in a better way
1 parent 40442f5 commit 527d71b

File tree

8 files changed

+45
-25
lines changed

8 files changed

+45
-25
lines changed

FastImageCache/FastImageCache/FastImageCache/FICEntity.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#import "FICImports.h"
1010
@class FICImageFormat;
1111

12+
NS_ASSUME_NONNULL_BEGIN
13+
1214
typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextSize);
1315

1416
/**
@@ -52,7 +54,7 @@ typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextS
5254
@see FICImageFormat
5355
@see [FICImageCacheDelegate imageCache:wantsSourceImageForEntity:withFormatName:completionBlock:]
5456
*/
55-
- (NSURL *)sourceImageURLWithFormatName:(NSString *)formatName;
57+
- (nullable NSURL *)sourceImageURLWithFormatName:(NSString *)formatName;
5658

5759
/**
5860
Returns the drawing block for a specific image and format name.
@@ -73,14 +75,16 @@ typedef void (^FICEntityImageDrawingBlock)(CGContextRef context, CGSize contextS
7375
7476
@note This block will always be called from the serial dispatch queue used by the image cache.
7577
*/
76-
- (FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;
78+
- (nullable FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;
7779

7880
@optional
7981
/**
8082
Returns the image for a format
8183
8284
@param format The image format that identifies which image table is requesting the source image.
8385
*/
84-
- (UIImage *)imageForFormat:(FICImageFormat *)format;
86+
- (nullable UIImage *)imageForFormat:(FICImageFormat *)format;
8587

8688
@end
89+
90+
NS_ASSUME_NONNULL_END

FastImageCache/FastImageCache/FastImageCache/FICImageCache.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
@protocol FICEntity;
1414
@protocol FICImageCacheDelegate;
1515

16-
typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image);
17-
typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
16+
typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> _Nullable entity, NSString * _Nonnull formatName, UIImage * _Nullable image);
17+
typedef void (^FICImageRequestCompletionBlock)(UIImage * _Nullable sourceImage);
18+
19+
NS_ASSUME_NONNULL_BEGIN
1820

1921
/**
2022
`FICImageCache` is the primary class for managing and interacting with the image cache. Applications using the image cache create one or more `<FICImageFormat>`
@@ -98,7 +100,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
98100
99101
@note Once the image formats have been set, subsequent calls to this method will do nothing.
100102
*/
101-
- (void)setFormats:(NSArray *)formats;
103+
- (void)setFormats:(NSArray<FICImageFormat*> *)formats;
102104

103105
/**
104106
Returns an image format previously associated with the image cache.
@@ -107,7 +109,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
107109
108110
@return An image format with the name `formatName` or `nil` if no format with that name exists.
109111
*/
110-
- (FICImageFormat *)formatWithName:(NSString *)formatName;
112+
- (nullable FICImageFormat *)formatWithName:(NSString *)formatName;
111113

112114
/**
113115
Returns all the image formats of the same family previously associated with the image cache.
@@ -116,7 +118,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
116118
117119
@return An array of `<FICImageFormat>` objects whose family is `family` or `nil` if no format belongs to that family.
118120
*/
119-
- (NSArray *)formatsWithFamily:(NSString *)family;
121+
- (nullable NSArray<FICImageFormat *> *)formatsWithFamily:(NSString *)family;
120122

121123
///-----------------------------------------------
122124
/// @name Storing, Retrieving, and Deleting Images
@@ -141,7 +143,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
141143
142144
typedef void (^FICImageCacheCompletionBlock)(id <FICEntity> entity, NSString *formatName, UIImage *image)
143145
*/
144-
- (void)setImage:(UIImage *)image forEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock;
146+
- (void)setImage:(UIImage *)image forEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageCacheCompletionBlock)completionBlock;
145147

146148
/**
147149
Attempts to synchronously retrieve an image from the image cache.
@@ -168,7 +170,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
168170
@note You can always rely on the completion block being called. If an error occurs for any reason, the `image` parameter of the completion block will be `nil`. See
169171
<[FICImageCacheDelegate imageCache:errorDidOccurWithMessage:]> for information about being notified when errors occur.
170172
*/
171-
- (BOOL)retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock;
173+
- (BOOL)retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageCacheCompletionBlock)completionBlock;
172174

173175
/**
174176
Asynchronously retrieves an image from the image cache.
@@ -193,7 +195,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
193195
194196
@see [FICImageCache retrieveImageForEntity:withFormatName:completionBlock:]
195197
*/
196-
- (BOOL)asynchronouslyRetrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageCacheCompletionBlock)completionBlock;
198+
- (BOOL)asynchronouslyRetrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageCacheCompletionBlock)completionBlock;
197199

198200
/**
199201
Deletes an image from the image cache.
@@ -281,7 +283,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
281283
the URL returned by <[FICEntity sourceImageURLWithFormatName:]>, deserializing the image data when the request completes, and finally calling this method's completion
282284
block to provide the image cache with the source image.
283285
*/
284-
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(FICImageRequestCompletionBlock)completionBlock;
286+
- (void)imageCache:(FICImageCache *)imageCache wantsSourceImageForEntity:(id <FICEntity>)entity withFormatName:(NSString *)formatName completionBlock:(nullable FICImageRequestCompletionBlock)completionBlock;
285287

286288
/**
287289
This method is called on the delegate when the image cache has received an image retrieval cancellation request.
@@ -332,3 +334,5 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
332334
- (void)imageCache:(FICImageCache *)imageCache errorDidOccurWithMessage:(NSString *)errorMessage;
333335

334336
@end
337+
338+
NS_ASSUME_NONNULL_END

FastImageCache/FastImageCache/FastImageCache/FICImageFormat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef NS_ENUM(NSUInteger, FICImageFormatProtectionMode) {
3434
prevent the image cache from consuming too much disk space. Each `<FICImageTable>` managed by the image cache is associated with a single image format.
3535
*/
3636

37+
NS_ASSUME_NONNULL_BEGIN
3738
@interface FICImageFormat : NSObject <NSCopying>
3839

3940
///------------------------------
@@ -152,7 +153,7 @@ typedef NS_ENUM(NSUInteger, FICImageFormatProtectionMode) {
152153
@discussion Fast Image Cache automatically serializes the image formats that it uses to disk. If an image format ever changes, Fast Image Cache automatically detects the change and invalidates the
153154
image table associated with that image format. The image table is then recreated from the updated image format.
154155
*/
155-
@property (nonatomic, copy, readonly) NSDictionary *dictionaryRepresentation;
156+
@property (nonatomic, copy, readonly) NSDictionary<NSString*, id> *dictionaryRepresentation;
156157

157158
///-----------------------------------
158159
/// @name Initializing an Image Format
@@ -180,3 +181,4 @@ typedef NS_ENUM(NSUInteger, FICImageFormatProtectionMode) {
180181
+ (instancetype)formatWithName:(NSString *)name family:(NSString *)family imageSize:(CGSize)imageSize style:(FICImageFormatStyle)style maximumCount:(NSInteger)maximumCount devices:(FICImageFormatDevices)devices protectionMode:(FICImageFormatProtectionMode)protectionMode;
181182

182183
@end
184+
NS_ASSUME_NONNULL_END

FastImageCache/FastImageCache/FastImageCache/FICImageTable.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
@class FICImageTableEntry;
1616
@class FICImage;
1717

18+
NS_ASSUME_NONNULL_BEGIN
19+
1820
extern NSString *const FICImageTableEntryDataVersionKey;
1921
extern NSString *const FICImageTableScreenScaleKey;
2022

@@ -81,7 +83,9 @@ extern NSString *const FICImageTableScreenScaleKey;
8183
8284
@warning `FICImageTable` raises an exception if `imageFormat` is `nil`. `FICImageTable`'s implementation of `-init` simply calls through to this initializer, passing `nil` for `imageFormat`.
8385
*/
84-
- (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache;
86+
- (nullable instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache NS_DESIGNATED_INITIALIZER;
87+
-(instancetype) init __attribute__((unavailable("Invoke the designated initializer initWithFormat:imageCache: instead")));
88+
+(instancetype) new __attribute__((unavailable("Invoke the designated initializer initWithFormat:imageCache: instead")));
8589

8690
///------------------------------------------------
8791
/// @name Storing, Retrieving, and Deleting Entries
@@ -123,7 +127,7 @@ extern NSString *const FICImageTableScreenScaleKey;
123127
@note If either the entity UUID or the source image UUID doesn't match the corresponding UUIDs in the entry data, then something has changed. The entry data is deleted for the
124128
provided entity UUID, and `nil` is returned.
125129
*/
126-
- (UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSString *)sourceImageUUID preheatData:(BOOL)preheatData;
130+
- (nullable UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSString *)sourceImageUUID preheatData:(BOOL)preheatData;
127131

128132
/**
129133
Deletes image entry data in the image table.
@@ -164,3 +168,5 @@ extern NSString *const FICImageTableScreenScaleKey;
164168
- (void)reset;
165169

166170
@end
171+
172+
NS_ASSUME_NONNULL_END

FastImageCache/FastImageCache/FastImageCache/FICImageTable.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,6 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImag
233233
return self;
234234
}
235235

236-
- (instancetype)init {
237-
return [self initWithFormat:nil imageCache:nil];
238-
}
239-
240236
- (void)dealloc {
241237
if (_fileDescriptor >= 0) {
242238
close(_fileDescriptor);

FastImageCache/FastImageCache/FastImageCache/FICImageTableChunk.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import "FICImports.h"
1010

11+
NS_ASSUME_NONNULL_BEGIN
12+
1113
@class FICImageTable;
1214

1315
/**
@@ -57,6 +59,8 @@
5759
5860
@return A new image table chunk.
5961
*/
60-
- (instancetype)initWithFileDescriptor:(int)fileDescriptor index:(NSInteger)index length:(size_t)length;
62+
- (nullable instancetype)initWithFileDescriptor:(int)fileDescriptor index:(NSInteger)index length:(size_t)length;
63+
64+
@end
6165

62-
@end
66+
NS_ASSUME_NONNULL_END

FastImageCache/FastImageCache/FastImageCache/FICImageTableEntry.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import "FICImports.h"
1010

11+
NS_ASSUME_NONNULL_BEGIN
12+
1113
@class FICImageTableChunk;
1214
@class FICImageCache;
1315

@@ -83,7 +85,7 @@ typedef struct {
8385
8486
@return A new image table entry.
8587
*/
86-
- (instancetype)initWithImageTableChunk:(FICImageTableChunk *)imageTableChunk bytes:(void *)bytes length:(size_t)length;
88+
- (nullable instancetype)initWithImageTableChunk:(FICImageTableChunk *)imageTableChunk bytes:(void *)bytes length:(size_t)length;
8789

8890
/**
8991
Adds a block to be executed when this image table entry is deallocated.
@@ -122,3 +124,5 @@ typedef struct {
122124
+ (NSInteger)metadataVersion;
123125

124126
@end
127+
128+
NS_ASSUME_NONNULL_END

FastImageCache/FastImageCache/FastImageCache/FICUtilities.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
size_t FICByteAlign(size_t bytesPerRow, size_t alignment);
1212
size_t FICByteAlignForCoreAnimation(size_t bytesPerRow);
1313

14-
NSString * FICStringWithUUIDBytes(CFUUIDBytes UUIDBytes);
15-
CFUUIDBytes FICUUIDBytesWithString(NSString *string);
16-
CFUUIDBytes FICUUIDBytesFromMD5HashOfString(NSString *MD5Hash); // Useful for computing an entity's UUID from a URL, for example
14+
NSString * _Nullable FICStringWithUUIDBytes(CFUUIDBytes UUIDBytes);
15+
CFUUIDBytes FICUUIDBytesWithString(NSString * _Nonnull string);
16+
CFUUIDBytes FICUUIDBytesFromMD5HashOfString(NSString * _Nonnull MD5Hash); // Useful for computing an entity's UUID from a URL, for example
1717

0 commit comments

Comments
 (0)