オブジェクトを取得して、そのすべてのプロパティを 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;
}