2017-03-02 35 views
0

我使用下面的代码在应用程序中创建nsdictionary。字典没有正确创建?

NSDictionary *allImportDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
          [NSNumber numberWithInt:containerId], @"containerId", 
           [NSNumber numberWithInt:docTypeId], @"docTypeId",filextension, @"fileExt",metaDataFiellds, @"metaDataFields",[NSNumber numberWithInt:tmpRepoId], @"repositoryId",[NSNumber numberWithInt:1], @"pagesCount", 
           tmpSessionID, @"sessionId", fileName, @"title",guid, @"tmpFileName",[NSNumber numberWithInt:tmpUserID], @"userId", 
          nil]; 

但它创造的字典如下allImportDict的

印刷描述:

{ 
    containerId = 2; 
    docTypeId = 1; 
} 

请指引我为什么它不包括所有的按键作为最关键的缺失?

+0

检查'filextension,@ “fileExt”' –

+0

什么是这个问题? – iOSGuy

+1

'filextension'似乎是'nil',然后停止字典的构建。 – Larme

回答

0

你的NSDictionary“fileExt”参数包含“nil”value.Plz,更新它并检查。

0

我刚刚添加了验证值,并用NSMutableDictionary替换了NSDictionary。任何空值都会破坏你的代码。您可以复制粘贴以下代码。

NSMutableDictionary *allImportDict = [[NSMutableDictionary alloc] init]; 

    if(containerId) { 
     [allImportDict setObject:[NSNumber numberWithInt:containerId] forKey:@"containerId"]; 
    } 

    if(docTypeId) { 
     [allImportDict setObject:[NSNumber numberWithInt:docTypeId] forKey:@"docTypeId"]; 
    } 

    if([self canUseString:filextension]) { 
     [allImportDict setObject:filextension forKey:@"fileExt"]; 
    } 

    if([self canUseString:metaDataFiellds]) { 
     [allImportDict setObject:metaDataFiellds forKey:@"metaDataFiellds"]; 
    } 

    if(tmpRepoId) { 
     [allImportDict setObject:[NSNumber numberWithInt:tmpRepoId] forKey:@"tmpRepoId"]; 
    } 

    [allImportDict setObject:[NSNumber numberWithInt:1] forKey:@"pagesCount"]; 

    if([self canUseString:tmpSessionID]) { 
     [allImportDict setObject:tmpSessionID forKey:@"sessionId"]; 
    } 

    if([self canUseString:fileName]) { 
     [allImportDict setObject:fileName forKey:@"fileName"]; 
    } 

    if([self canUseString:guid]) { 
     [allImportDict setObject:guid forKey:@"guid"]; 
    } 

    if(tmpUserID) { 
     [allImportDict setObject:[NSNumber numberWithInt:tmpUserID] forKey:@"userId"]; 
    } 

    NSLog(@"allImportDict: %@", allImportDict);