0

オブジェクトを取得して、そのすべてのプロパティを PLIST に書き出せるようにしたいと考えています。私はこれまでのところ:

// Get the properties of the parent class
NSMutableArray *contentViewPropertyNames = [self propertyNamesOfObject:[contentView superclass]];

// Add the properties of the content view class
[contentViewPropertyNames addObjectsFromArray:[self propertyNamesOfObject:contentView]];

// Get the values of the keys for both the parent class and the class itself
NSDictionary *keyValuesOfProperties = [contentView dictionaryWithValuesForKeys:contentViewPropertyNames];

// Write the dictionary to a PLIST
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathAndFileName = [documentsDirectory stringByAppendingPathComponent:[dataFileName stringByAppendingString:@".plist"]];

[keyValuesOfProperties writeToFile:pathAndFileName atomically:YES];

PLIST に準拠していないプロパティがいくつか含まれているため、これを PLIST に書き込むことができないことを除けば、すべて問題ありません。writeToFile:atomically:失敗して が返されますNO

PLIST にシリアライズ可能なプロパティのみをシリアライズする、またはオブジェクトの基本クラスを変更してこれを機能させる良い方法はありますか?

問題なくバイナリ ファイルにアーカイブできるNSCodingことはわかっていますが、MacOS アプリケーションと iOS アプリの間で出力を転送できるようにする必要があるため、プラットフォームに依存しない中間形式を使用する必要があります。

もちろん、要点を完全に見逃している可能性があります。もしあれば教えてください。いつものように、どんな助けも役に立ちます。

よろしくお願いします

デイブ

PS

オブジェクトのプロパティ名を取得する方法は次のとおりです。

- (NSMutableArray *)propertyNamesOfObject:(id)object {
    NSMutableArray *propertyNames = nil;
    unsigned int count, i;
    objc_property_t *properties = class_copyPropertyList([object class], &count);

    if (count > 0) {
        propertyNames = [[[NSMutableArray alloc] init] autorelease];

        for(i = 0; i < count; i++) {
            objc_property_t property = properties[i];
            const char *propName = property_getName(property);
            if(propName) {
                NSString *propertyName = [NSString stringWithCString:propName encoding:NSUTF8StringEncoding];
                [propertyNames addObject:propertyName];
            }
        }
    }
    free(properties);

    return propertyNames;
}
4

1 に答える 1

0

私が最近書いたこの関数を同様の状況で適用できるかどうかを確認してください。

// Property list compatible types: NSString, NSData, NSArray, or NSDictionary */
- (BOOL)isPlistCompatibleDictionary:(NSDictionary *)dict {
    NSSet *plistClasses = [NSSet setWithObjects:[NSString class], [NSData class],
        [NSArray class], [NSDictionary class], [NSDate class], 
        [NSNumber class], nil];

    BOOL compatible = YES;
    NSArray *keys = [dict allKeys];
    for (id key in keys) {
        id obj = [dict objectForKey:key];
        if (![plistClasses containsObject:[obj class]]) {
            NSLog(@"not plist compatible: %@", [obj class]);
            compatible = NO;
            break;
        }
    }

    return compatible;
}
于 2012-10-04T21:34:24.510 に答える