Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-27-2011, 06:02 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 77
aceallways is on a distinguished road
Default Issue with retaining Datepicker date

Hi everyone, I have a simple problem that I cannot seem to fix.

I have a datepicker, and am trying to convert the date to an NSString, then display it in a tableview. It works, until I reload the table then the pointer is lost.

The code:
Code:
		
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd, yyyy"];
lastName=[formatter stringFromDate:[[datePicker date]copy]];
I just need a way to make sure lastname (NSString) will not depend on the DateSelector, but rather be a deep copy.

Thanks!
__________________
http://www.meerkata.com
aceallways is offline   Reply With Quote
Old 05-27-2011, 08:55 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by aceallways View Post
Hi everyone, I have a simple problem that I cannot seem to fix.

I have a datepicker, and am trying to convert the date to an NSString, then display it in a tableview. It works, until I reload the table then the pointer is lost.

The code:
Code:
		
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd, yyyy"];
lastName=[formatter stringFromDate:[[datePicker date]copy]];
I just need a way to make sure lastname (NSString) will not depend on the DateSelector, but rather be a deep copy.

Thanks!
You want to save the formatted date string in lastName?

You want to copy the string. Your code is copying the date. You need to move your brackets around like this:


Code:
lastName=[[formatter stringFromDate: [datePicker date]] copy];
Note that you'll own lastName, and be responsible for releasing it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 05-28-2011, 07:45 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 77
aceallways is on a distinguished road
Default

Thanks you so much!

I just release it on dealloc as it is used throughout the life of the view, is this acceptable?

Thanks again!
__________________
http://www.meerkata.com
aceallways is offline   Reply With Quote
Old 05-28-2011, 08:24 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by aceallways View Post
Thanks you so much!

I just release it on dealloc as it is used throughout the life of the view, is this acceptable?

Thanks again!
If the code you posted is run once and only once in the life of an object, you're ok. However, if that code is ever run more than once, you will have a memory leak.


Say you put the code you posted in a view controller's viewDidLoad method.

Code:
lastName=[[formatter stringFromDate: [datePicker date]] copy];
The first time through, lastName is nil when that statement executes. Then, later in the life of the app, the system generates a low memory warning and the view controller unloads it's view. The next time you try to display the view controller's view, it calls viewDidLoad a second time.

This time, there is already an object in the lastName instance variable. You execute the assignment above, and the old value in lastName is replaced with a new date string, but you never release the old object, thus leaking it.

You would be better off setting up lastName as a retained (or copied) property). Then, use the syntax:

Code:
self.lastName=[formatter stringFromDate: [datePicker date]];

The setter for the lastName property takes care of retaining (or copying) the string, but first it releases any old value stored there.

Then, in your dealloc method, just add:

Code:
self.lastName = nil;
Properties take care of a lot of the details of memory management for you, meaning you don't have to write a bunch of error-prone memory management code.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 345
6 members and 339 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0