【问题标题】:How to write a string inside an array of .plist file如何在 .plist 文件数组中写入字符串
【发布时间】:2016-08-03 11:11:59
【问题描述】:

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

我正在创建一个调整,其中添加了 AppList (Ryan Petrich) 并使其正常工作,因此我偏好将链接单元与所有应用程序捆绑在一起,并为每个应用程序打开/关闭。

当我打开其中的一些时,它会在首选项 .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 或使用第二个,然后以我需要的方式复制这些行,对我来说无关紧要。

【问题讨论】:

  • 不清楚您的问题是什么。您至少需要展示您的代码并解释它与您的要求有何不同,然后也许有人可以帮助您。自己编辑问题,不要尝试在 cmets 中添加材料。
  • 你想把 2 个 plist 文件合并为一个吗?
  • 不,我已经编辑了答案以更清楚......对不起,我的英语!

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


【解决方案1】:

像这样。

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

  2. 点击+创建一个新的属性内信息属性列表。

  3. 为您的属性命名,例如-“文本”将类型设置为“数组”。

  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];
    

【讨论】:

  • 非常感谢!但我没有 .m 和 .h 文件。只有.mm。我还不知道如何将这些文件更改为 .mm。这发生在我身上,我创建的所有例子都和你一样......
  • 请点赞我的回答,让其他人也能从中受益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 2016-09-07
  • 2016-05-08
  • 1970-01-01
  • 2019-05-01
  • 2017-09-19
  • 1970-01-01
相关资源
最近更新 更多