gpt4 book ai didi

ios - 由于未捕获的异常 'NSInvalidArgumentExceptioneason: ' -[__NSCFDictionary 练习] : unrecognized selector 而终止应用程序

转载 作者:行者123 更新时间:2023-11-29 03:21:09 25 4
gpt4 key购买 nike

我是 xcode 新手,我遇到了这个问题,当我尝试点击进入 DetailViewcontroller 的单元格并出现错误时,应用程序崩溃并给出此错误:* 由于未捕获的异常而终止应用程序' NSInvalidArgumentException',原因:'-[__NSCFDictionaryExercise]:无法识别的选择器发送到实例 0x8ca15b0'。我不确定是什么原因。我在这里复制了代码:

MAsterViewcontroller.h

    #import <UIKit/UIKit.h>

@interface MasterViewController : UITableViewController{

NSMutableArray* routines_;
NSMutableArray *routineinfo_;
}

@property (nonatomic,retain)NSMutableArray* routines;
@property (nonatomic,retain)NSMutableArray* routineinfo;

@end

文件MasterViewController.m

    #import "MasterViewController.h"

#import "ClassDetailViewController.h"
#import "ClassConstants.h"

@implementation MasterViewController

@synthesize routines=routines_,routineinfo=routineinfo_;


- (void)awakeFromNib
{
[super awakeFromNib];
}


- (void)viewDidLoad

{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Mills" ofType:@"plist"];

routines_= [[NSMutableArray alloc] initWithContentsOfFile:path];

//Uncomment the following line to displa an Edit button in the navgation bar for this view controller.

self.navigationItem.leftBarButtonItem = self.editButtonItem;

}


- (void)dealloc {
[routines_ release];
[routineinfo_ release];
[super dealloc];
}


- (void)insertNewObject:(id)sender
{
// if (!_objects) {
// _objects = [[NSMutableArray alloc] init];
// }
// [_objects insertObject:[NSDate date] atIndex:0];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

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



//Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.routines count];

}


//Configure appearence of table view cells.


- (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];
}

//Configure the cell.
cell.textLabel.text = [[self.routines objectAtIndex:indexPath.row]
objectForKey:NAME_KEY];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {

// Get the data given the selected row
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger IndexSelected=[indexPath row];
//NSDictionary *routinedata = [self->routines_ objectAtIndex:[indexPath row]];

// Pass that data on to the DetailViewController
DetailViewController *detailVC = segue.destinationViewController;


NSString *pathSelected=@"";;


switch (IndexSelected)

{
case 0:

pathSelected = [[NSBundle mainBundle] pathForResource:@"Mills0" ofType:@"plist"];

break;

case 1:
pathSelected = [[NSBundle mainBundle] pathForResource:@"Mills1" ofType:@"plist"];

break;

case 2:
pathSelected = [[NSBundle mainBundle] pathForResource:@"Mills2" ofType:@"plist"];

break;

default:
break;

}
routineinfo_= [[NSMutableArray alloc] initWithContentsOfFile:pathSelected];

detailVC.routineinfo = self.routineinfo;


}
}



@end


#import <UIKit/UIKit.h>
@interface DetailViewController : UITableViewController{

@private
NSMutableArray *routineinfo_;


}
@property (nonatomic, retain) NSMutableArray *routineinfo;


@end

ClassDetailViewController.h

#import <UIKit/UIKit.h>
@interface DetailViewController : UITableViewController{

@private
NSMutableArray *routineinfo_;


}
@property (nonatomic, retain) NSMutableArray *routineinfo;


@end

ClassDetailViewController.m文件

    #import "ClassDetailViewController.h"
#import "ClassConstants.h"
#import "MillsDetailsConstant.h"
#import "LesMillsCell.h"

@implementation DetailViewController
@synthesize routineinfo=routineinfo_;



- (void) dealloc {

[super dealloc];
[routineinfo_ release];

}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.routineinfo count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"LesMillsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


LesMillsCell *LesMillsCell=self.routineinfo [indexPath.row];


//Configure the cell EXERCISE
LesMillsCell.Exercise.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:EXERCISE_KEY];


//Configure the cell SONG
LesMillsCell.Title.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:SONG_KEY];

//Configure the cell ARTIST
LesMillsCell.Artist.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:ARTIST_KEY];

//Configure the cell TIME
LesMillsCell.Artist.text= [[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:TIME_KEY];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

}

//Configure appearence of table view cells.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];


//setto la sezione
// NSInteger nSection;
// setto le righe per sezione
// NSInteger nRows;
// nRows= [self.tableView numberOfRowsInSection:1];
// creo una cella prptotypes per ogni riga della sezione
// NSIndexPath IndexTableView



// nRows=self.tableView
}

@end

子类文件 LesMillsCell.h

#import <UIKit/UIKit.h>

@interface LesMillsCell : UITableViewCell{

@private

// UITableViewCell *lesmillscell_;
UILabel *Exercise_;
UILabel *Title_;
UILabel *Artist_;
UILabel *Time_;
NSMutableArray *routineinfo_;



}


@property (nonatomic, retain) IBOutlet UILabel *Exercise;
@property (nonatomic, retain) IBOutlet UILabel *Title;
@property (nonatomic, retain) IBOutlet UILabel *Artist;
@property (nonatomic, retain) IBOutlet UILabel *Time;





@end

LesMillsCells.m

#import "LesMillsCell.h"
#import "ClassConstants.h"
#import "MillsDetailsConstant.h"

@implementation LesMillsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

谢谢你的帮助!

最佳答案

如果崩溃发生在这一行:

LesMillsCell.Exercise.text= [[self.routineinfo objectAtIndex:indexPath.row] objectForKey:EXERCISE_KEY];

这意味着LesMillsCell不是LesMillsCell类的对象,而是NSDictionary(或__NSCFDictionary)。因此,在该对象上使用Exercise会导致错误,因为字典不支持该方法。错误从这里开始:

LesMillsCell *LesMillsCell=self.routineinfo [indexPath.row];

self.routineinfo [indexPath.row] 返回类型为 id 的对象,因此编译器无法知道该类型实际上是什么类型,因此赋值会在没有警告的情况下进行。

此外,您还以两种不同的方式使用routineinfo。首先你把它当作 LesMillsCell

LesMillsCell *LesMillsCell=self.routineinfo [indexPath.row];

下面的一行将其视为字典,并提取 EXERCISE_KEY 的值:

[[self.routineinfo objectAtIndex:indexPath.row]
objectForKey:EXERCISE_KEY];

关于ios - 由于未捕获的异常 'NSInvalidArgumentExceptioneason: ' -[__NSCFDictionary 练习] : unrecognized selector 而终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com