gpt4 book ai didi

IOS: [NSDictionary initWithObjects:forKeys:]:

转载 作者:行者123 更新时间:2023-11-29 11:43:30 27 4
gpt4 key购买 nike

在我的项目中,我创建了一个包含对象和键的字典我为对象和键创建 NSArray

但是当我尝试创建字典时它会发送以下错误:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSDictionary initWithObjects:forKeys:]: count of objects (2) differs from count of keys (12)'

但我正在将所有 12 个对象发送到字典

我的代码是:

NSArray *keys = [NSArray arrayWithObjects: @"api_key", @"truck_id",@"driver_id", @"member_id",@"amount",@"net_amount",@"vendor_id",@"delivery_date",@"address_id", @"create_Id", @"transaction_id",@"Cart_Details", nil];

NSLog(@"Truck Id Str is %@",TruckIdStr);
NSLog(@"Driver Id is %@",DriverIdStr);
NSLog(@"Member Id is %@",Member_id);
NSLog(@"Amount is %@",amount);
NSLog(@"Grand Total is %@",GrandTotalString);
NSLog(@"Vendor Id is %@",vendor_id);
NSLog(@"delivery date is %@",delivery_date);
NSLog(@"Address Id is %@",RcvdAddressId);
NSLog(@"Create Id is %@",create_Id);
NSLog(@"Transaction Id is %@",Transaction_Id);
NSLog(@"Selected Item array is %@",SelectedItemsArray);


NSArray *objects = [NSArray arrayWithObjects:@"bf45c093e542f057c123ae7d6",TruckIdStr,DriverIdStr,Member_id,amount,GrandTotalString,vendor_id,delivery_date,RcvdAddressId,create_Id,Transaction_Id,SelectedItemsArray, nil];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSLog(@"Dictionary is %@",dictionary);

在控制台区域,它显示 nslog 值:

 Truck Id Str is 
Driver Id is (null)
Member Id is 269
Amount is 10
Grand Total is 30.06
Vendor Id is 447
delivery date is 2017/07/21
Address Id is 37
Create Id is 2017-07-21T05:43:06Z
Transaction Id is PAY-1RA1614620154883CLFYZI2Q
Selected Item array is (
{
TotalPrice = "0.06";
UnitPrice = "0.02";
VendorId = 447;
itemId = 75;
itemQuantity = 3;
name = "Green-Chile Burgers with Fried Eggs";
},
{
TotalPrice = 30;
UnitPrice = 10;
VendorId = 447;
itemId = 77;
itemQuantity = 3;
name = TestRohit11;
}
)

最佳答案

这是由于 NULL 值。

这是导致应用崩溃的代码:

 NSArray *keys = [NSArray arrayWithObjects: @"api_key", @"truck_id",@"driver_id", @"member_id",@"amount",@"net_amount",@"vendor_id",@"delivery_date",@"address_id", @"create_Id", @"transaction_id",@"Cart_Details", nil];

// @"DriverIdStr"
NSArray *objects = [NSArray arrayWithObjects:@"bf45c093e542f057c123ae7d6",@"TruckIdStr",NULL,@"Member_id",@"amount",@"GrandTotalString",@"vendor_id",@"delivery_date",@"RcvdAddressId",@"create_Id",@"Transaction_Id",@"SelectedItemsArray", nil];

如果我给出 NULL ,我的应用程序就会崩溃。

正确的代码:

NSArray *objects = [NSArray arrayWithObjects:@"bf45c093e542f057c123ae7d6",@"TruckIdStr",@"DriverIdStr",@"Member_id",@"amount",@"GrandTotalString",@"vendor_id",@"delivery_date",@"RcvdAddressId",@"create_Id",@"Transaction_Id",@"SelectedItemsArray", nil];

如果里面有NULL需要替换成Blank字符串

将 NULL 字符串替换为空白:

  NSString* strValue = NULL;
if (strValue == nil || strValue == (id)[NSNull null]) {
// Set Blank to string
strValue = @"";
} else {
// You don't have NULL in your string, so you can use it directly
}

关于IOS: [NSDictionary initWithObjects:forKeys:]:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45230041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com