Skip to content

Commit

Permalink
新增模型归档自动化
Browse files Browse the repository at this point in the history
  • Loading branch information
lovemo committed Apr 29, 2016
1 parent 8def854 commit cf6b18c
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
Binary file modified SUIMVVMDemo/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions SUIMVVMDemo/SUIMVVMDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
346F3BC21CD345B5004C735E /* NSObject+SMKCoding.m in Sources */ = {isa = PBXBuildFile; fileRef = 346F3BC11CD345B5004C735E /* NSObject+SMKCoding.m */; };
34870A401CBA4B1200EDEF26 /* FirstVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 34870A1E1CBA4B1200EDEF26 /* FirstVC.m */; };
34870A411CBA4B1200EDEF26 /* FirstModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 34870A211CBA4B1200EDEF26 /* FirstModel.m */; };
34870A421CBA4B1200EDEF26 /* FirstRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 34870A231CBA4B1200EDEF26 /* FirstRequest.m */; };
Expand Down Expand Up @@ -42,6 +43,8 @@

/* Begin PBXFileReference section */
344113281C7AD7FE001FEAE6 /* PrefixHeader.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = "<group>"; };
346F3BC01CD345B5004C735E /* NSObject+SMKCoding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SMKCoding.h"; sourceTree = "<group>"; };
346F3BC11CD345B5004C735E /* NSObject+SMKCoding.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SMKCoding.m"; sourceTree = "<group>"; };
34870A1D1CBA4B1200EDEF26 /* FirstVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstVC.h; sourceTree = "<group>"; };
34870A1E1CBA4B1200EDEF26 /* FirstVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstVC.m; sourceTree = "<group>"; };
34870A201CBA4B1200EDEF26 /* FirstModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstModel.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -438,6 +441,8 @@
34FFD3E31CC4DAA80025AFCF /* SUIMVVMKit */ = {
isa = PBXGroup;
children = (
346F3BC01CD345B5004C735E /* NSObject+SMKCoding.h */,
346F3BC11CD345B5004C735E /* NSObject+SMKCoding.m */,
34FFD3E41CC4DAA80025AFCF /* NSObject+SMKProperties.h */,
34FFD3E51CC4DAA80025AFCF /* NSObject+SMKProperties.m */,
34FFD3E61CC4DAA80025AFCF /* NSObject+SMKRequest.h */,
Expand Down Expand Up @@ -653,6 +658,7 @@
34FFD4061CC4DAA80025AFCF /* UIView+SMKEvents.m in Sources */,
34FFD3FF1CC4DAA80025AFCF /* NSObject+SMKRequest.m in Sources */,
34870A461CBA4B1200EDEF26 /* FirstViewModel.m in Sources */,
346F3BC21CD345B5004C735E /* NSObject+SMKCoding.m in Sources */,
34870A421CBA4B1200EDEF26 /* FirstRequest.m in Sources */,
34870A491CBA4B1200EDEF26 /* ThirdRequest.m in Sources */,
34F72F851C8BF6A90033ED03 /* BHNavViewController.m in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions SUIMVVMKit.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|

s.name = 'SUIMVVMKit'
s.version = '0.5.1'
s.version = '0.5.2'
s.summary = 'SUIMVVMKit is a MVVM frameWork easy to develop iOS'
s.homepage = 'https://github.com/lovemo/MVVMFramework'
s.platform = :ios, '7.0'
s.license = 'MIT'
s.author = { 'lovemo' => 'lovemomoyulin@qq.com' }
s.source = { :git => 'https://github.com/lovemo/MVVMFramework.git',:tag => '0.5.1' }
s.source = { :git => 'https://github.com/lovemo/MVVMFramework.git',:tag => s.version.to_s }
s.requires_arc = true
s.public_header_files = 'SUIMVVMKit/**/*.h'
s.source_files = 'SUIMVVMKit/SUIMVVMKit.h'
Expand Down
1 change: 1 addition & 0 deletions SUIMVVMKit/SUIMVVMKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef SUIMVVMKit_h
#define SUIMVVMKit_h

#import "NSObject+SMKCoding.h"
#import "SMKExtendPublic.h"
#import "SMKAction.h"
#import "SMKConstant.h"
Expand Down
43 changes: 43 additions & 0 deletions SUIMVVMKit/SUIMVVMKit/NSObject+SMKCoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// NSObject+SMKCoding.h
// SMKMVVM
//
// Created by yuantao on 16/4/29.
// Copyright © 2016年 momo. All rights reserved.
//

#import <Foundation/Foundation.h>

/**
* SMKCodingProtocol个性化设置archiver协议
*/
@protocol SMKCodingProtocol <NSObject>

@optional

/**
* 这个数组中的属性名才会进行归档
*/
+ (NSArray *)smk_allowedCodingPropertyNames;

/**
* 这个数组中的属性名将会被忽略:不进行归档
*/
+ (NSArray *)smk_ignoredCodingPropertyNames;

@end


@interface NSObject (SMKCoding) <SMKCodingProtocol>

/**
* archiver
*/
- (BOOL)smk_writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

/**
* unarchiver
*/
+ (instancetype)smk_objectWithFile:(NSString *)path;

@end
128 changes: 128 additions & 0 deletions SUIMVVMKit/SUIMVVMKit/NSObject+SMKCoding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//
// NSObject+SMKCoding.m
// SMKMVVM
//
// Created by yuantao on 16/4/29.
// Copyright © 2016年 momo. All rights reserved.
//

#import "NSObject+SMKCoding.h"
#import <objc/runtime.h>

@implementation NSObject (SMKCoding)

+ (BOOL)supportsSecureCoding {
return YES;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
[self setWithCoder:aDecoder];
return self;
}
#pragma clang diagnostic pop

- (void)encodeWithCoder:(NSCoder *)aCoder {
for (NSString *key in [self getProperties]) {
id object = [self valueForKey:key];
if (object) [aCoder encodeObject:object forKey:key];
}
}

- (void)setWithCoder:(NSCoder *)aDecoder {
for (NSString *key in [self getProperties]) {
id value = [aDecoder decodeObjectForKey:key];
[self setValue:value forKey:key];
}
}

- (NSDictionary *)getPropertiesDict {

Class class = [self class];
unsigned int propertyCount;
__autoreleasing NSMutableDictionary *propertiesDict = [NSMutableDictionary dictionary];
objc_property_t *properties = class_copyPropertyList(class, &propertyCount);

NSArray *allowedCodingPropertyNames = [class smk_allowedCodingPropertyNames];
NSArray *ignoredCodingPropertyNames = [class smk_ignoredCodingPropertyNames];

void(^setValueBlock)(NSString *key, NSMutableDictionary *propertiesDict) = ^(NSString *key, NSMutableDictionary *propertiesDict) {
if (![ignoredCodingPropertyNames containsObject:key]) {
id value = [self valueForKey:key];
value = value ? value : [NSNull null];
[propertiesDict setValue:value forKey:key];
}
};

if (allowedCodingPropertyNames.count) {
for (unsigned int i = 0; i < propertyCount; i++) {
objc_property_t property = properties[i];
const char *propertyName = property_getName(property);
__autoreleasing NSString *key = @(propertyName);

if ([allowedCodingPropertyNames containsObject:key]) {
setValueBlock(key, propertiesDict);
}
}

free(properties);
return propertiesDict;
}

for (unsigned int i = 0; i < propertyCount; i++) {
objc_property_t property = properties[i];
const char *propertyName = property_getName(property);
__autoreleasing NSString *key = @(propertyName);
setValueBlock(key, propertiesDict);

}

free(properties);
return propertiesDict;
}

- (NSDictionary *)getProperties
{
__autoreleasing NSDictionary *propertiesDict = objc_getAssociatedObject([self class], _cmd);
if (!propertiesDict) {
propertiesDict = [NSMutableDictionary dictionary];
Class subclass = [self class];
while (subclass != [NSObject class])
{
[(NSMutableDictionary *)propertiesDict addEntriesFromDictionary:[self getPropertiesDict]];
subclass = [subclass superclass];
}
propertiesDict = [NSDictionary dictionaryWithDictionary:propertiesDict];
objc_setAssociatedObject([self class], _cmd, propertiesDict, OBJC_ASSOCIATION_RETAIN);
}
return propertiesDict;
}

- (BOOL)smk_writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
return [data writeToFile:path atomically:useAuxiliaryFile];
}

+ (instancetype)smk_objectWithFile:(NSString *)path {

NSData *data = [NSData dataWithContentsOfFile:path];

id object = nil;
if (data) {
NSPropertyListFormat format;
object = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:NULL];

if (object) {
if ([object respondsToSelector:@selector(objectForKey:)] && [(NSDictionary *)object objectForKey:@"$archiver"]) {
object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
} else {
object = data;
}
}

return object;
}

@end

0 comments on commit cf6b18c

Please sign in to comment.