iOS reflex, MVC model

Source: Internet
Author: User

The MVC pattern is clear to everyone! The biggest advantage is a clear point, view and data separate, in the Viewcontroller as long as the logic of the view and data, and even the logic is in the model processing, well what to say, the past two weeks, Are doing something very lame: model is written like this:

@interface spexangemodel:nsobject/*! *  Model Attributes */@property (nonatomic, copy) NSString * City, @property (nonatomic, copy) NSString * content; @property (nonatom IC, copy) NSString * create_time; @property (nonatomic, copy) NSString * credits; @property (nonatomic, copy) NSString * Eid ; @property (nonatomic, copy) NSString * exchange_time; @property (nonatomic, copy) NSString * exchanged; @property (Nonatom  IC, copy) NSString * _id; @property (nonatomic, copy) NSString * image; @property (nonatomic, copy) NSString * key; @property (nonatomic, copy) NSString * NUM; @property (nonatomic, copy) NSString * status, @property (nonatomic, copy) NSString * surplus; @property (non Atomic, copy) NSString * tag; @property (nonatomic, copy) NSString * title; @end

Then spare no effort to implement this in the document:

@implementation spexangemodel-(ID) initwithdictionary: (Nsdictionary *) dictionary{self = [super init];        if (self) {self.city = spformatstring (dictionary[@ "City"]);        Self.content = spformatstring (dictionary[@ "content"]);        Self.create_time = dictionary[@ "Create_time"];        Self.credits = spformatstring (dictionary[@ "credits"]);        Self.eid = spformatstring (dictionary[@ "Eid"]);        Self.exchange_time = dictionary[@ "Exchange_time"];        self.exchanged = spformatstring (dictionary[@ "exchanged"]);        self._id = spformatstring (dictionary[@ "id"]);        Self.image = dictionary[@ "image"];        Self.key = dictionary[@ "Key"];        Self.num = dictionary[@ "num"];        Self.status = spformatstring (dictionary[@ "status"]);        Self.surplus = spformatstring (dictionary[@ "surplus"]);        Self.tag = spformatstring (dictionary[@ "tag"]);    Self.title = dictionary[@ "title"]; } return self;} @end 

Of course, a class words do not matter, but the beat later, the model increased to dozens of after, well, I myself collapsed, this really is in the move brick, read the runtime header file, more interesting, just familiar with C #, because in C # in Java there is reflection of this argument, So it's a lot easier to deal with, um, there's something in iOS, and here's a look at the improved version

Model base class:

#import <Foundation/Foundation.h> @interface basemodel:nsobject/*! *  Initialize with dictionary * *  @param dictionary dictionary * *  @return result */-(ID) initwithdictionary: (Nsdictionary *) dictionary;// Gets the dictionary form: the dictionary that corresponds to the attribute and value-(nsdictionary *) todictionary; @end

#import "BaseModel.h" #import <objc/runtime.h> @interface Basemodel (BS)/*! * Get Property List * * @return attribute list */-(Nsarray *) getpropertys; @end @implementation basemodel-(ID) initwithdictionary: (Nsdictionar    Y *) dictionary{self = [super init];        if (self) {///own list of attributes Nsarray *keys = [i getpropertys];                Dictionary attribute list Nsarray *dickeys = [dictionary AllKeys];        __weak typeof (self) weakself = self; [Dickeys enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop)            {if ([keys containsobject:obj]) {[weakself setvalue:dictionary[obj] forkey:obj];            }        }]; } return self;}        -(Nsarray *) getpropertys{Nsmutablearray *propertys = [[Nsmutablearray alloc] init];    unsigned int outcount, I;    objc_property_t *properties = Class_copypropertylist ([self class], &outcount);        For (i=0, i<outcount; i++) {objc_property_t property = Properties[i]; NSString* key = [[NSString alloc]initwithcstring:property_getname] encoding:nsutf8stringencoding];    [Propertys Addobject:key]; } return propertys;}        -(Nsdictionary *) todictionary{nsmutabledictionary *dictionary = [[Nsmutabledictionary alloc] init];    Own attribute list Nsarray *keys = [self getpropertys]; [Keys enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop)        {id value = [self valueforkey:obj];        if (value = nil) {[dictionary setobject:value forkey:obj];        }    }]; return dictionary;} @end

Later mode will directly integrate this Basemodel, and then call Initialize!!!

iOS reflex, MVC model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.