简体   繁体   English

我的程序不断崩溃,我不知道为什么

[英]My program keeps crashing and I can't figure out why

I am new to all this but I am wondering if I can receive a little help. 我不熟悉所有这些,但是我想知道我是否可以得到一点帮助。 I am trying to get the days left from a startDate and endDate. 我想让startDate和endDate剩下的日子。 I thought I found the answer in listing #12 in the Apple docs here ---> link 我以为我在这里的Apple文档中的清单12中找到了答案---> 链接

After scouring stackoverflow, this is what I came up with. 在搜寻stackoverflow之后,这就是我的想法。

    //My Code (This is code I found on stack overflow and modified
    //Start Date
    //Creating a date from a string then converting it into an NSDate Object
    NSString *startDateString = @"2016-01-01 00:00:00";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSDate *startDate = [dateFormatter dateFromString:startDateString];

    //End Date
    NSString *endDateString = @"2016-12-25 00:00:00";
    NSDateFormatter *endDateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSDate *endDate = [endDateFormatter dateFromString:endDateString];


    //This is Apple's code.

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSUInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay;

    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];
    NSInteger months = [components month];
    NSInteger days = [components day];


    NSLog(@"there are %ld months and %ld days left till Christmas", months, days);

The program keeps crashing at, 该程序不断崩溃,

 NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];

The question, 这个问题

Can anyone guide me on how to resolve this issue? 谁能指导我如何解决此问题?

Simples. Simples。 What dateFormat did you send for endDateFormatter? 您为endDateFormatter发送了什么dateFormat?

I'm not asking what dateFormat you think you did set, but which format you did set. 我不要求什么DATEFORMAT你认为你没有设置,但你设置其格式。 Two different questions with entirely different answers. 两个完全不同的答案。

Now you ask if we can guide you how to resolve this: 现在,您问我们是否可以指导您解决此问题:

Learn to use the debugger. 学习使用调试器。 The obvious thing to do is step through the code line by line, and examine each value that is produced. 显而易见的事情是一步一步地遍历代码,并检查所产生的每个值。 You would then notice before components:fromDate:toDate: crashes, that your endDate is nil. 然后,您会在component:fromDate:toDate:崩溃之前注意到,您的endDate为零。

Most importantly: Learn the first rule of debugging: You did something wrong, and you need to find out what it is. 更重要的是:了解调试的第一条规则: 做错了什么,你需要找出它是什么。 That reduces the possibilities so much. 这极大地降低了可能性。 There are only 11 lines of code before the crash. 崩溃前只有11行代码。 One of them is wrong. 其中之一是错误的。 One of them you wrote wrong. 其中之一写错了。 Just go through them one by one, with the firm conviction in your mind that one of the lines is wrong , and it would be obvious. 只要一一仔细地研究它们, 并坚定地相信其中一条线是错误的 ,这是显而易见的。

(That's how I found the bug. I looked at the code with the conviction that there is a mistake, and searched for the mistake, and with that attitude it's easy to find. Beginners are convinced their code is correct, and as a result they can't see the most obvious bugs). (这就是我发现错误的方式。 坚信存在错误的情况下查看了代码,并以错误的态度进行搜索,并且以这种态度很容易找到。初学者确信他们的代码是正确的,因此他们看不到最明显的错误)。

Or examine your code variable by variable. 或者逐个检查您的代码变量。 Check how dateFormatter gets created and changed. 检查dateFormatter如何创建和更改。 Check how endDateFormatter gets created and changed. 检查endDateFormatter如何创建和更改。 Anything unusual that you can see? 有什么异常之处吗?

BTW. BTW。 The YYYY vs yyyy bug produces lots of laughter among the more experienced developers and creates enormous frustration among the less experienced at the beginning of each year. YYYY与yyyy错误在每年年初的开发人员中引起了很多笑声,并在经验不足的开发人员中产生了极大的挫败感。 Up to three days around New Year every year that bug causes many applications to display years wrong. 每年大约在新年前后三天,该错误会导致许多应用程序显示错误的年份。

BTW. BTW。 A better question than "how to find this problem" would be "how to avoid the problem". 一个比“如何找到这个问题”更好的问题是“如何避免这个问题”。 Maddy remarked (and I'd say 99% chance the it is correct) that this is a copy / paste error. Maddy说(这是正确的几率是99%),这是复制/粘贴错误。

Ignoring the fact that two date formatters were not needed: After a copy / paste like that, I use the features of the editor. 忽略不需要两个日期格式化程序的事实:像这样的复制/粘贴之后,我使用了编辑器的功能。 Obviously you need to change uses of startDateString, dateFormatter and startDate. 显然,您需要更改startDateString,dateFormatter和startDate的用法。 The compiler even tells you, because immediately after the paste operation you have three re-declared variables. 编译器甚至告诉您,因为粘贴操作之后,您立即拥有三个重新声明的变量。

What I do: Double-click on startDateString giving the first error. 我的工作:双击startDateString给出第一个错误。 Command-Shift-E (make it the search string). Command-Shift-E(使其成为搜索字符串)。 Type in the new name endDateString, double-click on it and Command-C (copy). 键入新名称endDateString,双击它并按Command-C(复制)。 Command-G to find the next occurence of startDateString, Command-V (paste) to replace it with the new name, Command-G again until all uses of the name are changed. Command-G查找下一次出现的startDateString,Command-V(粘贴)将其替换为新名称,再次使用Command-G,直到更改了该名称的所有用法。 Then the same with the second error. 然后与第二个错误相同。 Practice using your editor. 练习使用编辑器。

暂无
暂无

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

相关问题 SecItemUpdate()不断返回errSecParam,我不知道为什么 - SecItemUpdate() keeps returning errSecParam and I can't figure out why 我不知道为什么我的电话使用本地时区的代码不起作用 - I can't figure out why my code for using the local timezone for the phone doesn't work Segue无法正常工作,我不知道为什么 - Segue is not working, and i can't figure out why 我无法弄清楚为什么不按照我的说明打印标签? - I am unable to figure out why my label isn't printing for my instructions? Swift UI - 我不知道为什么这个按钮不会打印我设置的操作 - Swift UI - I can't figure out why this button won't print the action I set 无法弄清楚为什么我得到“类没有初始化程序” - Can't figure out why I am getting “Class has no initializers” 我想水平和垂直滚动我的CollectionViews。 无法弄清楚怎么做 - I would like to scroll my CollectionViews both horizontal and vertical. Can't figure out how to do it 我的 AVSpeechSynthesizer(文本到语音)正在以非常快的速度说话,我不知道如何减慢速度 - My AVSpeechSynthesizer (text to speech) is talking at a very fast rate and I can't figure out how to slow it down 在UITextView上调用setAttributedText会重置字体大小,我不知道为什么 - Calling setAttributedText on UITextView resets the font size, and I can't figure out why 无法弄清楚为什么对象会过早地释放 - Can't figure out why object is deallocated prematurely
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM