无法在目标c中创建字典?

时间:2017-02-24 12:21:18

标签: ios objective-c dictionary xcode6

我无法在目标c中创建字典。我正在收到警告

  

将nsinteger发送到参数

的整数与指针转换不兼容

以下是我的代码。

 NSString *tmpSessionID = [[[AppSettings sharedAppSettings] getUserSession] objectForKey:@"UserSessionId"];
  int tmpRepoId = [[[[AppSettings sharedAppSettings] getUserRepository] objectForKey:@"RepositoryId" ]intValue];
  int tmpUserID = [[[[AppSettings sharedAppSettings] getUserSession] objectForKey:@"UserId"]intValue];
  NSDictionary * fmDic = [[AppSettings sharedAppSettings] getFolderModel];
  int containerId = containerId = [[fmDic objectForKey:@"ContainerId"]intValue];
  int docTypeId = selectedDcoumentType.docTypeId;
  NSDictionary *metaData = [[NSDictionary alloc] initWithObjectsAndKeys:
                       selectFileIndices.columnId, @"ColumnId",
                       indexValue, @"Value",
                       nil];
  NSMutableArray *metaArray  = [[NSMutableArray alloc]init];
  [metaArray addObject:metaData];
  NSDictionary *metaDataFiellds = [[NSDictionary alloc] initWithObjectsAndKeys:
                                   metaArray, @"metaDataFields",
                                   nil];

  NSDictionary *allImportDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                            containerId, @"containerId",
                            docTypeId, @"docTypeId",selectFileIndices.transText, @"fileExt",metaDataFiellds, @"metaDataFields",tmpRepoId, @"repositoryId",1, @"pagesCount",
                                 tmpSessionID, @"sessionId", @"1487918350642.jpg", @"title",guid, @"tmpFileName",tmpUserID, @"userId",
                            nil];
  NSLog(@"dictionary is %@",allImportDict);

我想创建一个如下所示的json:

{
  "containerId": 2,
  "docTypeId": 1,
  "fileExt": ".jpg",
  "metaDataFields": [
    {
      "ColumnId": 1,
      "Value": "23"
    }
  ],
  "pagesCount": 1,
  "repositoryId": 1,
  "sessionId": "1587c3c9-f261-4749-ace4-c134d031ec38",
  "title": "1487918350642.jpg",
  "tmpFileName": "5f8a0340-bd65-476c-b377-67fa19121203",
  "userId": 5
}

1 个答案:

答案 0 :(得分:1)

NSDictionary仅接受对象,不接受基本类型。您需要将int包装在NSNumber中

[NSNumber numberWithInt:tmpRepoId]

或者,在现代的Obj-C中:

@(tmpRepoId)

并将其添加到字典中。

一般来说,我建议使用现代的Obj-C以提高可读性,例如:

int containerId fmDic[@"ContainerId"].intValue;
NSDictionary *metaDataFiellds = @{ @"metaDataFields" : metaArray };

还要考虑使用NSInteger而不是int。