1

这是 NSDictionary 的输出:

{
    client =     {
        environment = mock;
        "paypal_sdk_version" = "1.0.3";
        platform = iOS;
        "product_name" = "PayPal iOS SDK";
    };
    payment =     {
        amount = "39.95";
        "currency_code" = USD;
        "short_description" = "Awesome saws";
    };
    "proof_of_payment" =     {
        "adaptive_payment" =         {
            "app_id" = "APP-1245783590";
            "pay_key" = "AP-70M62356425642W";
            "payment_exec_status" = COMPLETED;
            timestamp = "2012-03-03T15:53:55Z";
        };
    };

它来自贝宝自适应支付。我最终这样做了:

- (void)verifyCompletedPayment:(PayPalPayment *)completedPayment {

    NSDictionary *pOPDictionary = [NSDictionary dictionaryWithDictionary:completedPayment.confirmation];
    NSLog(@"pOPDictionary: %@",pOPDictionary);

    NSDictionary *subDictionary = [pOPDictionary objectForKey:@"proof_of_payment"];
    NSDictionary *sub2Dictionary = [subDictionary objectForKey:@"adaptive_payment"];
    NSString *proofString = [sub2Dictionary objectForKey:@"payment_exec_status"];

    if ([proofString isEqualToString:@"COMPLETED"]) {
        NSLog(@"Payment Completed Successfully");
    } else {
        NSLog(@"Payment Error");
    }

    NSData *confirmation = [NSJSONSerialization dataWithJSONObject:completedPayment.confirmation options:0 error:nil];

}

有没有更有效的方法从字典字典中获取该键?我只需要那把钥匙。所以我不认为我应该循环或循环通过其他人。

4

1 回答 1

0

如何简化代码有两种不同的可能性。您可以使用“现代 Objective-C 下标语法”(有关更多信息,请参阅http://clang.llvm.org/docs/ObjectiveCLiterals.html):

NSDictionary *pOPDictionary = completedPayment.confirmation;
NSString *proofString = pOPDictionary[@"proof_of_payment"][@"proof_of_payment"][@"payment_exec_status"];

或键值编码:

NSDictionary *pOPDictionary = completedPayment.confirmation;
NSString *proofString = [pOPDictionary valueForKeyPath:@"proof_of_payment.proof_of_payment.payment_exec_status"];

我不认为一个比另一个有优势,这只是你选择哪一个的品味问题。

于 2013-04-13T19:23:16.907 回答