NSDate 被自动释放导致 Bad exe 错误。 iPhone
我花了一整天的时间来研究这个问题,重写了很多次,试图弄清楚它。
我在标题视图中声明了一组日期,
@property (nonatomic, retain)NSDate *time;
确实加载了我从数据库读取的字符串,将其转换为日期并将其设置为该变量。
NSDateFormatter *dateFormatter =[[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd / MMMM / yyyy"];
time=[dateFormatter dateFromString:datetime];
如果我 NSLog time 我得到预期的日期(尽管不是正确的格式)。如果我然后在视图中进一步使用 NSDate,我会收到 Bad Exe 错误,或者由于时间 = 为零而导致的错误。
我不明白这几乎就像它没有保留它一样。
在解决问题时,我在 IBAction 中创建了一个新日期,并尝试复制上面的代码,但在需要的地方,我遇到了同样的问题。我认为这是日期格式问题,但我将其格式化的方式与最初编写为字符串的方式相同。
我做错了什么?我不能再在这件事上再浪费一天的时间了。
加油
丹
I have spent all day working on this, re-writing this many a times trying to get my head around it.
I have a set of dates declared in the Header
@property (nonatomic, retain)NSDate *time;
On view did load i read a string from a database, convert it to a date and set it to this variable.
NSDateFormatter *dateFormatter =[[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd / MMMM / yyyy"];
time=[dateFormatter dateFromString:datetime];
if i NSLog time i get the date expected (although not the right format). if i then go to use the NSDate further on in the view i get a Bad Exe error, or error due to time being = to nil.
I dont understand it is almost like its not retaining it.
Whilst problem-solving i created a new date within an IBAction and tried to replicate the code above but where it was required and i get the same issue. I assume that it is dateformat issue but i am formatting it the same way it was originally written as a string.
What am i doing wrong? i cant afford to loose another day on this.
Cheer
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将该属性设置为
self.time = [dateFormatter dateFromString:datetime];
或[self setTime: [dateFormatter dateFromString:dateTime]]
如果您只是使用= 不使用 self,它不会保留它,它只会传输指针。使用点运算符将使其保留它。
You need to the the property as
self.time = [dateFormatter dateFromString:datetime];
or[self setTime: [dateFormatter dateFromString:dateTime]]
If you just assign it using = without using self, it won't retain it, it will just transfer the pointer. using dot operator will make it retain it.
问题是,您没有设置该属性。
time
没有使用点符号。您必须将其写为(假设该属性存在于 self 上),可以采用以下两种方式之一:或
The problem is, you're not setting the property.
time
isn't using the dot notation there. You either have to write that as (assuming the property exists on self), it one of two ways:or
您需要使用将其分配给您的财产
,否则它不会被保留
You need to assign it to your property using
Otherwise it doesn't get retained
尝试使用 self.time ,这将通过生成的 setter 方法,该方法将保留日期,此时您只是将其分配给实例变量。
try using self.time , this will go through the generated setter method, which will retain the date, at the moment you are just assigning it to the instance variable.
您需要使用: self.time = [dateFormatter dateFromString:datetime];
You need to use:
self.time = [dateFormatter dateFromString:datetime];