【问题标题】:IOS: [NSDictionary initWithObjects:forKeys:]:IOS:[NSDictionary initWithObjects:forKeys:]:
【发布时间】:2017-07-21 05:57:10
【问题描述】:

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

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

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSDictionary initWithObjects:forKeys:]:对象计数 (2) 与计数不同 钥匙 (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;
    }
)

【问题讨论】:

  • 这是因为你的值是 nil,而 nsarray 接近 nil 的位置
  • 尝试用空值代替 nil

标签: ios objective-c object key nsdictionary


【解决方案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
}

【讨论】:

  • @Yatendra 您不能将 NULL 添加到 NSDictionary 中。我给你们两个例子。在第一个示例中,我尝试使用 NULL 添加它给我的错误。其次,我会将一些值传递给字符串,它的工作正常。还要检查我更新的代码以从字符串中删除 null。
  • @Yatendra 我发布了正确的解释以及您的解决方案。我的回答不应该是反对票。
  • @Yatendra 你应该发布一个答案,而不是评论。
  • @JamesP 有时候需要引导还没烤好的食物,我认为问题是ios的一般概念,所以我们只需要引导和理解。
猜你喜欢
  • 2017-07-23
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多