iPhone 开发 - NSInternalInconsistencyException 帮助?

Posted

技术标签:

【中文标题】iPhone 开发 - NSInternalInconsistencyException 帮助?【英文标题】:iPhone dev - NSInternalInconsistencyException help? 【发布时间】:2009-08-19 15:07:13 【问题描述】:
2009-08-19 11:00:06.482 Pickers[26090:20b] *** 断言失败 -[UIDatePickerView _updateBitsForDate:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-963.10/UIDatePicker.m:908
2009-08-19 11:00:06.483 Pickers[26090:20b] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:日期”
2009-08-19 11:00:06.484 选择器[26090:20b] 堆栈:(
    807902715,
    2513055291,
    807986683,
    811271572,
    816448278,
    816444436,
    816445688,
    10353,
    815223951,
    815274694,
    815269866,
    815277278,
    9116,
    814713539,
    814750709,
    814739251,
    814722434,
    814748641,
    839148405,
    807687520,
    807683624,
    814715661,
    814752238,
    8242
)

我不断收到该错误。我已将其追踪到有问题的类 DatePickerViewController,因为当我不将其包含在 UITabBarController 中时,它不会崩溃。整个项目编译得很好。这是 DatePickerViewController:

/*
 File : DatePickerViewController.m
 Abstract: View Controller
 project Pickers

 Copyright (C) 2009 Mitchell K. All Rights Reserved.

 */

#import "DatePickerViewController.h"
#import "Model.h"


@implementation DatePickerViewController

@synthesize datePicker;


#pragma mark -
#pragma mark Setting up / Tearing down

- (id)init 
    if((self = [super init])) 
    
    return self;


- (void)dealloc 
    [datePicker release];
    [super dealloc];


- (void)loadView 
    UIView *localView = [[UIView alloc] initWithFrame:
                              CGRectMake(0, 0, 320, 411)];
    [localView setBackgroundColor:[UIColor whiteColor]];
    [self setView:localView];
    [localView release];


- (void)viewDidLoad 
    UIDatePicker *localDatePicker = [[UIDatePicker alloc] initWithFrame:
                        CGRectMake(0, 0, 320, 216)];
    // ======******OFFENDING LINE BELOW************=======
    [localDatePicker setDate:[[Model sharedModel] dateCache] animated:NO];
    /// =====**********========================*************===========
    [localDatePicker addTarget:self action:@selector(datePicker_ValueChanged:)
                                forControlEvents:UIControlEventValueChanged];
    [self setDatePicker:localDatePicker];
    [localDatePicker release];

    UIButton *btnSelect = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSelect setFrame:CGRectMake(100, 298, 120, 37)];
    [btnSelect setTitle:@"Select" forState:UIControlStateNormal];
    [btnSelect addTarget:self action:@selector(btnSelect_Pressed:)
        forControlEvents:UIControlEventTouchUpInside];

    [[self view] addSubview:[self datePicker]];
    [[self view] addSubview:btnSelect];

    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self
        selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];

    [super viewDidLoad];


- (void)viewDidUnload 
    [self setDatePicker:nil];
    [super viewDidUnload];


#pragma mark -
#pragma mark Timer methods

- (void)timerFireMethod:(NSTimer *)theTimer 
    NSDate *now = [[NSDate alloc] init];
    [[self datePicker] setDate:now animated:YES];
    [now release];
    [theTimer invalidate];


#pragma mark -
#pragma mark Control actions

- (void)datePicker_ValueChanged:(id)sender 
    [[Model sharedModel] setDateCache:[[self datePicker] date]];


- (void)btnSelect_Pressed:(id)sender 
    NSString *selected = (NSString *)[[self datePicker] date];;
    NSString *msg = [[NSString alloc] initWithFormat:
        @"The date and time you selected is: %@", selected];
    UIAlertView *alert = [[UIAlertView alloc]
                initWithTitle:@"Date and Time Selected"
                message:msg
                delegate:nil 
            cancelButtonTitle:@"Yes, I did" 
            otherButtonTitles:nil];
    [msg release];
    [alert show];
    [alert release];


#pragma mark -
#pragma mark Memory warning

- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];


@end

我对 iPhone 开发还很陌生。所以对我做错了什么有任何猜测吗?谢谢!!

编辑:我在代码中指出的违规行导致了它。我忘了检查我试图将 datePicker 的日期设置为的值是否为零。确实如此,所以这就是它崩溃的原因。

【问题讨论】:

【参考方案1】:

嗯,在您使用 DatePicker 时出现了一些错误,您发布的所有代码都很难弄清楚,您是否可以单步执行代码并标记导致引发异常的行?

【讨论】:

好的,我仍在努力。请稍后再回来查看更多信息。 感谢您的快速回答:)。我想通了,是因为我没有检查日期是否为零。【参考方案2】:

您可以尝试将 datePicker_didChange 方法签名更改为 something like this,但我认为这不是问题所在。我认为这与您的消息处理程序中调用[[self datePicker] date] 然后将NSDate 转换为NSString 的代码有关。如果需要字符串,请关注Date / Time Formatting Guide。

如果有一个人类可读的堆栈跟踪可能会有所帮助。从 GDB 控制台输入“bt”或“backtrace”。希望有帮助:)

【讨论】:

谢谢,但我已经解决了。字符串/日期不是问题,尽管我决定将其更改为使用 NSDateFormatter。谢谢!

以上是关于iPhone 开发 - NSInternalInconsistencyException 帮助?的主要内容,如果未能解决你的问题,请参考以下文章

iphone开发者选项设置在哪

我可以在 iphone3.0 上安装用 iphone sdk 3.1.3 开发的 iphone 应用程序吗?

iPhone 第一天笔记2016年03月28日(周一)A.M iPhone开发入门

开始 iPhone 开发 [重复]

XCode - iPhone 开发团队

无法在 Swift 4 上的 XCUITest 中设置设备方向