将NSString转换为Objective C中的unsigned。

4

我想将一个 NSString 转换为 unsigned 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];

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

你的问题是什么?另外,我认为你的意思是 unsigned int 而不是 unsigned - Tom van der Woerdt
@TomvanderWoerdt,我认为在Objective-C中int是可选的,如果你只写unsigned,它的意思是unsigned int。 - Siddiqui
1
@Siddiqui 在 C 语言中是可选的,但这并不是一个好习惯。 - Tom van der Woerdt
有人能详细解释一下我的问题吗?如何做? - user1501354
提供的任何答案是否有帮助或实际回答了您的问题? - trumpetlicks
如需请求支持从NSString读取无符号值,请访问http://bugreport.apple.com并针对组件“Foundation | X”提交radar:// 2264733的副本。 - Quinn Taylor
3个回答

3
在我的情况下,我需要实现类似于这样的东西(根据您的需求进行调整):
NSString *myStringObject = [NSString stringWithFormat:@"%u", someObject.hash];
NSUInteger hashValue = (NSUInteger)myStringObject.longLongValue;

我最初使用了myStringObject.intValue,如下所建议的,但是当被假定为有符号数时,我的整数被“截断”了,因为它们太大了(但是无符号数由于多一个比特而不会太大)。使用myStringObject.longLongValue解决了我的问题。

值得注意的是,我的字符串最初是使用NSUIntegers(无符号整数)创建的,因此我不必担心解析过大的整数。


2
你为什么不直接使用 longLongValue 呢? - Hot Licks
那可能是更好的方法,不是吗?我已经相应地调整了我的答案。 - Michael

2
假设您的数组中有一个字符串:
unsigned int order = (unsigned int)[[feeArray objectAtIndex:0] intValue];

实际上,没有NSString方法可以直接转换为无符号整数值。但是,如果您可以保证没有任何值是带符号整数,则上述方法应该适用于您。


1
我正在使用这个查询在PayPal中进行并行支付。 - user1501354
目前我能想到的唯一可能是,如果这个方法对你不起作用,那么你的数组中可能没有NSString对象。你是如何生成feeArray和amountArray的呢? - trumpetlicks

1

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

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

或者 int

int order = [[freeArray objectAtIndex:0] intValue];
NSLog(@"order=%d", order);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接