2017-02-24 101 views
0

我無法在目標c中創建字典。我得到的警告,無法在目標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

哪條線你得到了錯誤 – iOS

+4

NSDictionary不能存儲標量值(比如BOOL,NSInteger等),它只能存儲對象。你必須將你的標量值封裝到NSNumber中才能存儲它們:[NSNumber numberWithInteger containerId] – iOS

+0

你知道沒有人像現在這樣寫Objective-C。你聽說過字典文字或下標訪問嗎? – deadbeef

回答

1

的NSDictionary只接受對象,沒有基本類型。你需要換你INT一個NSNumber

[NSNumber numberWithInt:tmpRepoId]

,或者在現代的OBJ-C:

@(tmpRepoId)

這添加到字典中。

一般情況下,我想推薦使用現代的OBJ-C爲更好的可讀性,例如:

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

還要考慮使用的不是int NSInteger的。