ios - 以预定义格式将 NSDictionary 转换为 NSString 并转换回来

标签 ios objective-c

我需要将 NSDictionary 转换为预定义格式的字符串格式,然后再转换回原始字典。该字符串必须采用预定义的格式。比如:

我做的很乏味:

字典转字符串:

+ (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;
    }
}
  1. 对于第一个函数,我可能会改为写入 dictionary[@"entityID"] = message.entityID。所以这仍然是将字典转换为字符串的问题。

  2. 我不想为我所有的实体都写这个,有什么简单的方法可以做到吗?

  3. 最后一个属性是“文本”,它可以包含任何字符,包括“=”和空白。这就是为什么我把它放在最后并构建“前缀字符串”的原因。但这很乏味。

  4. 顺序无关紧要。但如果按字母顺序排列就好了。

最佳答案

像这样:

字典到字符串:

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

关于ios - 以预定义格式将 NSDictionary 转换为 NSString 并转换回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22257437/

相关文章:

objective-c - 让这段代码更有效率?

ios - Grand Central Dispatch 和单元测试

ios - 仅以英文获取用户的城市

ios - 覆盖核心数据值

ios - 如何使我的 ReferenceWritableKeyPath 类通用

objective-c - iOS:卸载和弹出 View Controller

ios - 由于未捕获的异常而终止应用程序,原因 : 'attempt to delete row 3 from section 1 which only contains 0 rows before the update'

iOS:同步缩放和 3D 旋转动画

iphone - iOS 中的图像圆形环绕

objective-c - iOS5, Storyboard,ARC : Weird categories issue