访问plist iphone中数组中的字符串
我有一个 plist,其中包含类型字典的根,其中包含数组类型的键,每个数组都有三个字符串类型的项目。
我想访问字符串并将它们传递给视图控制器。
此行在我的 cellForRowAtIndexPath
NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0] 中成功);
当我将同一行放入 didSelectRowAtIndexPath 方法中时,出现 BAD_ACCESS 错误。
任何有关如何获取字符串的建议都会有所帮助。
这是我的 .plist
<dict>
<key>Derivative</key>
<array>
<string>Derivative 1</string>
<string>front.png</string>
<string>back.png</string>
</array>
<key>Rolle's Theorem</key>
<array>
<string>Rolle's Theorem 1</string>
<string>front.png</string>
<string>3bk.png</string>
</array>
<key>Chain Rule</key>
<array>
<string>Chain Rule</string>
<string>chainrule1.png</string>
<string>chainrule1_bk.png</string>
</array>
</dict>
</plist>
这是两种方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.backgroundColor = [UIColor purpleColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = [self.keys objectAtIndex:row];
// Key output to NSLOG accessing elements from the plist
NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);
cell.textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:18.0];
cell.textLabel.numberOfLines = 2;
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCell_BG.png"]] autorelease];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSLog(@"Row selected was: %i", row);
// Key output to NSLOG accessing elements from the plist
//NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);
/*
NSString *selectedCard = [[aDictionary objectForKey:(@"%@",[keys objectAtIndex:row])] objectAtIndex:0];
NSLog(@"Showing Flash Card: %@", selectedCard);
*/
FlashCardViewController *flashVC = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:nil];
id aKey = [keys objectAtIndex:row];
flashVC.aSelectedCard = [[aDictionary objectForKey:aKey] objectAtIndex:0];
[self.navigationController pushViewController:flashVC animated:YES];
[flashVC release];
}
I have a plist with root of type dictionary containing keys of type array, and each array has three items of type string.
I want to access the strings and pass them to a view controller.
This line is successful in my cellForRowAtIndexPath
NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);
When I put the same line in my didSelectRowAtIndexPath method I get a BAD_ACCESS error.
Any suggestions as to how to get at the strings would be helpful.
Here is my .plist
<dict>
<key>Derivative</key>
<array>
<string>Derivative 1</string>
<string>front.png</string>
<string>back.png</string>
</array>
<key>Rolle's Theorem</key>
<array>
<string>Rolle's Theorem 1</string>
<string>front.png</string>
<string>3bk.png</string>
</array>
<key>Chain Rule</key>
<array>
<string>Chain Rule</string>
<string>chainrule1.png</string>
<string>chainrule1_bk.png</string>
</array>
</dict>
</plist>
And here are the two methods:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.backgroundColor = [UIColor purpleColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = [self.keys objectAtIndex:row];
// Key output to NSLOG accessing elements from the plist
NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);
cell.textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:18.0];
cell.textLabel.numberOfLines = 2;
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCell_BG.png"]] autorelease];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSLog(@"Row selected was: %i", row);
// Key output to NSLOG accessing elements from the plist
//NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);
/*
NSString *selectedCard = [[aDictionary objectForKey:(@"%@",[keys objectAtIndex:row])] objectAtIndex:0];
NSLog(@"Showing Flash Card: %@", selectedCard);
*/
FlashCardViewController *flashVC = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:nil];
id aKey = [keys objectAtIndex:row];
flashVC.aSelectedCard = [[aDictionary objectForKey:aKey] objectAtIndex:0];
[self.navigationController pushViewController:flashVC animated:YES];
[flashVC release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,您没有保留
aDictionary
或keys
,或者代码中此时出现了另一个内存问题。但如果没有其余的代码,很难判断。My guess is that you are not retaining
aDictionary
orkeys
, or you have another memory problem that is manifesting at that point in the code. But it's difficult to tell without the rest of the code.将此行更改为
flashVC.aSelectedCard = [[aDictionary objectForKey:[keys objectAtIndex:row]] objectAtIndex:0];
程序在哪里崩溃,哪一行?
Change this line to
flashVC.aSelectedCard = [[aDictionary objectForKey:[keys objectAtIndex:row]] objectAtIndex:0];
Where does the program crash, what line?