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

时间:2014-03-07 18:02:33

标签: 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. 最后一个属性是'text',它可以包含任何字符,包括'='和空白。这就是为什么我把它放在最后并构造'前缀字符串。但这很乏味。

  4. 顺序无关紧要。但如果它按字母顺序排列会很好。

3 个答案:

答案 0 :(得分:1)

这样的事情:

字典到字符串:

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

答案 1 :(得分:0)

您的“将字典转换为字符串”块没有字典,所以我不知道如何帮助您,因为您的邮件对象可以是所有内容......

您可以使用“字符串到字典”转换:

+ (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;
    }
}

答案 2 :(得分:0)

目前还不清楚你最终想做什么,但在我看来,你可能会过度思考这一点。我在这里要做的是将字典直接转换为plist - 因为这个特性是内置的(例如,它是writeToFile:所做的)。您可以写入文件并将文件作为NSString读取,并且您已经获得了字典的XML版本。

或者,如果您致电NSKeyedArchiver archivedDataWithRootObject:,您可以在一个步骤中将字典转换为NSData,适用于加密和发送到另一台计算机

无论哪种方式,逆转这个过程同样微不足道。