xcode 无法从 IOS 中的 plist 文件读取字典数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13359344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant read dictionary data from plist file in IOS
提问by Tahlil
I have PropertyList.plist file in the "Supporting Files" folder. I have made a dictionary in it. The plist file is:
我在“支持文件”文件夹中有 PropertyList.plist 文件。我在里面做了一本字典。plist 文件是:
MY ViewController.m file code is
我的 ViewController.m 文件代码是
@implementation GroupedInexedViewController
{
NSDictionary *names;
NSArray *keys;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]
initWithContentsOfFile:path];
names = dict;
[dict release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:
@selector(compare:)];
keys = array;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
return key;
}
Unfortunately the "keys" array doesnt contain any element. Because I made an alert with its count value which is "keys.count" and it was zero. and also the "names" count was also zero. The path variable on vieDidLoad method shows the correct path. but it cant read from the dictionary of the plist file's.
不幸的是,“keys”数组不包含任何元素。因为我用它的计数值“keys.count”发出了一个警报,它是零。而且“名称”计数也为零。vieDidLoad 方法上的路径变量显示了正确的路径。但它无法从 plist 文件的字典中读取。
Edit: I used nslog and it shows that in "viewDidLoad" method "names" is able to load the dictionary . but "keys" array is unable to load it.
编辑:我使用了 nslog,它显示在“viewDidLoad”方法中“names”能够加载字典。但是“keys”数组无法加载它。
回答by Tahlil
What @EugeneK said worked but got sigabrt in "cellForRowAtIndexPath" method on the line
@EugeneK 所说的有效但在线上的“cellForRowAtIndexPath”方法中得到了 sigabrt
cell.textLabel.text = [nameSection objectAtIndex:row];
cell.textLabel.text = [nameSection objectAtIndex:row];
the error message is
错误信息是
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6832440".
“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x6832440”。
The problem was nameSection variable was appearing as a NSDictionary type object which doesn't support objectAtIndex method.
问题是 nameSection 变量显示为不支持 objectAtIndex 方法的 NSDictionary 类型对象。
So what I did I know is not a very good way to do it. I am sure there is some other way. I changed the 'viewDidLoad' method to this,
所以我所做的我知道不是一个很好的方法。我确定还有其他方法。我将“viewDidLoad”方法更改为此,
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
names = [[NSDictionary alloc]
initWithContentsOfFile:path];
keys = [names allKeys];
NSString *key = [keys objectAtIndex:0];
names = [names objectForKey:key];
keys = [[[names allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];
NSLog(@"keys = %@ names = %@",keys,names);
}
It works! Any idea how to do it better will be appreciated though.
有用!任何想法如何做得更好,但将不胜感激。
回答by EugeneK
Martin Ris right. Please see comments in your code:
马丁 R是对的。请查看代码中的注释:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]
initWithContentsOfFile:path]; //retainCount of dict is 1
names = dict; // you made weak reference to dict
[dict release]; // retainCount is 0 - dict is being dealloced
NSArray *array = [[names allKeys] sortedArrayUsingSelector:
@selector(compare:)]; // you try to get data from dealloced object
keys = array;
}
Try to do the following:
尝试执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
ofType:@"plist"];
names = [[NSDictionary alloc]
initWithContentsOfFile:path];
keys = [[[names allKeys] sortedArrayUsingSelector:
@selector(compare:)] retain];
}
And do not forgot to release names
and keys
in your view controller dealloc
.
并且不要忘记在您的视图控制器中释放names
和。keys
dealloc