浏览代码

swift api compatibility

ibireme 9 年之前
父节点
当前提交
5f6c6b7092
共有 3 个文件被更改,包括 40 次插入27 次删除
  1. 19 15
      YYModel/NSObject+YYModel.h
  2. 16 12
      YYModel/YYClassInfo.h
  3. 5 0
      YYModel/YYClassInfo.m

+ 19 - 15
YYModel/NSObject+YYModel.h

@@ -11,6 +11,8 @@
 
 
 #import <Foundation/Foundation.h>
 #import <Foundation/Foundation.h>
 
 
+NS_ASSUME_NONNULL_BEGIN
+
 /**
 /**
  Provide some data-model method:
  Provide some data-model method:
  
  
@@ -73,7 +75,7 @@
  
  
  @return A new instance created from the json, or nil if an error occurs.
  @return A new instance created from the json, or nil if an error occurs.
  */
  */
-+ (instancetype)yy_modelWithJSON:(id)json;
++ (nullable instancetype)yy_modelWithJSON:(id)json;
 
 
 /**
 /**
  Creates and returns a new instance of the receiver from a key-value dictionary.
  Creates and returns a new instance of the receiver from a key-value dictionary.
@@ -94,7 +96,7 @@
      `NSValue` -> struct or union, such as CGRect, CGSize, ...
      `NSValue` -> struct or union, such as CGRect, CGSize, ...
      `NSString` -> SEL, Class.
      `NSString` -> SEL, Class.
  */
  */
-+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary;
++ (nullable instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary;
 
 
 /**
 /**
  Set the receiver's properties with a json object.
  Set the receiver's properties with a json object.
@@ -138,7 +140,7 @@
  If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it just convert
  If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it just convert
  the inner object to json object.
  the inner object to json object.
  */
  */
-- (id)yy_modelToJSONObject;
+- (nullable id)yy_modelToJSONObject;
 
 
 /**
 /**
  Generate a json string's data from the receiver's properties.
  Generate a json string's data from the receiver's properties.
@@ -149,7 +151,7 @@
  If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it will also convert the 
  If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it will also convert the 
  inner object to json string.
  inner object to json string.
  */
  */
-- (NSData *)yy_modelToJSONData;
+- (nullable NSData *)yy_modelToJSONData;
 
 
 /**
 /**
  Generate a json string from the receiver's properties.
  Generate a json string from the receiver's properties.
@@ -160,14 +162,14 @@
  If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it will also convert the 
  If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it will also convert the 
  inner object to json string.
  inner object to json string.
  */
  */
-- (NSString *)yy_modelToJSONString;
+- (nullable NSString *)yy_modelToJSONString;
 
 
 /**
 /**
  Copy a instance with the receiver's properties.
  Copy a instance with the receiver's properties.
  
  
  @return A copied instance, or nil if an error occurs.
  @return A copied instance, or nil if an error occurs.
  */
  */
-- (id)yy_modelCopy;
+- (nullable id)yy_modelCopy;
 
 
 /**
 /**
  Encode the receiver's properties to a coder.
  Encode the receiver's properties to a coder.
@@ -227,7 +229,7 @@
  
  
  @return A array, or nil if an error occurs.
  @return A array, or nil if an error occurs.
  */
  */
-+ (NSArray *)yy_modelArrayWithClass:(Class)cls json:(id)json;
++ (nullable NSArray *)yy_modelArrayWithClass:(Class)cls json:(id)json;
 
 
 @end
 @end
 
 
@@ -248,7 +250,7 @@
  
  
  @return A array, or nil if an error occurs.
  @return A array, or nil if an error occurs.
  */
  */
-+ (NSDictionary *)yy_modelDictionaryWithClass:(Class)cls json:(id)json;
++ (nullable NSDictionary *)yy_modelDictionaryWithClass:(Class)cls json:(id)json;
 @end
 @end
 
 
 
 
@@ -298,7 +300,7 @@
  
  
  @return A custom mapper for properties.
  @return A custom mapper for properties.
  */
  */
-+ (NSDictionary *)modelCustomPropertyMapper;
++ (NSDictionary<NSString *, id> *)modelCustomPropertyMapper;
 
 
 /**
 /**
  The generic class mapper for container properties.
  The generic class mapper for container properties.
@@ -327,7 +329,7 @@
  
  
  @return A class mapper.
  @return A class mapper.
  */
  */
-+ (NSDictionary *)modelContainerPropertyGenericClass;
++ (NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass;
 
 
 /**
 /**
  If you need to create instances of different classes during json->object transform,
  If you need to create instances of different classes during json->object transform,
@@ -361,23 +363,23 @@
  @return Class to create from this dictionary, `nil` to use current class.
  @return Class to create from this dictionary, `nil` to use current class.
 
 
  */
  */
-+ (Class)modelCustomClassForDictionary:(NSDictionary*)dictionary;
++ (nullable Class)modelCustomClassForDictionary:(NSDictionary *)dictionary;
 
 
 /**
 /**
  All the properties in blacklist will be ignored in model transform process.
  All the properties in blacklist will be ignored in model transform process.
  Returns nil to ignore this feature.
  Returns nil to ignore this feature.
  
  
- @return An array of property's name (Array<NSString>).
+ @return An array of property's name.
  */
  */
-+ (NSArray *)modelPropertyBlacklist;
++ (NSArray<NSString *> *)modelPropertyBlacklist;
 
 
 /**
 /**
  If a property is not in the whitelist, it will be ignored in model transform process.
  If a property is not in the whitelist, it will be ignored in model transform process.
  Returns nil to ignore this feature.
  Returns nil to ignore this feature.
  
  
- @return An array of property's name (Array<NSString>).
+ @return An array of property's name.
  */
  */
-+ (NSArray *)modelPropertyWhitelist;
++ (NSArray<NSString *> *)modelPropertyWhitelist;
 
 
 /**
 /**
  If the default json-to-model transform does not fit to your model object, implement
  If the default json-to-model transform does not fit to your model object, implement
@@ -410,3 +412,5 @@
 - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic;
 - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic;
 
 
 @end
 @end
+
+NS_ASSUME_NONNULL_END

+ 16 - 12
YYModel/YYClassInfo.h

@@ -12,6 +12,8 @@
 #import <Foundation/Foundation.h>
 #import <Foundation/Foundation.h>
 #import <objc/runtime.h>
 #import <objc/runtime.h>
 
 
+NS_ASSUME_NONNULL_BEGIN
+
 /**
 /**
  Type encoding's type.
  Type encoding's type.
  */
  */
@@ -104,7 +106,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
 @property (nonatomic, assign, readonly) IMP imp;                        ///< method's implementation
 @property (nonatomic, assign, readonly) IMP imp;                        ///< method's implementation
 @property (nonatomic, strong, readonly) NSString *typeEncoding;         ///< method's parameter and return types
 @property (nonatomic, strong, readonly) NSString *typeEncoding;         ///< method's parameter and return types
 @property (nonatomic, strong, readonly) NSString *returnTypeEncoding;   ///< return value's type
 @property (nonatomic, strong, readonly) NSString *returnTypeEncoding;   ///< return value's type
-@property (nonatomic, strong, readonly) NSArray *argumentTypeEncodings; ///< array of arguments' type
+@property (nonatomic, strong, readonly) NSArray<NSString *> *argumentTypeEncodings; ///< array of arguments' type
 
 
 /**
 /**
  Creates and returns a method info object.
  Creates and returns a method info object.
@@ -125,7 +127,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
 @property (nonatomic, assign, readonly) YYEncodingType type;      ///< property's type
 @property (nonatomic, assign, readonly) YYEncodingType type;      ///< property's type
 @property (nonatomic, strong, readonly) NSString *typeEncoding;   ///< property's encoding value
 @property (nonatomic, strong, readonly) NSString *typeEncoding;   ///< property's encoding value
 @property (nonatomic, strong, readonly) NSString *ivarName;       ///< property's ivar name
 @property (nonatomic, strong, readonly) NSString *ivarName;       ///< property's ivar name
-@property (nonatomic, assign, readonly) Class cls;                ///< may be nil
+@property (nullable, nonatomic, assign, readonly) Class cls;      ///< may be nil
 @property (nonatomic, assign, readonly) SEL getter;               ///< getter (nonnull)
 @property (nonatomic, assign, readonly) SEL getter;               ///< getter (nonnull)
 @property (nonatomic, assign, readonly) SEL setter;               ///< setter (nonnull)
 @property (nonatomic, assign, readonly) SEL setter;               ///< setter (nonnull)
 
 
@@ -143,15 +145,15 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
  Class information for a class.
  Class information for a class.
  */
  */
 @interface YYClassInfo : NSObject
 @interface YYClassInfo : NSObject
-@property (nonatomic, assign, readonly) Class cls;      ///< class object
-@property (nonatomic, assign, readonly) Class superCls; ///< super class object
-@property (nonatomic, assign, readonly) Class metaCls;  ///< class's meta class object
-@property (nonatomic, assign, readonly) BOOL isMeta;    ///< whether this class is meta class
+@property (nonatomic, assign, readonly) Class cls; ///< class object
+@property (nullable, nonatomic, assign, readonly) Class superCls; ///< super class object
+@property (nullable, nonatomic, assign, readonly) Class metaCls;  ///< class's meta class object
+@property (nonatomic, assign, readonly) BOOL isMeta; ///< whether this class is meta class
 @property (nonatomic, strong, readonly) NSString *name; ///< class name
 @property (nonatomic, strong, readonly) NSString *name; ///< class name
-@property (nonatomic, strong, readonly) YYClassInfo *superClassInfo; ///< super class's class info
-@property (nonatomic, strong, readonly) NSDictionary *ivarInfos;     ///< key:NSString(ivar),     value:YYClassIvarInfo
-@property (nonatomic, strong, readonly) NSDictionary *methodInfos;   ///< key:NSString(selector), value:YYClassMethodInfo
-@property (nonatomic, strong, readonly) NSDictionary *propertyInfos; ///< key:NSString(property), value:YYClassPropertyInfo
+@property (nullable, nonatomic, strong, readonly) YYClassInfo *superClassInfo; ///< super class's class info
+@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassIvarInfo *> *ivarInfos; ///< ivars
+@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassMethodInfo *> *methodInfos; ///< methods
+@property (nullable, nonatomic, strong, readonly) NSDictionary<NSString *, YYClassPropertyInfo *> *propertyInfos; ///< properties
 
 
 /**
 /**
  If the class is changed (for example: you add a method to this class with
  If the class is changed (for example: you add a method to this class with
@@ -171,7 +173,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
  @param cls A class.
  @param cls A class.
  @return A class info, or nil if an error occurs.
  @return A class info, or nil if an error occurs.
  */
  */
-+ (instancetype)classInfoWithClass:(Class)cls;
++ (nullable instancetype)classInfoWithClass:(Class)cls;
 
 
 /**
 /**
  Get the class info of a specified Class.
  Get the class info of a specified Class.
@@ -182,6 +184,8 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
  @param className A class name.
  @param className A class name.
  @return A class info, or nil if an error occurs.
  @return A class info, or nil if an error occurs.
  */
  */
-+ (instancetype)classInfoWithClassName:(NSString *)className;
++ (nullable instancetype)classInfoWithClassName:(NSString *)className;
 
 
 @end
 @end
+
+NS_ASSUME_NONNULL_END

+ 5 - 0
YYModel/YYClassInfo.m

@@ -296,6 +296,11 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding) {
         }
         }
         free(ivars);
         free(ivars);
     }
     }
+    
+    if (!_ivarInfos) _ivarInfos = @{};
+    if (!_methodInfos) _methodInfos = @{};
+    if (!_propertyInfos) _propertyInfos = @{};
+    
     _needUpdate = NO;
     _needUpdate = NO;
 }
 }