Search
Rich's Mad Rants
Powered by Squarespace

Creating iOS 5 Apps Discussion > WeightEntry property date ..copy or assign

I was having a problem with using following property attributes inside WeithEntry.h

@property(nonatomic, copy, readonly) NSDate* date

I was getting EXC_BAD_ACCESS exception when I was trying to configure HistoryCell in configureWithWeightEntry call from cellForRowAtIndexPath.

So I changed it to "copy" in property declaration and seems to work fine.

I dont know what drives me to change this to copy but I suspect we are using self.currentDate = [NSDate date inside EnterWeightController and currentDate is set to system date only in viewDidAppear method.

Also currentDate attribue is strong in property declaration im unsure why I get this exception.


Yes and I get this exception when I have more than 1 Weight entry added and when I switch to HistoryViewController

Please explain. ? Im using SDK iOS 5.1

March 24, 2012 | Unregistered CommenterGuru

as a matter of fact the strong reference to date also seems to work in my case other than copy. But assign doest seems to work and i get runtime exception.

Reading about assign in the book (pg 80) it doesn't perform any memory management. This should only be used storing non-objects data (e.g. floats, ints, structs etc) but Im not sure how NSDate (being an object) falls into this category that author wrote used as "assign"

Has anyone faced this issue? Please explain.

March 25, 2012 | Unregistered CommenterGuru

Guru,

I'll need to check the source code and book text to see what's going on here, but in general you're correct. EXC_BAD_ACCESS usually means you're trying to call methods on an object that has been deallocated. Both copy and strong will retain the object for you (preventing it from being deallocated). Retain will as well, though it's generally not used under ARC (though it may appear when subclassing NSManagedObjects, since Core Data handles the memory management behind the scenes anyway).

It may just be a typo in the text. I'll check on it when I get back to my computer, and let you know what I find.

-Rich-

March 25, 2012 | Registered CommenterRichard Warren

thanks, i will wait for your answer. So far strong works for me very well and just finished Chapter 4) so excited to read next. Hope time permits from day to day schedule

somehow notification on this page doest work :( it only send email when i post something or reply to my own posts

March 25, 2012 | Unregistered CommenterGuru

Hey, Guru,

In chapter 3, page 141, the WeightEntry properties are defined as follows:


@interface WeightEntry : NSObject
@property (nonatomic, assign, readonly) CGFloat weightInLbs;
@property (nonatomic, strong, readonly) NSDate* date;
@end

Notice that date uses (nonatomic, strong, readonly) while weightInLbs uses (non atomic, assign, readonly). That's because the date is an object (so we need to tell ARC how to manage its memory), while weightInLbs is simply a float value (no memory management is needed).

I probably didn't explain it clearly enough in the text, but it looks like you might have simply misread it, and used assign for both lines.

Does that make sense?

-Rich-

March 27, 2012 | Registered CommenterRichard Warren

Oh, I've also filed a help ticket about the notifications. Let me know if they start working or if you have any other problems.

-Rich-

March 27, 2012 | Registered CommenterRichard Warren

Hi Rich

My bad yes it's indeed strong for NSDate and not sure how I missed it first time. But at least happy to found out and debug to correct it :)

Thanks for your help
Guru

March 27, 2012 | Unregistered CommenterGuru