如何为Xcode 6.x创建plist的自定义结构定义?

时间:2015-02-25 08:04:56

标签: ios objective-c xcode plugins plist

更明确一点,当我打开plist编辑器并右键单击时,我可以从随Xcode 6安装的可用列表类型列表中选择一个属性列表类型。如果我可以的话,这将是很好的我的自定义类型出现在此菜单中。我看到Apple for iOS提供的那些是在插件DVTiOSPlistStructDefs.dvtplugin中编译的,它位于Contents / PlugIns中。

1 个答案:

答案 0 :(得分:0)

遗憾的是,你不能像你想象的那样做到这一点。您可以在plist中存储的唯一对象是NSDictionary,NSArray,NSString,NSNumber和NSData。但是你可以用这些类代表任何类型的对象/模型图表。

通常当我们加载一个plist文件时,我们会将它读入一个NSDictionary对象(有时候是一个NSArray)

NSDictionary *theDictionary = [[NSDictionary alloc]initWithContentsOfFile:@"aFilePath.plist"];

然后从那里我们可以拉出上面列出的那些类的对象。因此,您需要编写初始化方法,以便从方法或C函数中的一个创建数据对象或结构。

例如

//custom initializer
-(instanceType )initWithDictionary:(NSDictionary *)dictionary{

    if (self=[super init]){
    self.floatProperty = [[dictionary objectForKey:@"theFloatKey"]floatValue];
    self.stringProperty = [dictionary objectForKey:@"theStringKey"];
    //etc

    }

return self;
}



//or like this for some struct
myDataStruct structCreateFromDictionary(NSDictionary *dictionary) {

    myDataStruct result;

    result.floatThing = [[dictionary objectForKey:@"theFloatKey"]floatValue];
    result->stringPointer = [dictionary objectForKey:@"theStringKey"];

    //etc

return result;
}