Change the id<NSCoding> into id<NSObject, NSCoding>, to support directlly usage like isKindOfClass

This commit is contained in:
DreamPiggy 2019-11-25 17:05:27 +08:00
parent 46b0c4bae8
commit 9f470954c4
3 changed files with 6 additions and 5 deletions

View File

@ -200,8 +200,8 @@
[self _storeImageDataToDisk:data forKey:key];
if (image) {
// Check extended data
id<NSCoding> extendedObject = image.sd_extendedObject;
if (extendedObject) {
id extendedObject = image.sd_extendedObject;
if ([extendedObject conformsToProtocol:@protocol(NSCoding)]) {
NSData *extendedData = [NSKeyedArchiver archivedDataWithRootObject:extendedObject];
if (extendedData) {
[self.diskCache setExtendedData:extendedData forKey:key];

View File

@ -16,7 +16,8 @@
Read and Write the extended object and bind it to the image. Which can hold some extra metadata like Image's scale factor, URL rich link, date, etc.
The extended object should conforms to NSCoding, which we use `NSKeyedArchiver` and `NSKeyedUnarchiver` to archive it to data, and write to disk cache.
@note The disk cache preserve both of the data and extended data with the same cache key. For manual query, use the `SDDiskCache` protocol method `extendedDataForKey:` instead.
@note You can specify arbitrary object conforms to NSCoding (NSObject protocol here is used to support object like `dispatch_data_t`, which is not NSObject subclass). If you load image from disk cache, you should check the extended object class to avoid corrupted data.
*/
@property (nonatomic, strong, nullable) id<NSCoding> sd_extendedObject;
@property (nonatomic, strong, nullable) id<NSObject, NSCoding> sd_extendedObject;
@end

View File

@ -12,11 +12,11 @@
@implementation UIImage (ExtendedCacheData)
- (id<NSCoding>)sd_extendedObject {
- (id<NSObject, NSCoding>)sd_extendedObject {
return objc_getAssociatedObject(self, @selector(sd_extendedObject));
}
- (void)setSd_extendedObject:(id<NSCoding>)sd_extendedObject {
- (void)setSd_extendedObject:(id<NSObject, NSCoding>)sd_extendedObject {
objc_setAssociatedObject(self, @selector(sd_extendedObject), sd_extendedObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}