最近写程序总是遇到这么一个问题: 网络获取数据后,我们需要将一些信息转为类,方便后面的操作。例如:
list 里面的字段,我们需要转成一个类的属性。例如 转为下面的样子:
@interface OffLinePublicModel : NSObject
@property (nonatomic, strong) NSString *orderno;
@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) NSString *statusflag;
@property (nonatomic, strong) NSString *Id;
@property (nonatomic, strong) NSString *userid;
@property (nonatomic, strong) NSString *companyid;
@property (nonatomic, strong) NSString *companyname;
@property (nonatomic, strong) NSString *areaid;
@property (nonatomic, strong) NSString *areaname;
@property (nonatomic, strong) NSString *rootid;
@property (nonatomic, strong) NSString *parentids;
@property (nonatomic, strong) NSString *channelid;
@property (nonatomic, strong) NSString *channelname;
@property (nonatomic, strong) NSString *isfinish;
@property (nonatomic, strong) NSString *releasetime;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *desc;
@property (nonatomic, strong) NSString *gongsi;
@property (nonatomic, strong) NSString *imageurl;
@property (nonatomic, strong) NSString *infotype;;
@property (nonatomic, strong) NSString *personname;
@property (nonatomic, strong) NSString *personphone;
@property (nonatomic, strong) NSArray *imagesList;
@property (nonatomic, strong) NSString *year;
@property (nonatomic, strong) NSString *salary;
@property (nonatomic, strong) NSString *edu;
@property (nonatomic, strong) NSString *num;
@property (nonatomic, strong) NSString *age;
@property (nonatomic, strong) NSString *place;
@property (nonatomic, strong) NSString *estate;
@property (nonatomic, strong) NSString *numshi;
@property (nonatomic, strong) NSString *numting;
@property (nonatomic, strong) NSString *numwei;
@property (nonatomic, strong) NSString *measure;
@property (nonatomic, strong) NSString *sheshi;
@property (nonatomic, strong) NSString *numlou;
@property (nonatomic, strong) NSString *numall;
@property (nonatomic, strong) NSString *ispersonal;
@property (nonatomic, strong) NSString *money;
@property (nonatomic, strong) NSString *qq;
@property (nonatomic, strong) NSString *usage;
@property (nonatomic, strong) NSString *color;
@property (nonatomic, strong) NSString *mode ;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
#import "OffLinePublicModel.h"
#import "NSDictionary+StringValue.h"
@implementation OffLinePublicModel
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
self.orderno = [dictionary stringValueForKey:@"orderno"];
self.status = [dictionary stringValueForKey:@"status"];
self.statusflag = [dictionary stringValueForKey:@"statusflag"];
self.Id = [dictionary stringValueForKey:@"id"];
self.userid = [dictionary stringValueForKey:@"userid"];
self.companyid = [dictionary stringValueForKey:@"companyid"];
self.companyname = [dictionary stringValueForKey:@"companyname"];
self.areaid = [dictionary stringValueForKey:@"areaid"];
self.areaname = [dictionary stringValueForKey:@"areaname"];
self.rootid = [dictionary stringValueForKey:@"rootid"];
self.parentids = [dictionary stringValueForKey:@"parentids"];
self.channelid = [dictionary stringValueForKey:@"channelid"];
self.channelname = [dictionary stringValueForKey:@"channelname"];
self.isfinish = [dictionary stringValueForKey:@"isfinish"];
self.releasetime = [dictionary stringValueForKey:@"releasetime"];
self.title = [dictionary stringValueForKey:@"title"];
self.desc = [dictionary stringValueForKey:@"desc"];
self.gongsi = [dictionary stringValueForKey:@"gongsi"];
self.imageurl = [dictionary stringValueForKey:@"imageurl"];
self.infotype = [dictionary stringValueForKey:@"infotype"];
self.personname = [dictionary stringValueForKey:@"personname"];
self.personphone = [dictionary stringValueForKey:@"personphone"];
self.imagesList = [dictionary objectForKey:@"imagesList"];
self.year = [dictionary stringValueForKey:@"year"];
self.salary = [dictionary stringValueForKey:@"salary"];
self.edu = [dictionary stringValueForKey:@"edu"];
self.num = [dictionary stringValueForKey:@"num"];
self.age = [dictionary stringValueForKey:@"age"];
self.place = [dictionary stringValueForKey:@"place"];
self.estate = [dictionary stringValueForKey:@"estate"];
self.numshi = [dictionary stringValueForKey:@"numshi"];
self.numting = [dictionary stringValueForKey:@"numting"];
self.numwei = [dictionary stringValueForKey:@"numwei"];
self.measure = [dictionary stringValueForKey:@"measure"];
self.sheshi = [dictionary stringValueForKey:@"sheshi"];
self.numlou = [dictionary stringValueForKey:@"numlou"];
self.numall = [dictionary stringValueForKey:@"numall"];
self.ispersonal = [dictionary stringValueForKey:@"ispersonal"];
self.money = [dictionary stringValueForKey:@"money"];
self.qq = [dictionary stringValueForKey:@"qq"];
self.usage = [dictionary stringValueForKey:@"usage"];
self.color = [dictionary stringValueForKey:@"color"];
self.mode = [dictionary stringValueForKey:@"mode"];
}
return self;
}
@end
那么,问题来了 。这个要一个一个的敲吗?
苦逼的我一直是挨个敲得 。
今天终于忍不了了,我决定写个东西来缓解一下。但是苦逼的Mac 桌面程序不会写,web不会,怎么办? 没办法 。只能用Xcode 写一个Command Line Tool
在日志输出栏来操作 ,委屈求全相当蛋疼啊。
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
char s[10000] ;
NSMutableArray *StringArray = [NSMutableArray array];
while (gets(s) &&s[0] != '\0') {
NSString *string = [[NSString alloc]initWithCString:s encoding:NSUTF8StringEncoding];
[StringArray addObject:string];
}
for (NSString *string in StringArray) {
NSString *newString = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
NSArray *array = [newString componentsSeparatedByString:@":"];
newString = array[0];
printf("@property (nonatomic, strong) NSString *%s\n",[newString UTF8String]);
}
printf("\n\n");
for (NSString *string in StringArray) {
NSString *newString = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
NSArray *array = [newString componentsSeparatedByString:@":"];
newString = array[0];
printf("self.%s = [[dictionary stringValueForKey:\"%s\"];\n",[newString UTF8String],[newString UTF8String]);
}
printf("\n\n\n\n");
}
return 0;
}
复制上面的字段 粘贴到里面 (由于是个小东西)没有错误处理,所以一定要注意哦,只复制要生成属性的字段。
注意粘贴上后 多加两个回车。
输出样例如下:
@property (nonatomic, strong) NSString *orderno
@property (nonatomic, strong) NSString *status
@property (nonatomic, strong) NSString *statusflag
@property (nonatomic, strong) NSString *id
@property (nonatomic, strong) NSString *userid
@property (nonatomic, strong) NSString *companyid
@property (nonatomic, strong) NSString *companyname
@property (nonatomic, strong) NSString *areaid
@property (nonatomic, strong) NSString *areaname
@property (nonatomic, strong) NSString *rootid
@property (nonatomic, strong) NSString *parentids
@property (nonatomic, strong) NSString *channelid
@property (nonatomic, strong) NSString *channelname
@property (nonatomic, strong) NSString *isfinish
@property (nonatomic, strong) NSString *releasetime
@property (nonatomic, strong) NSString *title
@property (nonatomic, strong) NSString *desc
@property (nonatomic, strong) NSString *gongsi
@property (nonatomic, strong) NSString *imageurl
@property (nonatomic, strong) NSString *infotype
@property (nonatomic, strong) NSString *personname
@property (nonatomic, strong) NSString *personphone
@property (nonatomic, strong) NSString *imagesList
@property (nonatomic, strong) NSString *year
@property (nonatomic, strong) NSString *salary
@property (nonatomic, strong) NSString *edu
@property (nonatomic, strong) NSString *num
@property (nonatomic, strong) NSString *age
@property (nonatomic, strong) NSString *place
@property (nonatomic, strong) NSString *estate
@property (nonatomic, strong) NSString *numshi
@property (nonatomic, strong) NSString *numting
@property (nonatomic, strong) NSString *numwei
@property (nonatomic, strong) NSString *measure
@property (nonatomic, strong) NSString *sheshi
@property (nonatomic, strong) NSString *numlou
@property (nonatomic, strong) NSString *numall
@property (nonatomic, strong) NSString *ispersonal
@property (nonatomic, strong) NSString *money
@property (nonatomic, strong) NSString *qq
@property (nonatomic, strong) NSString *usage
@property (nonatomic, strong) NSString *color
@property (nonatomic, strong) NSString *mode
self.orderno = [[dictionary stringValueForKey:"orderno"];
self.status = [[dictionary stringValueForKey:"status"];
self.statusflag = [[dictionary stringValueForKey:"statusflag"];
self.id = [[dictionary stringValueForKey:"id"];
self.userid = [[dictionary stringValueForKey:"userid"];
self.companyid = [[dictionary stringValueForKey:"companyid"];
self.companyname = [[dictionary stringValueForKey:"companyname"];
self.areaid = [[dictionary stringValueForKey:"areaid"];
self.areaname = [[dictionary stringValueForKey:"areaname"];
self.rootid = [[dictionary stringValueForKey:"rootid"];
self.parentids = [[dictionary stringValueForKey:"parentids"];
self.channelid = [[dictionary stringValueForKey:"channelid"];
self.channelname = [[dictionary stringValueForKey:"channelname"];
self.isfinish = [[dictionary stringValueForKey:"isfinish"];
self.releasetime = [[dictionary stringValueForKey:"releasetime"];
self.title = [[dictionary stringValueForKey:"title"];
self.desc = [[dictionary stringValueForKey:"desc"];
self.gongsi = [[dictionary stringValueForKey:"gongsi"];
self.imageurl = [[dictionary stringValueForKey:"imageurl"];
self.infotype = [[dictionary stringValueForKey:"infotype"];
self.personname = [[dictionary stringValueForKey:"personname"];
self.personphone = [[dictionary stringValueForKey:"personphone"];
self.imagesList = [[dictionary stringValueForKey:"imagesList"];
self.year = [[dictionary stringValueForKey:"year"];
self.salary = [[dictionary stringValueForKey:"salary"];
self.edu = [[dictionary stringValueForKey:"edu"];
self.num = [[dictionary stringValueForKey:"num"];
self.age = [[dictionary stringValueForKey:"age"];
self.place = [[dictionary stringValueForKey:"place"];
self.estate = [[dictionary stringValueForKey:"estate"];
self.numshi = [[dictionary stringValueForKey:"numshi"];
self.numting = [[dictionary stringValueForKey:"numting"];
self.numwei = [[dictionary stringValueForKey:"numwei"];
self.measure = [[dictionary stringValueForKey:"measure"];
self.sheshi = [[dictionary stringValueForKey:"sheshi"];
self.numlou = [[dictionary stringValueForKey:"numlou"];
self.numall = [[dictionary stringValueForKey:"numall"];
self.ispersonal = [[dictionary stringValueForKey:"ispersonal"];
self.money = [[dictionary stringValueForKey:"money"];
self.qq = [[dictionary stringValueForKey:"qq"];
self.usage = [[dictionary stringValueForKey:"usage"];
self.color = [[dictionary stringValueForKey:"color"];
self.mode = [[dictionary stringValueForKey:"mode"];
Program ended with exit code: 0
这里只有NSString 类型的属性,是因为个人觉得 把一些 Int Float 等类型的数据转为NSString更方便以后的操作。 例如 int 类型的 数据是要在Label或其他控件上显示的 ,用的时候还需要转。 当然这个是个人喜好。
上面只是因我个人喜好结果输出的 ,灵活性太低。 可以根据自己的喜好改动一下。
等我学点web方面的知识以后 ,回来完善一下这个功能。