我需要以预定义的格式转换为字符串格式,然后转换回原始字典。字符串必须是预定格式的。例如:
我所做的非常乏味:
将字典转换为字符串:
+ (NSString *)cipherTextWithMessage:(Message *)message {
NSString *entity = @"Message";
NSString *entityID = message.entityID;
NSString *roomID = message.room.entityID;
NSString *userID = message.sender.userID;
NSString *datetime = [[self private_dateFormatter] stringFromDate:message.datetime];
NSNumber *selfDestructive = message.selfDestructive;
NSString *status = message.status;
NSString *type = message.type;
NSString *originalBlobID = message.originalBlobID;
NSString *thumbnailBlobID = message.thumbnailBlobID;
NSString *text = message.text;
NSString *posx = [Format posStringWithNumber:message.posx];
NSString *posy = [Format posStringWithNumber:message.posy];
NSString *link = message.link;
NSString *plainText = [NSString stringWithFormat:@"entity=%@ entityID=%@ roomID=%@ userID=%@ datetime=%@ selfDestructive=%@ status=%@ type=%@ originalBlobID=%@ thumbnailBlobID=%@ posx=%@ posy=%@ link=%@ text=%@", entity, entityID, roomID, userID, datetime, selfDestructive, status, type, originalBlobID, thumbnailBlobID, posx, posy, link, text];
return [self private_encrypt:plainText];
}
将字符串转换为字典
+ (NSMutableDictionary *)infoWithCipherText:(NSString *)cipherText {
NSString *plainText = [self private_decrypt:cipherText];
if ([plainText hasPrefix:[NSString stringWithFormat:@"entity=%@", @"Message"]]) {
NSArray *array = [plainText componentsSeparatedByString:@" "];
NSString *entity = [((NSString *)array[0]) substringFromIndex:@"entity=".length];
NSString *entityID = [((NSString *)array[1]) substringFromIndex:@"entityID=".length];
NSString *roomID = [((NSString *)array[2]) substringFromIndex:@"roomID=".length];
NSString *userID = [((NSString *)array[3]) substringFromIndex:@"userID=".length];
NSString *datetime = [((NSString *)array[4]) substringFromIndex:@"datetime=".length];
NSString *selfDestructive = [((NSString *)array[5]) substringFromIndex:@"selfDestructive=".length];
NSString *status = [((NSString *)array[6]) substringFromIndex:@"status=".length];
NSString *type = [((NSString *)array[7]) substringFromIndex:@"type=".length];
NSString *originalBlobID = [((NSString *)array[8]) substringFromIndex:@"originalBlobID=".length];
NSString *thumbnailBlobID = [((NSString *)array[9]) substringFromIndex:@"thumbnailBlobID=".length];
NSString *posx = [((NSString *)array[10]) substringFromIndex:@"posx=".length];
NSString *posy = [((NSString *)array[11]) substringFromIndex:@"posy=".length];
NSString *link = [((NSString *)array[12]) substringFromIndex:@"link=".length];
NSString *pref = [NSString stringWithFormat:@"entity=%@ entityID=%@ roomID=%@ userID=%@ datetime=%@ selfDestructive=%@ status=%@ type=%@ originalBlobID=%@ thumbnailBlobID=%@ posx=%@ posy=%@ link=%@ text=", entity, entityID, roomID, userID, datetime, selfDestructive, status, type, originalBlobID, thumbnailBlobID, posx, posy, link];
NSString *text = [plainText substringFromIndex:pref.length];
NSMutableDictionary *info = [NSMutableDictionary dictionary];
info[@"entity"] = entity;
info[@"entityID"] = entityID;
info[@"roomID"] = roomID;
info[@"userID"] = userID;
info[@"datetime"] = [[self private_dateFormatter] dateFromString:datetime];
info[@"selfDestructive"] = [NSNumber numberWithBool:selfDestructive.boolValue];
info[@"status"] = status;
info[@"type"] = type;
info[@"originalBlobID"] = originalBlobID;
info[@"thumbnailBlobID"] = thumbnailBlobID;
info[@"posx"] = [Format posWithPosString:posx];
info[@"posy"] = [Format posWithPosString:posy];
info[@"link"] = link;
info[@"text"] = text;
return info;
}
}
对于第一个功能,我可能会写入
dictionary[@"entityID"] = message.entityID
。因此,这仍在将字典转换为字符串问题。我不想为我所有的实体写这个,有什么简单的方法吗?
最后一个属性是"文本",它可以包含任何字符,包括'='和空白。这就是为什么我将其放在最后并构造'前缀字符串。但这很乏味。
订单无关紧要。但是,如果按字母顺序排列,那将是一件好事。
类似的东西:
字典的字典:
NSMutableString *result = [NSMutableString string];
NSDictionary *dict = ... // your dictionary
for (NSString *key in [dict allKeys]) {
id value = dict[key];
if (result.length) {
[result appendString:@" "];
}
[result appendFormat:@"%@=%@", key, [value description]];
}
// result has the desired string
字典字符串:
NSMutableDictionary dict = [NSMutableDictionary dictionary];
NSString *text = ... // the string created from the above
NSArray *array = [plainText componentsSeparatedByString:@" "];
for (NSString *pair in array) {
NSArray *parts = [pair componentsSeparatedByString:@"="];
dict[parts[0]] = parts[1];
}
// dict is now populated with the keys and values
尚不清楚您最终要做什么,但在我看来,您可能会过度思考。我在这里要做的是将字典直接转换为plist-因为此功能是在构建的(例如,这就是writeToFile:
所做的)。您可以写入文件并读取文件为NSSTRING,PRESTO,您的字典有一个XML版本。
另外,如果您致电NSKeyedArchiver archivedDataWithRootObject:
,则可以单个步骤将字典转换为NSDATA,适合加密并发送到另一台计算机
无论哪种方式,反向过程都同样微不足道。
您的" 将字典转换为字符串" block没有字典,所以我不知道如何帮助您,因为您的消息对象可以是每个事物。。
您可以使用" 字符串到字典"转换:
+ (NSMutableDictionary *)infoWithCipherText:(NSString *)cipherText {
NSString *plainText = [self private_decrypt:cipherText];
if ([plainText hasPrefix:[NSString stringWithFormat:@"entity=%@", @"Message"]]) {
NSArray *array = [plainText componentsSeparatedByString:@" "];
NSMutableDictionary *info = [NSMutableDictionary dictionary];
for (NSString *keyvalString in array) {
NSArray *keyvalArray = [keyvalString componentsSeparatedByString:@"="];
if ([keyvalArray count] == 2) {
info[[keyvalArray firstObject]] = [keyvalArray lastObject];
}
}
return info;
} else {
return nil;
}
}