在我的项目中,我创建了一个包含对象和键的字典 我为对象和键创建NSArray
但是当我尝试创建字典时,它会发送以下错误:
*由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'* - [NSDictionary initWithObjects:forKeys:]:对象的数量(2)与count的数量不同 键(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;
}
)
答案 0 :(得分:1)
由于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替换为空字符串。
将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
}