crashing iPhone program when used to read a dictionary file from plist file
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?!
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.
精彩评论