简体   繁体   English

从.plist(-[NSCFString objectAtIndex:])填充时TableView崩溃

[英]TableView Crashes when populates from .plist (-[NSCFString objectAtIndex:])

Hey guys, my app crashes upon load and it throws up this when debugging: 大家好,我的应用在加载时崩溃,在调试时会抛出此错误:

2011-02-26 00:24:33.254 LCPapers[5182:207] Set up the cell...
2011-02-26 00:24:33.255 LCPapers[5182:207] 0
2011-02-26 00:24:33.257 LCPapers[5182:207] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x4b175f0
2011-02-26 00:24:33.261 LCPapers[5182:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x4b175f0'

It'd be really great if someone could tell me what to fix, as I'm fairly new and the hours spent frustrated over this have been horrible! 如果有人告诉我要解决的问题,那真是太好了,因为我还很新,而且为此而沮丧的时间真是太恐怖了!

RootViewController.h RootViewController.h

#import <UIKit/UIKit.h>
@class OverlayViewController;
@interface RootViewController : UITableViewController {

    NSArray *tableDataSource;
    NSString *CurrentTitle;
    NSInteger CurrentLevel;
    IBOutlet UITableView *tableView;
    NSDictionary *data;
    BOOL letUserSelectRow;

    OverlayViewController *ovController;

}
@property (nonatomic, retain) NSArray *tableDataSource;
@property (nonatomic, retain) NSString *CurrentTitle;
@property (nonatomic, readwrite) NSInteger CurrentLevel;
@property (nonatomic, retain) NSDictionary *data;

@end

and my RootViewController.m 我的RootViewController.m

#import "RootViewController.h"
#import "DetailViewController.h"
#import "LCPapersAppDelegate.h"

@implementation RootViewController
@synthesize tableDataSource, CurrentTitle, CurrentLevel;
@synthesize data;
#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
    // Override point for customization after application launch.
    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *DataPath = [Path stringByAppendingPathComponent:@"data.plist"];

    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
    self.data = tempDict;
    [tempDict release];

    if(CurrentLevel == 0) {
        NSLog(@"Initialize our table data source");
        //Initialize our table data source
        self.navigationItem.title = @"Leaving Cert. Papers";

        NSArray *tempArray = [[NSArray alloc] init];
        self.tableDataSource = tempArray;
        [tempArray release];


        self.tableDataSource = [data objectForKey:@"Rows"];

    }
    else 
        NSLog(@"self.navigationItem.title = CurrentTitle;");

    self.navigationItem.title = CurrentTitle;   

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Customize the number of rows in the table view.");
    return [self.tableDataSource count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Customize the appearance of table view cells.");
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    // Set up the cell...
    NSLog(@"Set up the cell...");
    NSLog(@"%d", indexPath.row);
    NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    NSArray *array = [dictionary objectForKey:@"Title"];
    cell.text = [array objectAtIndex:indexPath.row];

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Get the selected Subject

    NSString *selectedSubject = nil;

        NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
        NSArray *array = [dictionary objectForKey:@"Title"];
        selectedSubject = [array objectAtIndex:indexPath.row];


    //Initialize the detail view controller and display it.
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    dvController.selectedSubject = selectedSubject;
    [self.navigationController pushViewController:dvController animated:YES];

    [dvController release];
    dvController = nil;
}

- (NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(letUserSelectRow)
        return indexPath;
    else
        return nil;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

    //return UITableViewCellAccessoryDetailDisclosureButton;
    return UITableViewCellAccessoryDisclosureIndicator;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

    [self tableView:tableView didSelectRowAtIndexPath:indexPath];
}




- (void)dealloc {
    [data release];
    [ovController release];

    [super dealloc];
}


@end

At Luca Bernardi's request, heres the plist: 应卢卡·贝纳迪(Luca Bernardi)的要求,以下为清单:

<?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>Rows</key>
    <array>
        <dict>
            <key>Title</key>
            <string>English</string>
            <key>Children</key>
            <array>
                <dict>
                    <key>Title</key>
                    <string>Paper 1</string>
                    <key>Children</key>
                    <array>
                        <dict>
                            <key>Title</key>
                            <string>2010</string>
                        </dict>
                        <dict>
                            <key>Title</key>
                            <string>2009</string>
                        </dict>
                    </array>
                </dict>
                <dict>
                    <key>Title</key>
                    <string>Paper 2</string>
                    <key>Children</key>
                    <array>
                        <dict>
                            <key>Title</key>
                            <string>2010</string>
                        </dict>
                        <dict>
                            <key>Title</key>
                            <string>2009</string>
                        </dict>
                    </array>
                </dict>
            </array>
        </dict>
        <dict>
            <key>Title</key>
            <string>Maths</string>
            <key>Children</key>
            <array>
                <dict>
                    <key>Title</key>
                    <string>Higher</string>
                    <key>Children</key>
                    <array>
                        <dict>
                            <key>Title</key>
                            <string>Paper 1</string>
                            <key>Children</key>
                            <array>
                                <dict>
                                    <key>Title</key>
                                    <string>2010</string>
                                    <key>Url</key>
                                    <string>http://x.ie/LC003ALP100EV.pdf</string>
                                </dict>
                                <dict>
                                    <key>Title</key>
                                    <string>2009</string>
                                    <key>Url</key>
                                    <string>http://x.ie/LC003ALP100EV.pdf</string>
                                </dict>
                            </array>
                        </dict>
                        <dict>
                            <key>Title</key>
                            <string>Paper 2</string>
                            <key>Children</key>
                            <array>
                                <dict>
                                    <key>Title</key>
                                    <string>2010</string>
                                    <key>Url</key>
                                    <string>http://examinations.ie/archive/exampapers/2010/LC003ALP200EV.pdf</string>
                                </dict>
                                <dict>
                                    <key>Title</key>
                                    <string>2009</string>
                                    <key>Url</key>
                                    <string>http://examinations.ie/archive/exampapers/2009/LC003ALP200EV.pdf</string>
                                </dict>
                            </array>
                        </dict>
                    </array>
                </dict>
                <dict>
                    <key>Title</key>
                    <string>Ordinary</string>
                    <key>Children</key>
                    <array>
                        <dict>
                            <key>Title</key>
                            <string>Paper 1</string>
                            <key>Children</key>
                            <array>
                                <dict>
                                    <key>Title</key>
                                    <string>2010</string>
                                    <key>Url</key>
                                    <string>http://www.examinations.ie/archive/exampapers/2010/LC003GLP100EV.pdf</string>
                                </dict>
                                <dict>
                                    <key>Title</key>
                                    <string>2009</string>
                                    <key>Url</key>
                                    <string>http://www.examinations.ie/archive/exampapers/2009/LC003GLP100EV.pdf</string>
                                </dict>
                            </array>
                        </dict>
                        <dict>
                            <key>Title</key>
                            <string>Paper 2</string>
                            <key>Children</key>
                            <array>
                                <dict>
                                    <key>Title</key>
                                    <string>2010</string>
                                    <key>Url</key>
                                    <string>http://www.examinations.ie/archive/exampapers/2010/LC003GLP200EV.pdf</string>
                                </dict>
                                <dict>
                                    <key>Title</key>
                                    <string>2009</string>
                                    <key>Url</key>
                                    <string>http://www.examinations.ie/archive/exampapers/2009/LC003GLP200EV.pdf</string>
                                </dict>
                            </array>
                        </dict>
                    </array>
                </dict>
            </array>
        </dict>
    </array>
</dict>
</plist>
enter code here

At this line you are actually fetching a string (" English "): 在这一行,您实际上是在获取一个字符串(“ English ”):

NSArray *array = [dictionary objectForKey:@"Title"];

next you do: 接下来,您要做的是:

selectedSubject = [array objectAtIndex:indexPath.row];

this crashes because the object is a string, not an array. 这崩溃,因为对象是字符串,而不是数组。

This should work: 这应该工作:

NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.text = [dictionary objectForKey:@"Title"];

For your warning Local declaration... It seems that you have taken table through xib.... and the name you have given must be tableview...and the name in the tableview delegate and data source method is also tableview. 警告您的本地声明...似乎您已通过xib取得了表格...。并且给定的名称必须是tableview ...,而tableview委托和数据源方法中的名称也为tableview。

Change the name of your tableview to something else,like presentTableView or what ever you want. 将表视图的名称更改为其他名称,例如presentTableView或您想要的名称。 Now this will not show you this type of warning..as there was the same name..... 现在,它不会向您显示这种类型的警告..因为名称相同.....

Or it may be the cause that you have declared object in your .h and .m file with the same name 或可能是因为您在.h和.m文件中声明了相同名称的对象

e.g. if i write 
NSString *myStr; 

in my .h file 在我的.h文件中

and in my .m file if i also write 如果我也写在我的.m文件中

 NSString *myStr;
 myStr== [NSString stringWithFormat:@"bg%d_iPad.png"];

The compiler will show warning that local declaration of variable hide instance variable something like that. 编译器将显示警告,变量的局部声明会隐藏实例变量。 Hope this will help you 希望这个能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM