【问题标题】:How to create a custom structure defintion for a plist for Xcode 6.x?如何为 Xcode 6.x 的 plist 创建自定义结构定义?
【发布时间】:2015-02-25 08:04:56
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c xcode plugins plist


    【解决方案1】:

    遗憾的是,您不能像您想象的那样简单地做到这一点。您可以存储在 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;
    }
    

    【讨论】:

    • 我知道如何从 plist 中读取数据,我指的是如何拥有自定义列表类型(结构),就像 Xcode 中提供的那样,您可以在其中拥有字典的默认键和值具有多个允许值的下拉列表。这种方式对于编辑 plist 的人来说会容易得多,只需选择适当的值。
    • @Sindrom 你找到解决方案了吗?因为我也有同样的问题,不明白自定义定义放在哪里..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2012-01-11
    • 1970-01-01
    相关资源
    最近更新 更多