2016-03-19 17 views
0

私は初心者であり、最近この問題で大きな問題を抱えています。 アプリケーションからサーバにNSDictnonaryデータを渡したい場合があります。また、ユーザがnilオブジェクトを削除したいオプションを選択していない場合もあります。 私は正しい方法であるように思われるこのスレッドを見てきましたが、私のコードで実装するのに成功しませんでした。 How to remove a null value from NSDictionaryNSDictionaryからヌル値を直接取り除く方法

私の推測では、私の辞書のコード私のNSDictonary Here'sに直接Nullにコードを実装することです

 -(NSDictionary*)parametersForCreateActivities 

    { 
    NSString *token = [[A0SimpleKeychain keychain] stringForKey:tokenConstant]; 
    NSString *userId = [[A0SimpleKeychain keychain] stringForKey:child_id]; 
    NSString *userCreate = [[NSUserDefaults standardUserDefaults] objectForKey:@"CreateTitle"]; 
    NSString *createDescription = [[NSUserDefaults standardUserDefaults] objectForKey:@"DescriptionText"]; 
    NSString *createTimestart = [[NSUserDefaults standardUserDefaults] objectForKey:@"TimeStartString"]; 
    NSString *createTimestop = [[NSUserDefaults standardUserDefaults] objectForKey:@"TimeStopString"]; 
    NSString *createImage = [[NSUserDefaults standardUserDefaults] objectForKey:@"DefaultcreateImageID"]; 

    NSDictionary *parameters; 
    if (userId && token) { 
     parameters = @{child_id: userId, tokenConstant:token, activity_name :userCreate, create_Description :createDescription, create_Timestart :createTimestart, create_Timestop :createTimestop, create_Image :createImage}; 

    } 
    return parameters; 
} 

私の推測では、それがnilオブジェクトのコードのどこかでチェックし、削除しなければならないことですテーマ。しかし、私は実際にコードをどのようにフォーマットするかを考え出すのに苦労しています。 私はコードがこのようなものでなければならないと思っていますが、どこに配置するのか、どのようにフォーマットするのか分かりません。

NSMutableDictionary *dict = [parametersForCreateActivities mutableCopy]; 
NSArray *keysForNullValues = [dict allKeysForObject:[NSNull null]]; 
[dict removeObjectsForKeys:DefaultcreateImageID]; 

答えて

0
NSMutableDictionary *mutableDict = [testDictionary mutableCopy]; 
    for (NSString *key in [testDictionary allKeys]) { 
     if ([testDictionary[key] isEqual:[NSNull null]]) { 
     mutableDict[key] = @"";//or [NSNull null] or whatever valueyou want to change it to 
    } 
} 
testDictionary = [mutableDict copy]; 
NSLog(@"%@",testDictionary); 
0

コード

NSMutableDictionary *yourDictionary; // Your dictionary object with data 

NSMutableDictionary *updatedDic = [yourDictionary mutableCopy]; 
for (NSString *key in [yourDictionary allKeys]) { 
    if ([yourDictionary[key] isEqual:[NSNull null]] || [yourDictionary[key] isKindOfClass:[NSNull class]]) { 
     updatedDic[key] = @""; 
    } 
} 
yourDictionary = [updatedDic copy]; 
NSLog(@"%@",yourDictionary); 
+0

の下に試してみてください私はあなたのソリューションを試みたが、 - 私はエラーメッセージinitWithObjects取得:forKeysを:カウント:]:オブジェクトからはnilオブジェクトを挿入しようとする試みを、[6]」 –

関連する問題