NSDate not an Objective-C object? - ios

This is one of those questions that is so absurdly simple that I can't believe I'm asking it on here. But, I'm stumped, so here I go: I'm trying to create an NSDate for a certain time in the future. I thought it was easy, but when I try:
NSDate *destinationDate = [NSDate dateWithTimeIntervalSinceNow:30];
a breakpoint at the following line says that destinationDate is "not an Objective-C object." I tried a million different versions, from this:
NSDate *destinationDate = [[NSDate alloc] init];
destinationDate = [NSDate dateWithTimeIntervalSinceNow:30];
to this:
NSDate *destinationDate = [NSDate date];
to this (suggested here):
NSDate *destinationDate = [[NSDate date] copy];
And nothing works! They're all "not Objective-C objects"! I'm sure it's something simple and embarrassing that I'm missing, but I am completely at a loss. Can someone help me out?

Well, you can see in the documentation here that NSDate is indeed a NSObject, which ofcourse is a Objective-C object.
This is probably a bug of the debugger panel, unfortunately it happens a lot. When this weird things happens, always use the console, when in a breakpoint, with a po myVariable to check the content of your variable, it's more reliable.

NSDate is a subclass of NSObject as per apple's documentation.
The methods you listed (alloc/init, and class method dateWithTimeIntervalSinceNow:) are valid uses.
It's hard to say more about it without knowing what your actual issue is, but if you are not using ARC, you might check that you retain/release correctly.

Are you using the variable after the breakpoint as well? Otherwise the object might already be released by the time you hit the breakpoint, because the app doesn't need it anymore. Then you'll get a "not an object" message.

Is Foundation not included in the project?
#include <Foundation/foundation.h>

Related

Strange Xcode behaviour

I am about to raise this as a bug with apple support but it takes a long time to get any response from them so ... I wanted to ask here.
Debugging an app where i use some dateTime conversions between timezones.
I get the following strange behavior and I wonder if anyone else has seen this and knows how to fix it.
Xcode 8.3.2
You can see that the GMTDate is displaying 2 completely different date/time values ( they aren't even related values) and it is difficult to know which one is correct.
I have restarted Xcode /computer, etc, but still the same!
Screenshot attached:
- (NSString*) convertToDeviceTime:(NSString*) GMTDate {
//create the formatter for parsing
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//parsing the string and converting it to NSDate
NSDate *myGMTDate = [df dateFromString: GMTDate];
//create the formatter for the output
NSDateFormatter *out_df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[out_df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//output the date
NSString *myDeviceDate =[out_df stringFromDate:myGMTDate];
NSLog(#"the date is %#",myDeviceDate);
return myDeviceDate;
}
new screenshot below - this has to be something wrong with Xcode,
the real value of 'token' shows in the quick look, but the debugger window shows it as a date! Weird. I have restarted the Mac, still the same.
Update: It is happening with all NSString variables. As soon as I write a value to them, the debugger window shows them with the same date value. If I print them to the console or NSLog, then the values are shown correctly.
Found the culprit.
In the debugger the "Edit Summary Format" popup I had somehow inadvertently pasted this date value, and it was showing this for all NSString values.
FYI:It is NOT project specific, so when I loaded another project, it kept that "Edit Summary Format" string and applied it to all NSStrings.
Kudos to an older SO post here: Strange values displayed in Xcode debugger
Screenshots attached!

iOS NSDateFormatter behaving differently on device

I have the following code to create NSDate objects from NSString objects.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm aa M/dd/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *day = #"3/26/2015";
NSString *time = #"10:24 PM";
NSString *dateString = [NSString stringWithFormat:#"%# %#", time, day];
NSDate *date = [dateFormatter dateFromString:dateString];
This piece of code works perfectly on the simulator, yielding the exact corresponding time in my time zone. However when I run this on a device with iOS 8, date is set to nil.
The format I'm using is supposed to work according to this page that is referenced by this Apple Developer page.
Any help or information on this would be much appreciated. Thanks in advance.
Edit: I'm trying to create NSDate objects from formatted NSString's, not get the date from the system in a predefined fromat. The possible duplicate of this question is probably closely related but I couldn't get the solution to work in my situation.
Edit 2: I've just noticed this problem only occurs when 24-Hour Time is enabled in Settings. However, there is no way for me to know which format the device owner is using, so I'm still in need of a solution.
When working with such strict date formats, you need to set the locale to avoid having issues with the device current locale when formatting dates. Otherwise, the NSDateFormatter will use the device's locale, which explains the fact that it happens only when you enable 24-Hour Time in Settings.
See Apple's documentation:
In all cases, you should consider that formatters default to using the user’s locale (currentLocale) superimposed with the user’s preference settings. If you want to use the user’s locale but without their individual settings, you can get the locale id from the current user locale (localeIdentifier) and make a new "standard” locale with that, then set the standard locale as the formatter's locale.
For example, in your case, you could use the en_US_POSIX:
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.locale = enUSPOSIXLocale;

Why can't I get this stringFromDate code to work?

The following code is supposed to format the datestamp from coredata entity "DIncome"
But I'm getting the following error, which I'm pretty sure has something to do with this code.
-"Incompatible pointer types initializing 'NSString *' with an expression of type 'NSDate *'"-
DIncome *dIncome = [dailyIncomeArray objectAtIndex:indexPath.row];
NSDateFormatter *formateDateString = [NSDateFormatter alloc];
[formateDateString setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *formattedDateString = [formateDateString stringFromDate:dIncome.datestamp];
NSLog(#"%#" "This is your date log", formattedDateString);
Thanks a bunch for your time and help!
One simple way to fix this issue is to change the type for your attribute datestamp from "date" to "string, You will need to format the date how you want it to display before you send it to your data model. Of course this doesn't explain why the datestamp was having an issue when you were pulling it back out of the database. It is in he same formate as when it went in.

Accessing the browsing history of iphone using webkit private framework

Hi I request please read the question completely before marking it as duplicate.
I am trying to get the iphone browsing history by using Webkit private framework.I get the headers for it from the github site. But I am not getting which headers or which methods to use to accomplish my task. I tried with the following code but its not returning anything not even null.
WebHistory *history=[WebHistory optionalSharedHistory];
NSDate *now = [NSDate date];
//id date;
NSArray *arr = [history orderedItemsLastVisitedOnDay:now];
NSLog(#"%#",[history allItems]);
I am writing in house app so i don't mind with this private framework. But i just can't go for jailbreaking. Please guide me the right way.
In order for the optionalSharedHistory method to return anything but null it must be instantiated and set like so in a place that's convenient in your application. Like a root view controller or the AppDelegate.
// Create a shared WebHistory object
WebHistory *myHistory = [[WebHistory alloc] init];
[WebHistory setOptionalSharedHistory:myHistory];
Hope this helps!

'NSDate' may not respond to +dateWithString

NSDate *future = [NSDate dateWithString:#"2010-12-25 00:00:00 -0600"];
I'm getting a warning message: NSDate may not respond to +dateWithString
Any ideas?
NSDate dateFromString is MacOS only, not iOS.
maybe you mean the method in NSDateFormatter?

Resources