• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

如何创造Model以及数据和模型的嵌套

2016-01-09

如何创造Model

创造Model,就是把标准数据格式,转化成一个类,一般模型Model对应的是字典Dictionary.以便于修改和调用.Model可以用”.”语法调用,且可以赋值和修改,Dictionary如果不是mutable的话,在初始化后就不能改变了,调用还要记得key值

//Model.h
@interface CustomModel : NSObject<NSCopying>

@property (nonatomic, copy) NSString *theme;
@property (nonatomic, copy) NSString *mark;

- (instancetype)initWithDict:(NSDictionary *)dict;
+ (NSDictionary *)convertDictFromModel:(CustomModel *)model;
+ (instancetype)createWithDict:(NSDictionary *)dict;

@end

//Model.m
@implementation CustomModel

- (instancetype)initWithDict:(NSDictionary *)dict {
    self = [super init];
    if (self) {
        self.theme = [dict valueForKey:@"THEME"];
        self.mark = [dict valueForKey:@"MARK"];
    }
    return self;
}

+ (NSDictionary *)convertDictFromModel:(CustomModel *)model {
    [dict setValue:model.theme forKey:@"THEME"];
    [dict setValue:model.mark forKey:@"MARK"];
    return dict;
}

+ (instancetype)createWithDict:(NSDictionary *)dict {
    return [[self alloc]initWithDict:dict];
}

- (id)copyWithZone:(NSZone *)zone {
    CustomModel *newItem = [[CustomModel allocWithZone:zone] init];
    newItem.theme = self.theme;
    newItem.mark = self.mark;
    return newItem;
}

@end

数据和模型

数据主要是指通用化的结构数据,比如Array和Dictionary,模型是指自己自定义的Object

数据结构之间互相嵌套

这个就和Json一样,都是基本类型,很容易解析

数据和模型之间的嵌套

数据和模型之间嵌套,因为不能被标准库解析模型,所以一定要加入模型的.h文件,用来解析和使用

- (instancetype)initWithDict:(NSDictionary *)dict;//字典转模型(写)
+ (NSDictionary *)convertDictFromModel:(CustomModel *)model;//模型转字典(读,解析)
+ (instancetype)createWithDict:(NSDictionary *)dict;//字典转模型

模型套模型

为了优化数据的结构,可以采用模型套模型,但是一定要记得加载两个模型的.h文件,以便使用模型.

这么麻烦,可不可以帮我自动生成啊

有,我的老师说Github上有个她培训老师李明杰的第三放库,但是我没用过

MJextension
赏

请问老板还招人么(/ω\)

支付宝
微信
  • iOS
  • Memory
  • Tips

扫一扫,分享到微信

微信分享二维码
present出的ViewController位于哪个内存位置
排序一个Dictionary的Array
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。