NSDate 被自动释放导致 Bad exe 错误。 iPhone

发布于 2024-11-17 21:57:13 字数 663 浏览 6 评论 0原文

我花了一整天的时间来研究这个问题,重写了很多次,试图弄清楚它。

我在标题视图中声明了一组日期,

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抽个烟儿 2024-11-24 21:57:13

您需要将该属性设置为 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.

无畏 2024-11-24 21:57:13

问题是,您没有设置该属性。 time 没有使用点符号。您必须将其写为(假设该属性存在于 self 上),可以采用以下两种方式之一:

self.time = [dateFormatter dateFromString:datetime];

[self setTime: [dateFormatter dateFromString:datetime]];

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:

self.time = [dateFormatter dateFromString:datetime];

or

[self setTime: [dateFormatter dateFromString:datetime]];
浮生未歇 2024-11-24 21:57:13

您需要使用将其分配给您的财产

self.time = [dateFormatter dateFromString:datetime];

,否则它不会被保留

You need to assign it to your property using

self.time = [dateFormatter dateFromString:datetime];

Otherwise it doesn't get retained

静待花开 2024-11-24 21:57:13

尝试使用 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.

剩余の解释 2024-11-24 21:57:13

您需要使用: self.time = [dateFormatter dateFromString:datetime];

You need to use: self.time = [dateFormatter dateFromString:datetime];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文