7

コアデータをjsonに変換しようとしています.これを機能させるのに苦労していますが、ほとんど機能する方法を見つけました.

私のコード:

NSArray *keys = [[[self.form entity] attributesByName] allKeys];
        NSDictionary *dict = [self.form dictionaryWithValuesForKeys:keys];
        NSLog(@"dict::%@",dict);

        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                           options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                             error:&error];

        if (! jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {
            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            NSLog(@"json::%@",jsonString);
        }

また、「フォーム」は次のとおりです。

 @property (strong, retain) NSManagedObject *form;

Coredata 属性の一部に NSIndexSet が保存されていることを除いて、これは正常に機能します。これは、JSON 書き込みで問題を引き起こします。今、私のインデックスセットはjsonに変換する必要がないので、辞書からすべてのインデックスを削除する方法があるかどうか疑問に思っていましたか? または、私が気付いていないこれを行うためのより良い方法があるかもしれません。

以下は dict の nslog の一部です。

...
    whereExtent = "";
    wiring =     (
    );
    wiring1 = "<NSIndexSet: 0x82b0600>(no indexes)";
    wiringUpdated = "<null>";
    yardFenceTrees = "<null>";
}

したがって、この場合、「wiring1」をdictから削除したいのですが、「動的」な方法で実行できる必要があります(「wiring1」という名前を使用して削除するのではありません)

4

3 に答える 3

20

値を削除できるようにするには、辞書がNSMutableDictionaryクラスのインスタンスである必要があります。

値を動的に削除するには、dict からすべてのキーを取得し、各キーのオブジェクトをテストして、不要なオブジェクトを削除します。

NSArray *keys = [dict allKeys];
for (int i = 0 ; i < [keys count]; i++)
 {
   if ([dict[keys[i]] isKindOfClass:[NSIndexSet class]])
   {
     [dict removeObjectForKey:keys[i]];
   }
}

注: 値の削除は高速列挙では機能しません。別の高速ハックとして、不要なオブジェクトを含まない新しい辞書を作成することもできます。

于 2013-11-05T16:10:10.620 に答える
8

NSDictionary の代わりに NSMutableDictionary を使用してください。コードは次のようになります。

NSMutableDictionary *dict = [[self.form dictionaryWithValuesForKeys:keys] mutableCopy]; //create dict
[dict removeObjectForKey:@"wiring1"]; //remove object

mutableCopy の使用を忘れないでください。

于 2013-11-05T15:47:45.893 に答える
1

このサンプル コードは、 を通過し、JSON セーフ プロパティのみを含むNSDictionary新しい を構築します。NSMutableDictionary

現時点では、再帰的には機能しません。たとえば、辞書に辞書または配列が含まれている場合、辞書自体を通過して修正するのではなく、削除しますが、追加するのは簡単です。

// Note: does not work recursively, e.g. if the dictionary contains an array or dictionary it will be dropped.
NSArray *allowableClasses = @[[NSString class], [NSNumber class], [NSDate class], [NSNull class]];
NSDictionary *properties = @{@"a":@"hello",@"B":[[NSIndexSet alloc] init]};
NSMutableDictionary *safeProperties = [[NSMutableDictionary alloc] init];

[properties enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
    BOOL allowable = NO;
    for (Class allowableClass in allowableClasses)          {
        if ([obj isKindOfClass:allowableClass])
        {
            allowable = YES;
            break;
        }
    }       
    if (allowable)
    {
        safeProperties[key] = obj;
    }
}];
NSLog(@"unsafe: %@, safe: %@",properties,safeProperties);
于 2013-11-05T16:23:37.627 に答える