0

所以这是我的代码。我正在尝试将 plist 中的内容显示到表格中,但我一直遇到错误。我整天都在做,所以我可能错过了一些相当容易的东西。希望你们能弄清楚为什么当我建造时,桌子是空的。

这是我的 plist(没有标题):

<array>
<dict>
    <key>word</key>
    <string>AWord1</string>
    <key>definition</key>
    <string>here is a definition</string>
    <key>partOfSpeech</key>
    <string>here is a part of speech</string>
</dict>
<dict>
    <key>word</key>
    <string>Bword1</string>
    <key>definition</key>
    <string>here is a definition</string>
    <key>partOfSpeech</key>
    <string>here is a part of speech</string>
</dict>
</array>
</plist>

编辑:由于格式错误而缺少结束标签 - 已修复

好的。这是我的新代码。我 NSLog'd 并决定在我去 plist 之前备份。所以,如果你能帮忙的话。你会如何建议我从 plist 中提取这些数据(假设数据在 plist 中)

//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];

NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Italy", @"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", @"Test", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

[listOfItems addObject:countriesToLiveInDict];
[listOfItems addObject:countriesLivedInDict];

//Initialize the copy array.
copyListOfItems = [[NSMutableArray alloc] init];

//Set the title
self.navigationItem.title = @"Countries";

//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

searching = NO;
letUserSelectRow = YES;

我觉得好像我仍然没有真正告诉它解析来自 plist 的数据。我只是初始化它。想法?

4

2 回答 2

1

您的代码似乎很不完整,它与 plist 没有太大关系。这里有几个问题

  • 正如 H2CO3 和 Fogmeister 所指出的,您显示的 plist 缺少结束</array>标记(但假设这是问题中的错字)
  • 您将整个数组加载到字典中的单个条目中countriesToLiveIn(针对键“定义”)
  • countriesToLiveIn成为数组中的单个对象listOfItems
  • 你初始化了一个copyOfListItems数组,然后不做任何事情

然后你不指出表格从哪里得到它的日期。

如果它来自copyOfListItems,那么该数组是空的 .: 空表。

如果它来自listOfItems您将以countriesToLiveIn某种方式解析字典,但您没有显示如何。

如果它直接来自 plist 数组countriesToLiveInArray,那么缺少</array>的标签可能是一个线索。您还需要以某种方式解析数组包含的字典,并且您没有向我们展示该代码,那里可能有问题。

为了进一步了解这一点,我建议大量使用NSLog来了解数据进入对象的程度。如果您仍然遇到困难,也许您可​​以用结果扩展您的问题。

更新

根据您更新的代码,我的第一个怀疑是您的 URL 存在问题。

在您之前的编辑中,这是可疑行

NSMutableArray *wordListArray = [[NSMutableArray alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://url.com/dictpPlist.plist"]];

您需要在它之后立即进行 NSLog:

NSLog (@"wordListArray %@", wordListArray);

确认您没有在此处选择 plist。至少我们在您的代码中将问题隔离到这一点。

然后我们需要质疑 URL - 它是否正确?plist 是如何到达那里的?你能独立验证吗?它是本地文件 URL 还是网络 URL?

如果您正在访问网络资源,这很可能是您的问题。这些initWithContentsOfURL方法应该只与本地文件系统 URL 一起使用。参见例如

initwithcontentsofurl 方法被认为是有害的。

您应该NSURLConnection改为使用异步。苹果对此也有点谨慎

aURLwriteToURL:atomically: :包含由该方法生成的数组的字符串表示形式的文件的位置。

假设这是你的问题,你为什么不先让它与本地文件系统 plist 一起工作。

listOfItems从你的数组中写一个代码:

- (void) createPlist
{
    NSFileManager* fManager = [NSFileManager defaultManager];
    NSError* error;
    NSURL* docsURL = [fManager URLForDirectory:NSDocumentDirectory 
                                          inDomain:NSUserDomainMask 
                                 appropriateForURL:nil 
                                            create:NO 
                                             error:&error];

    NSURL* plistURL = [docsURL URLByAppendingPathComponent:@"testlist.plist"];
    [listOfItems writeToURL:plistURL atomically:YES];
}

重新读一遍,验证它是否有效,然后找出如何通过网络执行相同的操作。

于 2013-02-28T13:42:32.047 回答
-2

试试下面的代码

// Path to the plist (in the application bundle)
     NSString *path = [[NSBundle mainBundle] pathForResource:
    @"dictpPlist" ofType:@"plist"];

// Build the array from the plist  
     NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];

// Show the string values  
    for (NSString *str in array2)
      NSLog(@"--%@", str);
于 2013-02-28T13:27:51.827 回答