Skip to content

Commit

Permalink
swift api compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ibireme committed Feb 26, 2016
1 parent 86db8e1 commit 5f6c6b7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
34 changes: 19 additions & 15 deletions YYModel/NSObject+YYModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
Provide some data-model method:
Expand Down Expand Up @@ -73,7 +75,7 @@
@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.
Expand All @@ -94,7 +96,7 @@
`NSValue` -> struct or union, such as CGRect, CGSize, ...
`NSString` -> SEL, Class.
*/
+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary;
+ (nullable instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary;

/**
Set the receiver's properties with a json object.
Expand Down Expand Up @@ -138,7 +140,7 @@
If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it just convert
the inner object to json object.
*/
- (id)yy_modelToJSONObject;
- (nullable id)yy_modelToJSONObject;

/**
Generate a json string's data from the receiver's properties.
Expand All @@ -149,7 +151,7 @@
If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it will also convert the
inner object to json string.
*/
- (NSData *)yy_modelToJSONData;
- (nullable NSData *)yy_modelToJSONData;

/**
Generate a json string from the receiver's properties.
Expand All @@ -160,14 +162,14 @@
If the reciver is `NSArray`, `NSDictionary` or `NSSet`, it will also convert the
inner object to json string.
*/
- (NSString *)yy_modelToJSONString;
- (nullable NSString *)yy_modelToJSONString;

/**
Copy a instance with the receiver's properties.
@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.
Expand Down Expand Up @@ -227,7 +229,7 @@
@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

Expand All @@ -248,7 +250,7 @@
@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


Expand Down Expand Up @@ -298,7 +300,7 @@
@return A custom mapper for properties.
*/
+ (NSDictionary *)modelCustomPropertyMapper;
+ (NSDictionary<NSString *, id> *)modelCustomPropertyMapper;

/**
The generic class mapper for container properties.
Expand Down Expand Up @@ -327,7 +329,7 @@
@return A class mapper.
*/
+ (NSDictionary *)modelContainerPropertyGenericClass;
+ (NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass;

/**
If you need to create instances of different classes during json->object transform,
Expand Down Expand Up @@ -361,23 +363,23 @@
@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.
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.
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
Expand Down Expand Up @@ -410,3 +412,5 @@
- (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic;

@end

NS_ASSUME_NONNULL_END
28 changes: 16 additions & 12 deletions YYModel/YYClassInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

NS_ASSUME_NONNULL_BEGIN

/**
Type encoding's type.
*/
Expand Down Expand Up @@ -104,7 +106,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
@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 *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.
Expand All @@ -125,7 +127,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
@property (nonatomic, assign, readonly) YYEncodingType type; ///< property's type
@property (nonatomic, strong, readonly) NSString *typeEncoding; ///< property's encoding value
@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 setter; ///< setter (nonnull)

Expand All @@ -143,15 +145,15 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
Class information for a class.
*/
@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) 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
Expand All @@ -171,7 +173,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
@param cls A class.
@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.
Expand All @@ -182,6 +184,8 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding);
@param className A class name.
@return A class info, or nil if an error occurs.
*/
+ (instancetype)classInfoWithClassName:(NSString *)className;
+ (nullable instancetype)classInfoWithClassName:(NSString *)className;

@end

NS_ASSUME_NONNULL_END
5 changes: 5 additions & 0 deletions YYModel/YYClassInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ - (void)_update {
}
free(ivars);
}

if (!_ivarInfos) _ivarInfos = @{};
if (!_methodInfos) _methodInfos = @{};
if (!_propertyInfos) _propertyInfos = @{};

_needUpdate = NO;
}

Expand Down

0 comments on commit 5f6c6b7

Please sign in to comment.