4

我已经改变了我的问题。

我想将 a 转换NSStringunsigned int.

为什么?因为我想在 PayPal 中进行并行付款。

下面我给出了我想将 转换NSString为无符号整数的编码。

我的查询是:

    //optional, set shippingEnabled to TRUE if you want to display shipping
    //options to the user, default: TRUE
    [PayPal getPayPalInst].shippingEnabled = TRUE;

    //optional, set dynamicAmountUpdateEnabled to TRUE if you want to compute
    //shipping and tax based on the user's address choice, default: FALSE
    [PayPal getPayPalInst].dynamicAmountUpdateEnabled = TRUE;

    //optional, choose who pays the fee, default: FEEPAYER_EACHRECEIVER
    [PayPal getPayPalInst].feePayer = FEEPAYER_EACHRECEIVER;

    //for a payment with multiple recipients, use a PayPalAdvancedPayment object
    PayPalAdvancedPayment *payment = [[PayPalAdvancedPayment alloc] init];
    payment.paymentCurrency = @"USD";

    // A payment note applied to all recipients.
    payment.memo = @"A Note applied to all recipients";

    //receiverPaymentDetails is a list of PPReceiverPaymentDetails objects
    payment.receiverPaymentDetails = [NSMutableArray array];

    NSArray *emailArray = [NSArray arrayWithObjects:@"abc@def.com",@"def@abc.com", nil];

    for (int i = 1; i <= 2; i++) {
        PayPalReceiverPaymentDetails *details = [[PayPalReceiverPaymentDetails alloc] init];

        // Customize the payment notes for one of the three recipient.
        if (i == 2) {
            details.description = [NSString stringWithFormat:@"Component %d", i];
        }

        details.recipient = [NSString stringWithFormat:@"%@",[emailArray objectAtIndex:i-1]];

        unsigned order;

        if (i==1) {
            order = [[feeArray objectAtIndex:0] unsignedIntValue];
        }
        if (i==2) {
             order = [[amountArray objectAtIndex:0] unsignedIntValue];
        }

        //subtotal of all items for this recipient, without tax and shipping
        details.subTotal = [NSDecimalNumber decimalNumberWithMantissa:order exponent:-4 isNegative:FALSE];

        //invoiceData is a PayPalInvoiceData object which contains tax, shipping, and a list of PayPalInvoiceItem objects
        details.invoiceData = [[PayPalInvoiceData alloc] init];

        //invoiceItems is a list of PayPalInvoiceItem objects
        //NOTE: sum of totalPrice for all items must equal details.subTotal
        //NOTE: example only shows a single item, but you can have more than one
        details.invoiceData.invoiceItems = [NSMutableArray array];
        PayPalInvoiceItem *item = [[PayPalInvoiceItem alloc] init];
        item.totalPrice = details.subTotal;
        [details.invoiceData.invoiceItems addObject:item];

        [payment.receiverPaymentDetails addObject:details];
    }

    [[PayPal getPayPalInst] advancedCheckoutWithPayment:payment];

谁能告诉我如何进行这种转换?

提前致谢和问候。

4

3 回答 3

3

就我而言,我必须实现这样的东西(适应您的需要):

NSString *myStringObject = [NSString stringWithFormat:@"%u", someObject.hash];
NSUInteger hashValue = (NSUInteger)myStringObject.longLongValue;

我最初使用myStringObject.intValue,如下所示,但后来我的整数被“剪裁”了,因为它们在假设有符号时太大(但由于额外的位而不是太大的无符号)。使用myStringObject.longLongValue为我解决了这个问题。

我应该注意我的字符串最初是使用 NSUIntegers(无符号整数)创建的,所以我不必担心解析太大的整数。

于 2013-06-28T16:20:06.763 回答
2

假设您的数组中有一个字符串:

unsigned int order = (unsigned int)[[feeArray objectAtIndex:0] intValue];

确实没有 NSString 方法可以直接转换为 unsigned int 值。但是,如果您可以保证您的值都不是带符号的整数,那么上述内容应该适合您。

于 2012-07-09T12:44:56.070 回答
1

没有转换为无符号整数,但有转换为有符号整数 ( NSInteger):

NSInteger order = [[freeArray objectAtIndex:0] integerValue];
NSLog(@"order=%ld", order);

int

int order = [[freeArray objectAtIndex:0] intValue];
NSLog(@"order=%d", order);
于 2012-07-09T12:45:33.800 回答