如何在.plist文件的数组中编写一个字符串

时间:2016-04-12 17:21:34

标签: objective-c ios9 objective-c++ info-plist

我是新来的。希望有人可以帮助我!

我正在创建一个调整,其中我添加了AppList(Ryan Petrich)并且正在工作,所以我在我的偏好中捆绑了一个包含所有应用程序的链接单元以及每个应用程序的开/关开关。

当我将其中一些切换为ON时,它会在首选项.plist中保存<key>ALvalue-com.some.app</key>和值<true/>

我正在试图弄清楚如何将“key”“boolValue”的结构更改为:

AL-hd.plist文件

<dict>
    <key>AL-hd</key>
    <array>
        <string>com.apple.AppStore</string>
        <string>com.apple.gamecenter</string>
        <string>com.apple.stocks</string>
    </array>
</dict>
</plist>

而不是这个

<dict>
    <key>ALvalue-com.apple.AppStore</key>
    <true/>
    <key>ALvalue-com.apple.gamecenter</key>
    <true/>
    <key>ALvalue-com.apple.stocks</key>
    <true/>
</dict>
</plist>

这是我的AppList .plist,它保存了首选项文件AL-hd.plist:

    <dict>
        <key>cell</key>
        <string>PSLinkCell</string>
        <key>bundle</key>
        <string>AppList</string>
        <key>isController</key>
        <string>1</string>
        <key>label</key>
        <string>Show apps</string>
        <key>icon</key>
        <string>Favorites.png</string>
        <key>ALAllowsSelection</key>
        <string>1</string>
        <key>ALChangeNotification</key>
        <string>com.your.companyChanged</string>
        <key>ALSectionDescriptors</key>
        <array>
            <dict>
                <key>footer-title</key>
                <string>Check what apps you want to show up</string>
                <key>items</key>
                <array/>
            </dict>
            <dict>
                <key>cell-class-name</key>
                <string>ALCheckCell</string>
                <key>icon-size</key>
                <string>29</string>
                <key>suppress-hidden-apps</key>
                <string>1</string>
                <key>title</key>
                <string>All Applications</string>
            </dict>
        </array>
        <key>ALSettingsKeyPrefix</key>
        <string>ALvalue-</string>
        <key>ALSettingsPath</key>
        <string>/var/mobile/Library/Preferences/AL-hd.plist</string>
    </dict>

我经常搜索并尝试了数百件事。

谢谢!

我尝试的一件事是创建第二个.plist并稍后尝试复制该键并将其粘贴为第二个文件中的字符串。并在.mm文件中使用此操作在首选项包中添加一个按钮(没有运气):

- (void)save
{
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];

    // Either this or whatever works from link after this
    NSString *prefPath = @"/User/Library/Preferences/com.your.company.plist";
    NSString *showPath = @"/User/Library/Preferences/AL-hd.plist";
    NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:prefPath];    
    NSMutableArray *show = [[NSMutableArray alloc] initWithContentsOfFile:showPath];    
    if ([[plistDict objectForKey:bundleID] boolValue]) {
          [Show addObject:bundleID];
    }
}

如果我使用首选项包.plist或使用第二个,然后以我需要的方式复制这些行,对我来说是不相关的。

1 个答案:

答案 0 :(得分:0)

喜欢这个。

  1. 创建一个新的.plist文件并打开为属性列表。

  2. 点击+在信息属性列表中创建新属性。

  3. 为您的属性命名,例如 - “Text”将类型设置为“Array”。

  4. 在上面创建的数组中添加新项目,并为每个项目输入您的文字。

  5. 现在在你的视图控制器类中,你必须这样做。

        //ViewController.h
    
        @property (nonatomic,strong)NSArray *plistDataArr;
    
       //ViewController.m
    
    
     -(NSArray*) plistDataArr
      {
        if(! _plistDataArr)
        {
          NSString *filePath = [[NSBundle mainBundle]pathForResource:@"YOUR Plist file name" ofType:@"plist"];
         _plistDataArr = [NSArray arrayWithContentsOfFile:filePath];
        }
      return _plistDataArr;
    
       }
    
      //viewDidLoad
    
      NSMutableArray *tempArr=[NSMutableArray arrayWithArray:self. _plistDataArr];