ios - 无法在 objective-c 中创建字典?

标签 ios objective-c dictionary xcode6

我无法在 objective-c 中创建字典。我收到警告

Incompatible integer to pointer conversion sending nsinteger to parameter

下面是我的代码。

 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
}

最佳答案

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

[NSNumber numberWithInt:tmpRepoId]

或者,在现代 Obj-C 中:

@(tmpRepoId)

并将其添加到字典中。

一般来说,我想推荐使用现代 Obj-C 以获得更好的可读性,例如:

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

也可以考虑使用 NSInteger 而不是 int。

关于ios - 无法在 objective-c 中创建字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42438519/

相关文章:

python - 为字典列表中的两个键创建唯一的字典

ios - 无法获取 NSDictionary 键的值,出现错误 : The process has been returned to the state before expression evaluation

ios - Ansible regex_findall 多个字符串

android - AES 加密数据库条目大小

ios - 如何在AVPlayer或任何其他受支持的 Controller 中播放.mpeg和.x-wav格式的音频文件

c# - ConcurrentDictionary 添加或更新列表

ios - 更改 UIBarButtonItem 标题看起来很糟糕

objective-c - Swift 中的 EKEventStore enumerateEventsMatchingPredicate

iphone - UITableViewController 第二次加载时崩溃

python-3.x - 如何重复追加字符串