【问题标题】:How would i change this static array to pull from this plist in xCode?我将如何更改此静态数组以从 xCode 中的此 plist 中提取?
【发布时间】:2013-03-10 13:40:29
【问题描述】:

我有一个在线列表(格式为http://example.com/people.plist)。如何让 UITable 视图从 plist 而不是静态数组中提取名称?

<plist version="1.0">
<array>
<dict>
    <key>fname</key>
    <string>Scott</string>
    <key>sname</key>
    <string>Sherwood</string>
    <key>age</key>
    <string>30</string>
</dict>
<dict>
    <key>fname</key>
    <string>Janet</string>
    <key>sname</key>
    <string>Smith</string>
    <key>age</key>
    <string>26</string>
</dict>
<dict>
    <key>fname</key>
    <string>John</string>
    <key>sname</key>
    <string>Blogs</string>
    <key>age</key>
    <string>20</string>
</dict>
</array>
</plist>

这是我的 viewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];


Person *p1 = [[Person alloc] initWithFname:@"Scott" sname:@"Sherwood"  age:30];
Person *p2 = [[Person alloc] initWithFname:@"Janet" sname:@"Smith"  age:26];
Person *p3 = [[Person alloc] initWithFname:@"John" sname:@"Blogs"  age:20];

self.people = [NSArray arrayWithObjects:p1,p2,p3, nil];
}

这是我的 tableView cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
Person *p1 = [self.people objectAtIndex:indexPath.row];

cell.textLabel.text = p1.fname;
return cell;
}

【问题讨论】:

  • 您想获取存储在远程服务器中的 plist 还是要将 plist 与应用程序捆绑在一起并显示其内容? BTW plist 的 url 不可访问
  • 谢谢。首先,正确的 URL 是格式,而不是我的 plist 的实际 URL。我添加了我的 plist,所以你可以看到。其次,我希望它是在线的,而不是打包的。 :)
  • SO中有一个类似的帖子。我想这对你来说会很好stackoverflow.com/a/10973246/767730
  • 嘿。感谢您的回复。不幸的是,这是关于将 plist 写入文件,我不需要这样做。本质上,我只是想从 plist 中提取该数组。谢谢! :)
  • 请注意,这与 Xcode完全无关

标签: ios objective-c arrays plist


【解决方案1】:

您可以为您的 Person 类创建一个自定义初始化程序,并几乎直接从 plist 填充数组:

@implementation Person

- (id)initWithDictionary:(NSDictionary *)dict
{
    NSString *fname = [dict objectForKey:@"fname"];
    NSString *sname = [dict objectForKey:@"sname"];
    NSString *age =   [dict objectForKey:@"age"  ];
    return self = [self initWithFname:fname sname:sname age:[age intValue]];
}

@end

然后做这样的事情:

NSString *path = [[NSBundle mainBundle] pathForResource:@"people" ofType:@"plist"];
NSArray *plist = [NSArray arrayWithContentsOfFile:path];

NSMutableArray *people = [NSMutableArray array];
for (NSDictionary *item in plist) {
    Person *p = [[Person alloc] initWithDictionary:item];
    [people addObject:p];
    [p release];
}

然后只需使用people 作为数据源。

一个边际概念改进:不是将年龄存储为&lt;string&gt;,而是将其存储为&lt;integer&gt;。在这种情况下,您将拥有NSNumber 对象(您也可以在第一步中调用intValue 方法)。

【讨论】:

  • 哇。这看起来正是我正在寻找的。真正有帮助的是你写出了方法,而不是仅仅说“做一个[方法]”。我要澄清两点:无论如何,我要摆脱年龄,因为这是我正在定制的教程,用于为本地流行语构建自定义单词词典应用程序,所以没关系。其次,我可以用“withContentsOfURL”替换“withContentsOfFile”吗?
  • @GeoffHill 想一想,试着回答自己。你可以吗?如果是这样,为什么?如果没有,为什么不呢?
  • 糟糕。不,我不能。正如“路径”和“捆绑”所暗示的那样,它在包中,对吗?那么我怎样才能从在线资源中获取信息呢??
  • @GeoffHill 错误。你可以。为什么你不能?
  • 好的,所以我可以将NSArray *plist = [NSArray arrayWithContentsOfFile:path]; 更改为NSArray *plist = [NSArray arrayWithContentsOfURL:@"http://url.com/list.plist"]; 吗?
猜你喜欢
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多