iOS 不符合键值编码的 uitableview

Posted

技术标签:

【中文标题】iOS 不符合键值编码的 uitableview【英文标题】:iOS not key value coding compliant uitableview 【发布时间】:2012-10-12 00:11:10 【问题描述】:

我有一个 UIViewController,里面有一个 UITableView。 UIViewController 不是 UITableViewController,因为视图上还有其他内容。如果我不连接任何插座,空表和其他东西一起出现在视图上,没有错误。如果我将 UIViewController 的插座连接到 UITableView,我会收到以下错误:

'[ setValue:forUndefinedKey:]: 这个类不符合 key tableStories 的键值编码。'

如果我还将数据源和委托出口从 UITableView 连接到 UIViewController,我会得到同样的错误。这是视图控制器的 .h

#import <UIKit/UIKit.h>

@interface IntroViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, weak) IBOutlet UITableView *tableStories;
@property (nonatomic, strong) NSManagedObjectContext* managedObjectContext;
@property (nonatomic, strong) NSArray *stories;

这是.m

@implementation IntroViewController

@synthesize tableStories;
@synthesize stories;
@synthesize managedObjectContext;


- (void)viewDidLoad

    [super viewDidLoad];

    tableStories.delegate = self;
    tableStories.dataSource = self;

    managedObjectContext = ((ReadMeAppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"RMStory" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error;
    self.stories = [managedObjectContext executeFetchRequest:fetchRequest error:&error];




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


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

    return [stories count];


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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) 
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    RMStory *thisStory = [stories objectAtIndex:indexPath.row];
    cell.textLabel.text = thisStory.title;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", thisStory.author];

    return cell;

这可能与我的 Core Data 命令有关,但我一直在尝试堆栈中提到的其他解决方案以克服此错误,但到目前为止没有任何效果。 :-/

另外,我从另一个应用程序导入了故事板,所以这可能会造成一些麻烦。

【问题讨论】:

为了清楚起见,我没有将任何其他网点或其他任何东西连接到上述网点。 What does this mean? "'NSUnknownKeyException', reason: ... This class is not key value coding-compliant for the key X"的可能重复 【参考方案1】:

我想通了。我在调试器输出的一开始就收到一个错误,上面写着

Unknown class IntroViewController in Interface Builder file.

我在 StackExchange 上查找了错误并找到了答案 here 是 Matthew 给出了我使用的答案。我不得不将 IntroViewController 添加到编译源列表中。我不确定这意味着什么,或者为什么它没有阻止程序在没有表格视图的情况下运行,但现在它可以工作了。感谢 StackExchange!

【讨论】:

如果您没有声明@implementation,也可能发生这种情况。我会在***.com/questions/1725881/… 和***.com/questions/12790902/… 进一步讨论【参考方案2】:

在 IB 中,不要忘记将您的 UIViewController 类设置为 IntroViewController

【讨论】:

我仔细检查了一遍,把它换了回来,然后重新连接了所有的插座,我得到了同样的错误。不过谢谢! 如果是这样,那么您是否检查过用于视图控制器的 init 实际上是加载 nib 文件的那个?

以上是关于iOS 不符合键值编码的 uitableview的主要内容,如果未能解决你的问题,请参考以下文章

iOS 不符合键值编码的 uitableview

对于使用 RestKit v0.20.0 的键 Text,此类不符合键值编码

此类不符合 @IBInspectable 的键值编码

类不符合键值编码

RestKit - 不符合键值编码

此类不符合键的键值编码