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

|| 我正在学习使用Beginning iPhone 4编程为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中,则代码可以正常工作,并且我能够选择该行并显示alertview。我的猜测是我的字典或字典加载方法出了点问题。我无法弄清楚是什么。你能帮忙吗?     
已邀请:
NSArray *keysInDictionary = [self.numberDictionary allKeys];
self.numerals = keysInDictionary;
[keysInDictionary release];  // <- remove this line
请勿释放
keysInDictionary
。您已使用返回自动释放对象的便捷方法创建了它。正如您(应该)知道的那样,只允许释放您拥有的对象(即,您发送“ 6”,“ 7”,“ 8”或“ 9”消息的对象) 也许您应该再次阅读《内存管理编程指南》。了解内存管理至关重要。     

要回复问题请先登录注册