专栏首页大话swiftplayground 简易json字符串转化Model
原创

playground 简易json字符串转化Model

最近被公司超长的json【惊悚】给烦死了,趁着午休简易写了一个,还有很多改进之处,慢慢完善吧

1 完善出转化Object的

2 完善出转化Swift

3 晚上随意转化swift Object

比较粗糙六个Tagextension String: TMObjcProperty{func convertObjcProperty() -> String { return """ @property(copy,nonatomic) NSString * \(self); """ } func convertObjcClassProperty() -> String{ return """ @property(copy,nonatomic) \(self.capitalized) * \(self); """ } func convertObjcArrayProperty() -> String{ return """ @property(strong,nonatomic) NSArray<\(self.capitalized)*> * \(self); """ } func convertObjcStringProperty() -> String { return """ @property(copy,nonatomic) NSString * \(self); """ } func convertObjcNumberProperty() -> String { return """ @property(assign, nonatomic) NSNumber * \(self); """ }} var pName = TMObjecConvertProperty.init("name").propertyvar item = ["name":"Jack", "list":["name":"Jack"]] as String : Anyextension Dictionary{ func toModel(_ className: String = "ItemClassName", superClassName:String = "NSObject", frameworks:String = "#import<UIKit/UIKit.h>") -> String{ var container:String = String var imports = frameworks for (key, value) in self { let k = "(key)" switch value { case is String : container.append( k.convertObjcProperty()) break case is Double: container.append( k.convertObjcNumberProperty()) case is Int: container.append( k.convertObjcNumberProperty()) case is Bool: container.append( k.convertObjcNumberProperty()) break case is Array<Any> : container.append( k.convertObjcArrayProperty()) print((value as! Array<Any>).toModel(k.capitalized)) break case is Dictionary: container.append( k.convertObjcClassProperty()) imports.append(""" #import"\(k.capitalized).h" """) print((value as! Dictionary<String, Any>).toModel(k.capitalized)) break default: print("") } } return """ \(imports.joined(separator: "\n")) @interface \(className.capitalized):\(superClassName) \(container.joined(separator: "\n")) @end\n @interface \(className.capitalized)() //TODO 子这里补全extension @end @implementation \(className.capitalized) @end """ }} extension Array{ func toModel(_ propertyName: String = "list", className: String = "Item") -> String{ var dest = "" if self.isEmpty{ dest = "@property(nonatomic, strong) NSArray (propertyName);\n" }else{ let value = self.first! switch value { case is Dictionary<String, Any> : dest = "@property(nonatomic, strong) NSArray<(className.capitalized)> *(propertyName);\n" print((value as! Dictionary<String, Any> ).toModel(propertyName.capitalized)) break case is String: dest = "@property(nonatomic, strong) NSArray<String*> *\(propertyName);\n" break case is Bool: dest = "@property(nonatomic, strong) NSArray<NSNumber*> *\(propertyName);\n" break case is Int: dest = "@property(nonatomic, strong) NSArray<NSNumber*> *\(propertyName);\n" break case is Double: dest = "@property(nonatomic, strong) NSArray<NSNumber*> *\(propertyName);\n" break default: dest = "" } } return dest }}//print(["name":"jack", "age":12,"height":175.3, "sub":"name":"Jack", "items":1,2,3].toModel()) //print("\n") //print(["name":"JJJ"].toModel())do{ let jsonObj = try JSONSerialization.jsonObject(with: json.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary<String, Any> print(jsonObj.toModel("TlbModel"))}catch{ print(error.localizedDescription) }

测试用例print(["name":"jack", "age":12,"height":175.3, "sub":["name":"Jack"], "items":[1,2,3]].toModel()) print("\n") print([["name":"JJJ"]].toModel())

测试结果

#import<UIKit/UIKit.h>
@interface Sub:NSObject
@property(copy,nonatomic) NSString * name;
@end



@interface Sub()
//TODO 子这里补全extension

@end

@implementation Sub

@end
@property(nonatomic, strong) NSArray<NSNumber*> *Items;

#import<UIKit/UIKit.h>
#import"Sub.h"
@interface Itemclassname:NSObject
@property(assign, nonatomic) NSNumber * height;
@property(assign, nonatomic) NSNumber * age;
@property(copy,nonatomic) Sub * sub;
@property(copy,nonatomic) NSString * name;
@property(strong,nonatomic) NSArray<Items*> * items;
@end



@interface Itemclassname()
//TODO 子这里补全extension

@end

@implementation Itemclassname

@end


#import<UIKit/UIKit.h>
@interface List:NSObject
@property(copy,nonatomic) NSString * name;
@end



@interface List()
//TODO 子这里补全extension

@end

@implementation List

@end
@property(nonatomic, strong) NSArray<Item*> *list;

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 二 iOS模版引擎--Stencil

    大话swift
  • iOS 模版引擎加速web预览

    在iOS开发中大家对于有些功能和模块不可避免的需要使用HTML来加速开发和实现功能的快速变更,例如:预览和活动页面。 基于需求的不断变更大家很多采用的H5来做,...

    大话swift
  • UnsafePoint 指针

    从上看出数组名是对应的指针在swift中也是成立的,同时对于变量天际&即得到一个UnsafePointer

    大话swift
  • 不一样的七夕礼:最美情话+AI 作诗

    七夕到了,程序猿同志们都在想怎么送女朋友礼物,送包包?太俗气!送口红?没有新意!

    月小水长
  • 不一样的七夕礼:AI 教你写情诗

    没有点子的同志们不妨看看这篇,满满的干货:LDA 主题模型提取知乎情话中最美的意象 + AI 根据提取出的意象自动写情诗!送给女朋友一首这样的情诗,比口红包包什...

    CDA数据分析师
  • Kaggle上的泰坦尼克生还数据分析

    数据准备 先根据数据的codebook来给每列命名,同时预先设定类型 setwd("d:/course/kaggle/titanic/") train.co...

    机器学习AI算法工程
  • 极客周刊丨谷歌最强AI助手,余额宝不限购回归,传销组织云联惠被查封...

    一川水巷
  • 万能的BeanPostProcessor是如何让spring无限扩展的?

    很多朋友一提到spring,首先想到的肯定是IOC(控制反转)和 AOP (面向切面编程),没错,这两个是spring的核心功能。但是什么技术让spring拥...

    苏三说技术
  • 【Go 语言社区】算法课程 第一季 第6节 建立三角形

    package main import ( "fmt" ) // 主函数 func main() { fmt.Println("...

    李海彬
  • Node 应用的 Systemd 启动

    前面的文章介绍了 Systemd 的操作命令和基本用法,今天给出一个实例,如何使用 Systemd 启动一个 Node 应用。 本文是独立的,不需要前面的教程作...

    ruanyf

扫码关注云+社区

领取腾讯云代金券