Objective-C : Changing “self” value inside self

I have a category on NSDate, and I want to implement some functions to manipulate the date, like :

NSDate *thedate = [NSDate date];
[thedate setToMidnight];

so I have a function in NSDate like :

-(void)setToMidnight {
   some code with calendars and comps

   self = theNewDate;
}

This works inside the function, but outside this member function, thedate has not changed. I understand this malfunction because I've been told that self is just a local variable created inside the member function. So, how can I make this work ?

Of course, I could have written :

thedate = [thedate dateAsMidnightDate]
or thedate = [NSDate dateAtMidnightFromDate:thedate]

but I feel it has more sense inside the instance class, as I don't want to change the date but just adjust some values of the previously created one.

Answers


You can't. Specifically:

  1. A function or method cannot modify local variables in the calling context.

  2. NSDate is not mutable (i.e. you can't modify an NSDate once it's created).

Therefore, no such method can be written. The closest you can get would be a class that wraps an NSDate and forwards messages to that internal NSDate object, which reassigns the date instance variable when you want to make it represent a new date.


Need Your Help

How to remove “Quick edit” option in “register_post_meta” list page

php wordpress

I am using register_post_type in list page automatically it is four actions Edit/Quick Edit/trash/View. I want to remove "Quick edit" option from list page. How i can do this.Please help me ASAP.Its

Write to memory buffer instead of file with libjpeg?

c++ c libjpeg

I have found this function which uses libjpeg to write to a file:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.