Incompatible pointer type sending 'Class' to parameter of type 'NSDate *' - objective-c

I have an NSDate category with following method
#implementation NSDate (DateUtility)
+(NSString *)dateTimeStringForDB {
NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
[dateFormatForDB setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *aDateStr= [dateFormatForDB stringFromDate:self];
[dateFormatForDB release];
return aDateStr;
}
#end
with this definition I receive a warning .
Incompatible pointer type sending 'Class' to parameter of type 'NSDate *'
However type casting self before assign it as argument suppresses this warning.
+(NSString *)dateTimeStringForDB
{
NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
[dateFormatForDB setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *aDateStr= [dateFormatForDB stringFromDate:(NSDate*)self];
[dateFormatForDB release];
return aDateStr;
}
Can we really not pass self as an argument in a category without typecasting it ? What is this feature dependent on , the compiler ? Looking for an answer before actually posting it as a question on SO I came across this, however I am still not clear as to what goes behind the scene.

You have created a class method, I suspect you really want an instance method. I assume you want to convert an instance of an NSDate (ie an object) into an NSString representation. Currently you are trying to convert the actual NSDate class into an NSString representation.
Change
+(NSString *)dateTimeStringForDB {
to
-(NSString *)dateTimeStringForDB {

Related

how to use nsdate string objec for later use?

I'm so fedup with NSDate string object.
currently I am generating an unique id on the bases of NSDate as follows:
NSDate *current_date = [[NSDate date]retain];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"HHmmssddMMYY"];
NSString *unique_id=[df stringFromDate:current_date];
NSString * current_Test_id=[NSString stringWithString:unique_id];
NSLog(#"current_test_idString %#",current_Test_id);
The code above is generating unique id and prints successfully but if I am printing or accessing currtent_Test_id in another IBAction method then app crashes.
stringWithString will create an autorelease string, modify your code as
NSString * current_Test_id = [[NSString stringWithString:unique_id]retain];
Use this Method
- (NSString *)stringDateFromDate: (NSDate *) date{
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"HHmmssddMMYY"];
NSString *current_Test_id=[NSString stringWithString:[df stringFromDate:date]];
[df release];
NSLog(#"current_tst_id %#",current_Test_id);
return current_Test_id;
}
Call Method Like that
NSString *current_tst_id = [self stringDateFromDate:[NSDate date]];
an NSString (or any object, for that matter) created with a class method, and not an init method, will be autoreleased. This means on the next iteration of the event loop, current_Test_id is released, and now you have a pointer to a dead object.
See this similar question
As current_Test_id is instance method.
in the init (in case of mac os) or viewDidLoad (for ios) alloc+init it.
and then assign :
current_Test_id=[NSString stringWithString:unique_id]; //it will be in autorelease mode.
or
current_Test_id=[[NSString stringWithString:unique_id]retain];

Is there a way to get Xcode's debugger to show dates in local timezone (i.e., not UTC)?

I'm trying to debug code that makes pretty heavy use of dates which has me comparing tons of different NSDate values in the debugger. The debugger is displaying those dates in UTC format--for example:
date1 = (NSDate *) 0x01b11460 #"2012-02-15 18:55:00 +0000"
It would be a lot easier for me if it would show them in my local timezone as that is what the test code I'm debugging seems to be using.
I do have feeling that I'm missing something much more basic here so I'm hoping someone can enlighten me. Thanks in advance.
While a little "hackish" you can create a subclass of NSDate and override just the
-(NSString *)description;
method to return a date formatted however you want.
The debugger isn't doing any special decoding for you, it's just calling the "description" method of the object it wants to display. A handy trick... (Which is why all my objects publish a concise description suitable for viewing in a debugger.)
In the end, what worked best was in fact adding a Category for NSDate that just overrides the description method to return a string that represents the NSDate in my current timezone. I also made it DEBUG only as I really just need this override in place when debugging.
Here's the .h file I used:
#ifdef DEBUG
#interface NSDate (DebugHelper)
-(NSString *)description;
#end
#endif
and the .m file:
#ifdef DEBUG
#import "NSDate+DebugHelper.h"
#implementation NSDate (DebugHelper)
-(NSString *) description
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
return [dateFormatter stringFromDate:self];
}
#end
#endif
Thanks to Jim Hayes and jrturton for the discussion and ideas that led to this answer.
Just did the same thing, however instead of systemTimeZone I used localTimeZone (see the docs as to why it is slightly better). Also I noticed you are alloc'ing the dateFormatter object but never releasing it which is a leak.
Here's my category method:
-(NSString *) description;
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSString *dateString = [dateFormatter stringFromDate:self];
return dateString;
}

How do I retrieve a strings from an NSDate?

The function below (i.e. dateChanged()) is triggered by a UIDatePickersolve . My problem is that
NSLog(#"Future: %#", futureDate);
returns 'null'. However,
NSLog(#"Today: %#", today);
works just fine.
I know that casting sender as a UIDatePicker, allows me to solve the problem using:
futureDate = [dateFormat stringFromDate:sender.date];
but I cannot understand why I cannot cast sender as an NSDate. Any insight would be much appreciated.
//- (IBAction)dateChanged:(UIDatePicker*)sender {
- (IBAction)dateChanged:(NSDate *)sender {
NSDate* todaysDate = [NSDate date];
NSDateFormatter* dateFormat;
dateFormat=[[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d, yyyy hh:mm:ssa"];
NSString * today;
NSString * futureDate;
today=[dateFormat stringFromDate:todaysDate];
NSLog(#"Today: %#", today);
futureDate = [dateFormat stringFromDate:sender];
NSLog(#"Future: %#", futureDate);
}
Sure you can cast sender to be NSDate*. Or UIButton* - or whatever else. However it does't change the fact that date picker implementation sends an UIDatePicker* as a parameter of delegate message and the casting will be invalid. The most flexible delegate messages have id as a type of returned parameter, but the object passed is always an object of a certain class. And the Objective-c casting only makes your debugging and dev process easier with code completion and warnings for the casted class.
That would need to be casted as a UIDatePicker not NSDate. Also, it is easy to change the passed in object from type id to the native type in Interface Builder. This makes the connection properly in both the interface and implementation files. I do this occasionally when using custom subclasses of objects like UIButtons and such.
- (IBAction) dateChanged:(UIDatePicker *)sender {
NSDate* todaysDate = [NSDate date];
NSDateFormatter* dateFormat;
dateFormat=[[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d, yyyy hh:mm:ssa"];
NSString * today;
NSString * futureDate;
today=[dateFormat stringFromDate:todaysDate];
NSLog(#"Today: %#", today);
futureDate = [dateFormat stringFromDate:sender.date];
NSLog(#"Future: %#", futureDate);
}

Returning a string

I have just started with Objective-C and have a (probably) very, very basic question/problem.
int testf(int x){
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSString *infstr=[dateFormatter stringFromDate:[[NSDate] dateByAddingTimeInterval:(60*x)];
return infstr;
}
NSString *testString=testf(1);
I currently have the following problem: I don't know how to return a string from a function. I couldn't even figure it out through Google.
The aforementioned Code leads to a warning "Pointer from integer without a cast". I hope anyone does know a solution to this "problem" and is willing to share it with me.
Thanks in advance.
You need to create an NSString object using the int.
NSString *testf(int x) {
return [NSString stringWithFormat:#"%i",x];
}
NSString *testString = testf(1);
Just change your return type from int to an NSString pointer like so:
NSString* testf(int x){
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSString *infstr=[dateFormatter stringFromDate:[NSDate dateByAddingTimeInterval:(60*x)]];
[dateFormatter release];
return infstr;
}
NSString *testString=testf(1);
Also your dateFormatter is leaking, so add the release statement as shown above.

Getting date info into an array in Objective-C Cocoa framework

I have a function that returns an array to hold date info.
- (NSArray*) getTodayArray
{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY"];
NSString *year = [dateFormatter stringFromDate:today];
[dateFormatter setDateFormat:#"MM"];
NSString *month = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSArray *res = [NSArray arrayWithObjects: year, month, nil];
return res;
}
Q1 : Is there any easy way to get all the info (year, month, date, hour, minute ...) in an array not using setDateFormat over and over again?
Q2 : Is there a way so that I can access the content of array using res['year'] or similar? I mean using dictionary?
Q3 : Do I need to release NSArray *res in the caller of this function?
A1: You can do smth like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY|MM"];
NSArray* d_arr = [[dateFormatter stringFromDate: [NSDate date]] componentsSeparatedByString: #"|"];
A2: Use NSDictionary:
[NSDictionary dictionaryWithObjectsAndKeys: [d_arr objectAtIndex: 0], #"year", [d_arr objectAtIndex: 1], #"month", nil]
A3: return value is autoreleased. you don't need to release it.
#prosseek
1 - I dont think you have another choice to get the year, month, date, hour, minute ... from NSDate other than this.(I am not sure about it though.)
2 - you can access the objects in the dictionary in the above format but something more like objective-c style. like this
[dateDictionary obectForKey:#"year"];
but you need to define the dictionary in that format
like this
NSDictionary *dateDictionary = [NSDictionary dictionaryWithObjects:year,min,hr,nil forKeys:#"year", #"min", #"hour", nil];
3 - no you dont need to release or autorelease the NSArray in the above method . but i think you need to retain it in the array that is receiving res array if you want to use it after a while.
Why don't you just use a NSArray of NSDates?
You can probably get all of your desired functionality out of its plethora of functions.
A1: You could dump it all out into a string, but then you'd have to parse the string, which wouldn't be any easier.
A2: You could do that if you used an NSDictionary instead of an NSArray.*
A3: No, it's already autoreleased.
* Why don't you write a category for NSDate instead?
NSDate+Convenience.h
#interface NSDate (Convenience)
- (NSInteger)year;
- (NSInteger)month;
#end
NSDate+Convenience.m
#implementation NSDate (Convenience)
- (NSInteger)year {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY"];
NSString *myYear = [dateFormatter stringFromDate:self];
[dateFormatter release];
return myYear;
}
- (NSInteger)month {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM"];
NSString *myMonth = [dateFormatter stringFromDate:self];
[dateFormatter release];
return myMonth;
}
#end
Just #include NSDate+Convenience.h wherever you want to use your handy date and month accessors. All of your NSDate instances will then get them:
NSDate *myDate = [NSDate date];
NSLog(#"%ld %ld", [myDate year], [myDate month]);
No need for loosely-typed NSArrays or NSDictionaries to store this stuff.
(Note you could modify the above code to use a shared NSDateFormatter.)
Q1: Not an array, but you can use -[NSCalendar components:fromDate:] to get an NSDateComponents object. You can use it directly or build an array from it, if that is your preference.
Q2: No, but if you return an NSDateComponents object, then you can use -year, -month, etc methods on it.
Q3: No, you don't need to release it in this method or the caller, unless the caller retains it (which may be desirable).
You're looking for the NSDateComponents class. You'll need to create an NSCalendar object first, then call the components:fromDate: method to get the DateComponents object, after which you can access the object's month, year etc. properties.
Not quite sure what you want here. As it stands, the array cannot be accessed in the manner you describe, though if you want you could always create a dictionary and assign values for keys such as 'month' or 'year'. However, it might just be easier to return the DateComponents object, and access its properties.
No, there is no need to release the NSArray. You constructed it using the NSArray class method, which is already autoreleased.