从 plist 文件读取字典文件时 iPhone 程序崩溃

发布于 2024-11-01 09:15:05 字数 7188 浏览 3 评论 0原文

我正在使用 Beginning iPhone 4programming 学习 iPhone 编程。今天,我决定创建一个自己的程序,它将读取数字词典及其法语版本。该应用程序应该在表格视图中显示数字,点击时应该在警报视图中显示法语版本。我还没有实现法语翻译的显示,因为我在使表格视图正常工作时遇到困难。它显示数字,然后当我尝试向上或向下滚动时,它崩溃了。我不确定出了什么问题。这是 plist 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-    1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<string>un</string>
<key>2</key>
<string>deux</string>
<key>3</key>
<string>trois</string>
<key>4</key>
<string>quatre</string>
<key>5</key>
<string>cinq</string>
<key>6</key>
<string>six</string>
<key>7</key>
<string>sept</string>
<key>8</key>
<string>huit</string>
<key>9</key>
<string>neuf</string>
<key>10</key>
<string>dix</string>
<key>11</key>
<string>onze</string>
<key>12</key>
<string>dooz</string>
<key>13</key>
<string>treize</string>
<key>14</key>
<string>quatorze</string>
<key>15</key>
<string>quinze</string>
<key>16</key>
<string>seize</string>
<key>17</key>
<string>dix-sept</string>
<key>18</key>
<string>dix-huit</string>
<key>19</key>
<string>dix-neuf</string>
<key>20</key>
<string>vingt</string>
<key>21</key>
<string>vingt et un</string>
<key>22</key>
<string>vingt-deux</string>
<key>23</key>
<string>vingt trois</string>
<key>24</key>
<string>vingt-quatre</string>
<key>25</key>
<string>vingt-cinq</string>
<key>30</key>
<string>trent</string>
<key>32</key>
<string>trent-deux</string>
<key>40</key>
<string>quarante</string>
<key>43</key>
<string>quarante trois</string>
<key>50</key>
<string>cinquante</string>
<key>54</key>
<string>cinquante quatre</string>
<key>60</key>
<string>soixante</string>
<key>65</key>
<string>soixante-cinq</string>
<key>67</key>
<string>soixante-sept</string>
<key>70</key>
<string>soixante-dix</string>
<key>71</key>
<string>soixante et onze</string>
<key>72</key>
<string>soixante-douze</string>
<key>73</key>
<string>soixante-treize</string>
<key>74</key>
<string>soixante-quatorze</string>
<key>75</key>
<string>soixante-quinze</string>
<key>76</key>
<string>soixante-seize</string>
<key>77</key>
<string>soixante-dix-sept</string>
<key>78</key>
<string>soixante-dix-huit</string>
<key>79</key>
<string>soixante-dix-neuf</string>
<key>80</key>
<string>quatre-vingts</string>
<key>81</key>
<string>quatre-vingt-et-un</string>
<key>82</key>
<string>quatre-vingt-deux</string>
<key>83</key>
<string>quatre-vingt-trois</string>
<key>84</key>
<string>quatre-vingt-quatre</string>
<key>85</key>
<string>quatre-vingt-cinq</string>
<key>86</key>
<string>quatre-vingt-six</string>
<key>87</key>
<string>quatre-vingt-sept</string>
<key>88</key>
<string>quatre-vingt-huit</string>
<key>89</key>
<string>quatre-vingt-neuf</string>
<key>90</key>
<string>quatre-vingt-dix</string>
<key>91</key>
<string>quatre-vingt-onze</string>
<key>92</key>
<string>quatre-vingt-douze</string>
<key>93</key>
<string>quatre-vingt-treize</string>
<key>94</key>
<string>quatre-vinigt-quatorze</string>
<key>95</key>
<string>quatre-vingt-quinze</string>
<key>96</key>
<string>quatre-vingt-seize</string>
<key>97</key>
<string>quatre-vingt-dix-sept</string>
<key>98</key>
<string>quatre-viingt-dix-huit</string>
<key>99</key>
<string>quatre-vingt-dix-neuf</string>
<key>100</key>
<string>cent</string>
<key>200</key>
<string>deux-cent</string>
<key>300</key>
<string>trois-cent</string>
<key>900</key>
<string>neuf-cent</string>

我已将 plist 文件加载到项目的资源文件夹中。我有一个 NIB 文件,我在其中创建表视图。然后在下面的代码中,我读取了 plist 文件的内容:

   - (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"frenchNumbers" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
self.numberDictionary = dictionary;
[dictionary release];

NSArray *keysInDictionary = [self.numberDictionary allKeys];
self.numerals = keysInDictionary;
[keysInDictionary release];
    [super viewDidLoad];

这是 TableView 数据源方法的代码:

-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section{
return [self.numerals count];
}

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *FrenchNumbersIdentifier = @"FrenchNumbersIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FrenchNumbersIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:FrenchNumbersIdentifier]autorelease];
}

NSUInteger row = [indexPath row];
cell.textLabel.text = [numerals objectAtIndex:row];
NSLog(@"Value to be printed: %@",[numerals objectAtIndex:row]);
return cell;

}

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [numerals objectAtIndex:row];

NSString *message = [[NSString alloc] initWithFormat:@"You selected: %@",rowValue];

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Row Selected"
                                                message:message delegate:nil
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles:nil];
[alert show];

[message release];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

如果我用简单的硬编码数组替换 plist 文件并将其加载到 tableview 中,则代码可以正常工作我可以选择该行并显示警报视图。我的猜测是我的字典或字典加载方法有问题。我不知道是什么。你能帮忙吗?

I am learning to program for the iPhone using the Beginning iPhone 4 programming. Today, I decided to create a program of my own which will read in a dictionary of numbers and their French versions. The app is supposed to show the numbers in a table view and when tapped should show the French version in an alertview. I have not implemented the displaying of the french translation yet since I am having trouble in getting the table view to work. It shows the numbers and then when I try to scroll up or down, it crashes. I am not sure what is wrong. Here is the plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-    1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<string>un</string>
<key>2</key>
<string>deux</string>
<key>3</key>
<string>trois</string>
<key>4</key>
<string>quatre</string>
<key>5</key>
<string>cinq</string>
<key>6</key>
<string>six</string>
<key>7</key>
<string>sept</string>
<key>8</key>
<string>huit</string>
<key>9</key>
<string>neuf</string>
<key>10</key>
<string>dix</string>
<key>11</key>
<string>onze</string>
<key>12</key>
<string>dooz</string>
<key>13</key>
<string>treize</string>
<key>14</key>
<string>quatorze</string>
<key>15</key>
<string>quinze</string>
<key>16</key>
<string>seize</string>
<key>17</key>
<string>dix-sept</string>
<key>18</key>
<string>dix-huit</string>
<key>19</key>
<string>dix-neuf</string>
<key>20</key>
<string>vingt</string>
<key>21</key>
<string>vingt et un</string>
<key>22</key>
<string>vingt-deux</string>
<key>23</key>
<string>vingt trois</string>
<key>24</key>
<string>vingt-quatre</string>
<key>25</key>
<string>vingt-cinq</string>
<key>30</key>
<string>trent</string>
<key>32</key>
<string>trent-deux</string>
<key>40</key>
<string>quarante</string>
<key>43</key>
<string>quarante trois</string>
<key>50</key>
<string>cinquante</string>
<key>54</key>
<string>cinquante quatre</string>
<key>60</key>
<string>soixante</string>
<key>65</key>
<string>soixante-cinq</string>
<key>67</key>
<string>soixante-sept</string>
<key>70</key>
<string>soixante-dix</string>
<key>71</key>
<string>soixante et onze</string>
<key>72</key>
<string>soixante-douze</string>
<key>73</key>
<string>soixante-treize</string>
<key>74</key>
<string>soixante-quatorze</string>
<key>75</key>
<string>soixante-quinze</string>
<key>76</key>
<string>soixante-seize</string>
<key>77</key>
<string>soixante-dix-sept</string>
<key>78</key>
<string>soixante-dix-huit</string>
<key>79</key>
<string>soixante-dix-neuf</string>
<key>80</key>
<string>quatre-vingts</string>
<key>81</key>
<string>quatre-vingt-et-un</string>
<key>82</key>
<string>quatre-vingt-deux</string>
<key>83</key>
<string>quatre-vingt-trois</string>
<key>84</key>
<string>quatre-vingt-quatre</string>
<key>85</key>
<string>quatre-vingt-cinq</string>
<key>86</key>
<string>quatre-vingt-six</string>
<key>87</key>
<string>quatre-vingt-sept</string>
<key>88</key>
<string>quatre-vingt-huit</string>
<key>89</key>
<string>quatre-vingt-neuf</string>
<key>90</key>
<string>quatre-vingt-dix</string>
<key>91</key>
<string>quatre-vingt-onze</string>
<key>92</key>
<string>quatre-vingt-douze</string>
<key>93</key>
<string>quatre-vingt-treize</string>
<key>94</key>
<string>quatre-vinigt-quatorze</string>
<key>95</key>
<string>quatre-vingt-quinze</string>
<key>96</key>
<string>quatre-vingt-seize</string>
<key>97</key>
<string>quatre-vingt-dix-sept</string>
<key>98</key>
<string>quatre-viingt-dix-huit</string>
<key>99</key>
<string>quatre-vingt-dix-neuf</string>
<key>100</key>
<string>cent</string>
<key>200</key>
<string>deux-cent</string>
<key>300</key>
<string>trois-cent</string>
<key>900</key>
<string>neuf-cent</string>

I have loaded the plist file into the resources folder of the project. I have a NIB file where I create the tableview. Then in the code below, I read the contents of the plist file:

   - (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"frenchNumbers" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
self.numberDictionary = dictionary;
[dictionary release];

NSArray *keysInDictionary = [self.numberDictionary allKeys];
self.numerals = keysInDictionary;
[keysInDictionary release];
    [super viewDidLoad];

Here is the code for TableView Data Source methods:

-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section{
return [self.numerals count];
}

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *FrenchNumbersIdentifier = @"FrenchNumbersIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FrenchNumbersIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:FrenchNumbersIdentifier]autorelease];
}

NSUInteger row = [indexPath row];
cell.textLabel.text = [numerals objectAtIndex:row];
NSLog(@"Value to be printed: %@",[numerals objectAtIndex:row]);
return cell;

}

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [numerals objectAtIndex:row];

NSString *message = [[NSString alloc] initWithFormat:@"You selected: %@",rowValue];

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Row Selected"
                                                message:message delegate:nil
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles:nil];
[alert show];

[message release];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

If I replace my plist file with a simple hard coded array and load that into the tableview, the code works just fine and I am able to select the row and display the alertview. My guess is that something is wrong with my dictionary or my dictionary load methods. I am not able to figure out what. Can you please help?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2024-11-08 09:15:05
NSArray *keysInDictionary = [self.numberDictionary allKeys];
self.numerals = keysInDictionary;
[keysInDictionary release];  // <- remove this line

不要释放keysInDictionary。您已使用返回自动释放对象的便捷方法创建了它。正如您(应该)知道的那样,它只允许释放您拥有的对象(即您发送 allocretainnew复制消息)

也许你应该阅读内存管理编程再次引导。了解内存管理至关重要。

NSArray *keysInDictionary = [self.numberDictionary allKeys];
self.numerals = keysInDictionary;
[keysInDictionary release];  // <- remove this line

Do not release keysInDictionary. You have created it with a convenience method that returns an autoreleased object. And as you (should) know it is only allowed to release objects that you own (ie objects you send a alloc, retain, new or copy message)

Maybe you should read the Memory Management Programming Guide again. It's crucial to understand memory management.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文