简体   繁体   English

将两个 NSarray 转换为 NSString

[英]Convert two NSarray to NSString

Hi so I have two NS Arrays in my view controller, with both the same number of count.嗨所以我有两个 NS Arrays 在我看来 controller,两个计数相同。 btw this is for submitting for upload.顺便说一句,这是为了提交上传。

barcodeItems = [[NSMutableArray alloc] init];

barcodeItemsQuantity = [[NSMutableArray alloc] init];

Once the arrays have been populated this method will be called,填充 arrays 后,将调用此方法,

-(void) performUploadOperation {
parameters = [NSMutableDictionary dictionary];

// Add barcodes and quantity
if (barcodeItems != nil || barcodeItemsQuantity != nil) {

    [parameters setValue:barcodeItems forKey:@"upc"];
    [parameters setValue:barcodeItemsQuantity forKey:@"upcQuantity"];

   }
}

Once this is submitted another method will be called to convert it to nsstring提交后,将调用另一个方法将其转换为 nsstring

NSArray* upc = [parameters objectForKey:@"upc"];
NSArray* upcQuantity = [parameters objectForKey:@"upcQuantity"];

NSString* sample = [NSString stringWithFormat:@"{\"upc_list\" : { \"%@\": %@}}", upc, upcQuantity];
NSLog(@"%@", sample);

The result is this结果是这样的

{"upc_list" : { "(
    76239878,
    0827755090991,
    76239878
)": (
    12,
    23,
    32
)}}

and what I want to achieve is something like this.我想要实现的是这样的。 both arrays have the same count always arrays 始终具有相同的计数

{"upc_list" : { "
   (
    "76239878" : 12,
    "0827755090991" : 23,
    "76239878" : 32
   )
 }}

@jordanzee: You can use the below code to get your expected result. @jordanzee:您可以使用以下代码来获得预期的结果。

NSArray *upc = [parameters objectForKey:@"upc"];
NSArray *upcQuantity = [parameters objectForKey:@"upcQuantity"];

NSMutableDictionary *aMutDict = [NSMutableDictionary dictionary];
for(NSInteger aIndex = 0; aIndex < upc.count; aIndex++){
   [aMutDict setValue:upcQuantity[aIndex] forKey:upc[aIndex]];
}

NSString *sample = [NSString stringWithFormat:@"{\"upc_list\" : %@}", aMutDict];
NSLog(@"%@", sample);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM