当用于从plist文件读取字典文件时崩溃iPhone程序
我正在学习使用Beginning iPhone 4编程为iPhone编程。今天,我决定创建一个我自己的程序,用一个数字字典和他们的法语版本阅读。应用程序应该在表格视图中显示数字,并且点击时应在alertview中显示法语版本。我还没有实现法语翻译的显示,因为我无法使表格视图正常工作。它显示数字,然后当我尝试向上或向下滚动时,它会崩溃。我不知道什么是错的。这里是plist文件:当用于从plist文件读取字典文件时崩溃iPhone程序
<?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文件到项目的资源文件夹中。我有一个创建tableview的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
。你已经用一个返回一个自动释放对象的便捷方法创建它。而当你(应该)知道它是只允许释放你自己的对象(即对象发送一个alloc
,retain
,new
或copy
消息)
也许你应该重新读取Memory Management Programming Guide。了解内存管理至关重要。
太棒了!我应该意识到这一点。感谢您的快速回答。该计划现在起作用。 – user709903 2011-04-15 14:34:32
事故发生后控制台上有什么? – 2011-04-15 13:59:58
控制台显示会话开始,没有其他东西了! – user709903 2011-04-15 14:13:51