0

我有一些来自以下代码的意外结果:

- (NSData *)postDataWithDict:(NSDictionary *)postDict
{
    // Assume key is urlValid
    NSUInteger postCount = [postDict count];
    NSMutableArray *buildArray = [[NSMutableArray alloc] initWithCapacity:postCount];
    for (NSString *key in postDict) {
        //post data is key=value&key=value&key=value...

        // start with key
        NSMutableString *arrayLine = [NSMutableString stringWithString:key];

        [arrayLine appendString:@"="];

        // analyze and then append value
        id postValue = [postDict objectForKey:key];
        if ([postValue isKindOfClass:[NSNumber class]]) {
            NSString *valueString = [NSString stringWithFormat:@"%@",postValue];
            [arrayLine appendString:valueString];
        }
        else if ([postValue isKindOfClass:[NSString class]]) {
            NSString *urlEncodedString = [self urlEncodeValue:postValue];
            [arrayLine appendString:urlEncodedString];
        }
        else {

            NSLog(@"postKey: %@, postValue class:%@", key, [postValue class]);
            NSError *jsonError = nil;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postValue
                                                               options:0
                                                                 error:&jsonError];
            if (jsonError != nil) {
                NSLog(@"JSON serialization failed: %@ - %@", [jsonError localizedDescription], [jsonError userInfo]);
                NSLog(@"value: %@", postValue);
            }
            else {
                // need to urlencode
                NSString *stringifyJSON = [NSString stringWithUTF8String:[jsonData bytes]];
                NSString *urlJSONstring = [self urlEncodeValue:stringifyJSON];
                [arrayLine appendString:urlJSONstring];
            }
        }
        [buildArray addObject:arrayLine];
    }
    NSString *postString = [buildArray componentsJoinedByString:@"&"];
    NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];

    //testing
    NSLog(@"Post Dict: %@", postDict);
    NSLog(@"Post Array: %@", buildArray);
    NSLog(@"Post String: %@", postString);
    NSLog(@"Post Data: %@", [NSString stringWithUTF8String:[postData bytes]]);

    return postData;
}

我的//测试日志结果:

 Post Dict: {
    authenticationString = b3210c0bc6d2c47f4c2f7eeea12e063d;
    dataMode = updateSingle;
    dateCreated = "374300293.81108";
    dateModified = "374609294.313093";
    dateSynced = "374610683.588062";
    entityName = CommodityTypes;
    myName = 21;
    sortKey = 21;
    username = iPhoneAdamek;
    usernameString = iPhoneAdamek;
    uuidKey = "53403EAE-DD4F-4226-A979-316EF7F43991";
}

Post Dict 看起来不错。正是我想要的。

2012-11-14 13:31:23.640 FoodyU[11393:907] Post Array: (
    "myName=21",
    "dataMode=updateSingle",
    "dateSynced=374610683.588062",
    "uuidKey=53403EAE-DD4F-4226-A979-316EF7F43991",
    "sortKey=21",
    "dateModified=374609294.313093",
    "entityName=CommodityTypes",
    "dateCreated=374300293.81108",
    "authenticationString=b3210c0bc6d2c47f4c2f7eeea12e063d",
    "usernameString=iPhoneAdamek",
    "username=iPhoneAdamek"
)

后阵列看起来不错。字符串都设置为连接到 HTTP POST 字符串。

2012-11-14 13:31:23.641 FoodyU[11393:907] Post String: myName=21&dataMode=updateSingle&dateSynced=374610683.588062&uuidKey=53403EAE-DD4F-4226-A979-316EF7F43991&sortKey=21&dateModified=374609294.313093&entityName=CommodityTypes&dateCreated=374300293.81108&authenticationString=b3210c0bc6d2c47f4c2f7eeea12e063d&usernameString=iPhoneAdamek&username=iPhoneAdamek

帖子字符串看起来不错。我准备将其转换为数据以在[NSMutableURLRequest setHTTPBody:postData].

2012-11-14 13:31:23.643 FoodyU[11393:907] Post Data: myName=21&dataMode=updateSingle&dateSynced=374610683.588062&uuidKey=53403EAE-DD4F-4226-A979-316EF7F43991&sortKey=21&dateModified=374609294.313093&entityName=CommodityTypes&dateCreated=374300293.81108&authenticationString=b3210c0bc6d2c47f4c2f7eeea12e063d&usernameString=iPhoneAdamek&username=iPhoneAdamekoneAdamek;
    usernameString = iPhoneAdamek;
    uuidKey = "53403EAE-DD4F-4226-A

怎么回事???怎么&username=iPhoneAdamek变成的&username=iPhoneAdamekoneAdamek; usernameString = iPhoneAdamek; uuidKey = "53403EAE-DD4F-4226-A

我对可可相当陌生。有什么问题吗:

NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];

或者

NSLog(@"Post Data: %@", [NSString stringWithUTF8String:[postData bytes]]);
4

1 回答 1

3

你不应该使用NSLogas NSData

NSLog(@"Post Data: %@", [NSString stringWithUTF8String:[postData bytes]]);

而是将其用作,

NSLog(@"Post Data: %@", [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding]);

[NSString stringWithUTF8String:[postData bytes]]总是返回意想不到的结果。

根据字节的文档

bytes: 返回一个指向接收者内容的指针。

根据Apple 文档stringWithUTF8String

stringWithUTF8String: 返回通过从给定的 UTF8 编码字节 C 数组中复制数据而创建的字符串。 参数:bytes- UTF8 编码中以 NULL 结尾的 C 字节数组。

因此,当您使用时[postData bytes],它不是以NULL 结尾的,因此当您使用 with 时,会stringWithUTF8String返回写入内存中的数据,直到遇到 NULL 终止。

于 2012-11-14T19:14:57.883 回答