iOS 对象信息保存、转化JSON、JSON生成对象的模型方案记录
来源:互联网 发布:xp系统网络ip地址设置 编辑:程序博客网 时间:2024/05/29 14:20
目标:
将对象的一些信息提取保存起来,然后转化成JSON,再上传到服务器,或其他保存本地的用途。
从服务器接收或从本地读取,然后通过JSON的信息生成指定的对象。
方案代码:
//// KeepLabelInfoModel.h// TestViewXIB//// Created by yococo on 15/9/29.// Copyright (c) 2015年 yococo. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface KeepLabelInfoModel : NSObject/***************************///记录当前所保存的数据信息@property(nonatomic,strong,readonly) NSMutableArray* infoArray;/***************************///记录指定对象,内部自行提取需要保存的数据信息- (void) addObject:(UILabel*)anObject;/***************************///将指定的数据信息数组转换成JSON 字符串格式@property(nonatomic,readonly) NSString* JSONString;//将指定的数据信息数组转换成JSON 数据格式@property(nonatomic,readonly) NSData* JSONData;/***************************///内部处理间的错误情况@property(nonatomic,strong) NSError* error;/***************************///通过获取指定数据信息生成指定对象- (void) modelObjectsWithJSONString:(NSString*)jsonString;- (void) modelObjectsWithJSONData:(NSData*)jsonData;//记录所生成的对象@property(nonatomic,strong,readonly) NSArray* modelObjects;@end
//// infoLabelInfoModel.m// TestViewXIB//// Created by yococo on 15/9/29.// Copyright (c) 2015年 yococo. All rights reserved.//#import "KeepLabelInfoModel.h"@implementation KeepLabelInfoModel- (void) dealloc{ _infoArray = nil; _error = nil; _modelObjects = nil;}- (void) addObject:(UILabel*)anObject{ if (_infoArray == nil) { _infoArray = [NSMutableArray array]; } if (anObject == nil || [anObject isKindOfClass:[UILabel class]] == NO) { return; } NSMutableDictionary* tempDictionary = [NSMutableDictionary dictionary]; [tempDictionary setObject:anObject.text forKey:@"text"]; [tempDictionary setObject:NSStringFromCGRect(anObject.frame) forKey:@"frame"]; [tempDictionary setObject:NSStringFromCGAffineTransform(anObject.transform) forKey:@"transform"]; [tempDictionary setObject:[self getHSBAStringByColor:anObject.textColor] forKey:@"textColor"]; [_infoArray addObject:tempDictionary]; }- (NSString*) JSONString{ if (_infoArray == nil) { return nil; } NSError* tempError = nil; NSData* jsonData = [NSJSONSerialization dataWithJSONObject:_infoArray options:NSJSONWritingPrettyPrinted error:&tempError]; if (tempError == nil) { NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; return jsonString; } self.error = tempError; return nil;}- (NSData*) JSONData{ if (_infoArray == nil) { return nil; } NSError* tempError = nil; NSData* jsonData = [NSJSONSerialization dataWithJSONObject:_infoArray options:NSJSONWritingPrettyPrinted error:&tempError]; if (tempError == nil) { return jsonData; } self.error = tempError; return nil;}- (void) modelObjectsWithJSONData:(NSData *)jsonData{ if (jsonData == nil) { return; } NSError* tempError = nil; NSArray* tempArr = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&tempError]; if (tempError) { self.error = tempError; return; } if (tempArr == nil || [tempArr isKindOfClass:[NSArray class]] == NO) { return; } NSMutableArray* tempArray = [NSMutableArray array]; for (NSDictionary* tempDictionary in tempArr) { if ([tempDictionary isKindOfClass:[NSDictionary class]]) { UILabel* tempLabel = [[UILabel alloc] init]; NSString* tempText = tempDictionary[@"text"]; if (tempText && [tempText isKindOfClass:[NSString class]]) { tempLabel.text = tempText; } NSString* tempFrame = tempDictionary[@"frame"]; if (tempFrame && [tempFrame isKindOfClass:[NSString class]]) { tempLabel.frame = CGRectFromString(tempFrame); } NSString* tempTransform = tempDictionary[@"transform"]; if (tempTransform && [tempTransform isKindOfClass:[NSString class]]) { tempLabel.transform = CGAffineTransformFromString(tempTransform); } NSString* tempTextColor = tempDictionary[@"textColor"]; if (tempTextColor && [tempTextColor isKindOfClass:[NSString class]]) { tempLabel.textColor = [self colorWithHSBAString:tempTextColor]; } [tempArray addObject:tempLabel]; } } _modelObjects = tempArray;}- (void) modelObjectsWithJSONString:(NSString*)jsonString{ if (jsonString == nil) { return; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; [self modelObjectsWithJSONData:jsonData];}- (NSString *)getHSBAStringByColor:(UIColor *)originColor { NSDictionary *hsbaDict = [self getHSBAValueByColor:originColor]; return [NSString stringWithFormat:@"(%0.2f, %0.2f, %0.2f, %0.2f)", [hsbaDict[@"H"] floatValue],[hsbaDict[@"S"] floatValue],[hsbaDict[@"B"] floatValue],[hsbaDict[@"A"] floatValue]];}- (NSDictionary *)getHSBAValueByColor:(UIColor *)originColor{ CGFloat h=0,s=0,b=0,a=0; if ([originColor respondsToSelector:@selector(getHue:saturation:brightness:alpha:)]) { [originColor getHue:&h saturation:&s brightness:&b alpha:&a]; } return @{@"H":@(h),@"S":@(s),@"B":@(b),@"A":@(a)};}- (UIColor *) colorWithHSBAString:(NSString*)aColorString{ if (aColorString == nil) { return nil; } NSString* tempString = [aColorString stringByReplacingOccurrencesOfString:@"(" withString:@""]; tempString = [aColorString stringByReplacingOccurrencesOfString:@")" withString:@""]; NSArray* tempArray = [tempString componentsSeparatedByString:@","]; if (tempArray && tempArray.count == 4) { return [UIColor colorWithHue:[tempArray[0] floatValue] saturation:[tempArray[1] floatValue] brightness:[tempArray[2] floatValue] alpha:[tempArray[3] floatValue]]; } return nil;}@end
通过 Button 以及 Storyboard 上的Label进行测试
1.获取 Label 的信息
KeepLabelInfoModel* tempModel = [[KeepLabelInfoModel alloc] init]; for (UILabel* subview in self.view.subviews) { if ([subview isKindOfClass:[UILabel class]]) { [tempModel addObject:subview]; } } NSLog(@"%@",tempModel.JSONString);
2.删除 Storyboard 上的 Label
for (UILabel* subview in self.view.subviews) { if ([subview isKindOfClass:[UILabel class]]) { [subview removeFromSuperview]; } }
3.校验生成对象情况
NSString* tempString = @"[" "{" "\"textColor\" : \"(0.00, 0.00, 0.67, 1.00)\"," "\"frame\" : \"{{104, 380}, {134, 35}}\"," "\"text\" : \"123\"," "\"transform\" : \"[1, 0, 0, 1, 0, 0]\"" "}," "{" "\"textColor\" : \"(0.00, 0.96, 1.00, 1.00)\"," "\"frame\" : \"{{273, 276}, {29, 21}}\"," "\"text\" : \"234\"," "\"transform\" : \"[1, 0, 0, 1, 0, 0]\"" "}," "{" "\"textColor\" : \"(0.00, 0.00, 1.00, 1.00)\"," "\"frame\" : \"{{150, 498}, {29, 21}}\"," "\"text\" : \"567\"," "\"transform\" : \"[1, 0, 0, 1, 0, 0]\"" "}," "{" "\"textColor\" : \"(0.00, 0.00, 0.00, 1.00)\"," "\"frame\" : \"{{217, 405}, {29, 21}}\"," "\"text\" : \"346\"," "\"transform\" : \"[1, 0, 0, 1, 0, 0]\"" "}," "{" "\"textColor\" : \"(0.18, 0.74, 1.00, 1.00)\"," "\"frame\" : \"{{273, 498}, {29, 21}}\"," "\"text\" : \"498\"," "\"transform\" : \"[1, 0, 0, 1, 0, 0]\"" "}," "]"; KeepLabelInfoModel* tempModel = [[KeepLabelInfoModel alloc] init]; [tempModel modelObjectsWithJSONString:tempString]; for (UIView* subview in tempModel.modelObjects) { if ([subview isKindOfClass:[UIView class]]) { [self.view addSubview:subview]; } }
0 0
- iOS 对象信息保存、转化JSON、JSON生成对象的模型方案记录
- 对象信息生成Json串
- json与对象之间的转化(方案一)
- iOS 将对象转化为json串
- JSON对象转化
- JS.json对象转化
- 对象转化为json
- Json转化为对象
- 对象转化为json
- json对象转化成json的字符串、克隆对象
- Foundation对象转化Json对象
- 对象生成JSON的方式
- Json 与 后台对象的相互转化
- java JSON转化为对象的例子
- json和java对象的相互转化
- java对象和JSON的转化
- java和json对象的转化
- json与对象的相互转化
- 选择排序(直接选择)
- 黑马程序员--Java基础--06-多线程1
- webwork+spring+hibernate进行数据增删查改操作时出现NullPointerException异常
- Unity优化技巧提到的3篇翻墙文章之Unity 4.3 關於 Occlusion Culling : 錯誤診斷
- 插入排序(折半插入)
- iOS 对象信息保存、转化JSON、JSON生成对象的模型方案记录
- Unity3D图片质量设置
- 顺序容器和关联容器添加新元素方法详解
- LeetCode 题解(247) : Two Sum III - Data structure design
- 终端查看静态库的详细信息,以及合并不同版本的静态库
- iOS获取键盘的高度
- multimap容器查找元素的三种方法总结
- HBase 0.98安装(伪分布式)
- Linux Objdump Command Examples (Disassemble a Binary File)