Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some useful method to YYModel #208

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 63 additions & 57 deletions Benchmark/ModelBenchmark.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions Benchmark/ModelBenchmark/Base.lproj/Main.storyboard
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,17 +18,17 @@
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="See logs in Xcode" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="j5c-ge-kV1">
<rect key="frame" x="232" y="290" width="137" height="21"/>
<rect key="frame" x="119" y="323" width="137" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="centerY" secondItem="j5c-ge-kV1" secondAttribute="centerY" id="aDc-DL-Lbw"/>
<constraint firstAttribute="centerX" secondItem="j5c-ge-kV1" secondAttribute="centerX" id="nbQ-nb-cHc"/>
Expand Down
2 changes: 1 addition & 1 deletion Benchmark/ModelBenchmark/GitHubUser.m
Expand Up @@ -118,7 +118,6 @@ - (id)initWithCoder:(NSCoder *)aDecoder {




@implementation YYGHUser
+ (NSDictionary *)modelCustomPropertyMapper {
return @{
Expand All @@ -142,6 +141,7 @@ + (NSDictionary *)modelCustomPropertyMapper {
@"updatedAt" : @"updated_at",
};
}

- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (id)initWithCoder:(NSCoder *)aDecoder { return [self yy_modelInitWithCoder:aDecoder]; }
@end
Expand Down
23 changes: 23 additions & 0 deletions Benchmark/ModelBenchmark/ViewController.m
Expand Up @@ -25,12 +25,35 @@
FastEasyMapping: https://github.com/Yalantis/FastEasyMapping
MJExtension: https://github.com/CoderMJLee/MJExtension
*/

@interface YYTag : NSObject
@property (nonatomic, strong) NSString *tagName; ///< 标签名字,例如"上海·上海文庙"
@property (nonatomic, strong) NSString *tagScheme; ///< 链接 sinaweibo://...
@property (nonatomic, assign) int32_t tagType; ///< 1 地点 2其他
@property (nonatomic, assign) int32_t tagHidden;
@property (nonatomic, strong) NSURL *urlTypePic; ///< 需要加 _default
@property (nonatomic, copy)NSString *wbName;
@end
@implementation YYTag
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"wbName" : @"wb_name.newName.info[1].nameChangedTime[1].bbb.text[2].text.page[1].test[1]"};
}
+ (NSString *)replaceKeyFromPropertyName:(NSString *)propertyName {
return [propertyName stringWithMapperType:NSStringMapperUnderLineFromCamel];
}
@end

@implementation ViewController

- (void)viewDidLoad {


[super viewDidLoad];

YYTag *tag = [YYTag yy_modelWithJSON:@{@"tag_hidden" : @2 , @"tag_name" : @"上海·上海文庙", @"tag_scheme" : @"http://www.scheme", @"tag_type" : @1, @"url_type_pic" : @"http://www.pic", @"tag_topic" : @"#today is hot", @"wb_name" : @{@"newName" : @{ @"info" : @[@"test-data", @{@"nameChangedTime" : @[@{@"aaa" : @"2013-01"}, @{@"bbb" : @{@"text" : @[@"2014-01", @"2014-02", @{@"text" : @{@"page" : @[@"2017-08", @{@"test" : @[@"2017-09", @"2017-10"]}]}}]}}]}] } }}];

NSLog(@"%@", [tag yy_modelToJSONString]);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self benchmarkGithubUser];
[self benchmarkWeiboStatus];
Expand Down
68 changes: 64 additions & 4 deletions YYModel/NSObject+YYModel.h
Expand Up @@ -291,17 +291,31 @@ NS_ASSUME_NONNULL_BEGIN

@implementation YYBook
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID": @[@"id", @"ID", @"book_id"]};
return @{@"name" : @"n",
@"page" : @"p",
@"desc" : @"ext.desc",
@"bookID" : @[@"id", @"ID", @"book_id"],
@"tagTitle" : @"tags[0].title"};
}
@end

@return A custom mapper for properties.
*/
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper;


#pragma mark - YYAdd

/**
A custom method for mapper between property and key

@discussion if you implement method `+(NSDictionary *)modelCustomPropertyMapper` and return an valid dictionary, then the property in the dicionary will not as method '+ (NSString *)mapperToKeyFromPropertyName:' propertyName parameter

@param propertyName the property's name
@return the key associated with the property name
*/
+ (NSString *)mapperToKeyFromPropertyName:(NSString *)propertyName;

/**
The generic class mapper for container properties.

Expand Down Expand Up @@ -427,4 +441,50 @@ NS_ASSUME_NONNULL_BEGIN

@end



@interface NSObject (YYAdd)


/**
To avoid method `+ (NSDictionary *)yy_modelDictionaryWithClass:json:` too many parameter

@param json A json array of `NSArray`, `NSString` or `NSData`.
Example: [{"name","Mary"},{name:"Joe"}]
@return A array, or nil if an error occurs.
*/
+ (nullable NSDictionary *)yy_modelDictionaryWithJSON:(id)json;

/**
To avoid method `(NSDictionary *)yy_modelDictionaryWithClass:json:` too many parameter

@param json A json dictionary of `NSDictionary`, `NSString` or `NSData`.
Example: {"user1":{"name","Mary"}, "user2": {name:"Joe"}}
@return A dictionary, or nil if an error occurs.
*/
+ (nullable NSArray *)yy_modelArrayWithKeyValuesArray:(id)json;


/**
Create model array from a plist file

@discussion the plist file must be in the main bundle.

@param filename plist file name
@return model array
*/
+ (NSArray *)yy_modelArrayWithFilename:(NSString *)filename;

/**
Create model array from a plist file

@param file the full path of the plist file
@return model array
*/
+ (NSArray *)yy_modelArrayWithFile:(NSString *)file;


@end


NS_ASSUME_NONNULL_END