Ever time I try to make a string in xcode it won't work can someone point out what's wrong and help please
NSString *date = self.DatePicker.date;
This is the error "Incompatible pointer types initializing 'NSString * strong' with an expression of type 'NSDate *'"
self.DatePicker.date; returns date.
You are storing it in NSString.
Use:
NSDate *date = self.DatePicker.date;
Then if you want to store it in string then use:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
Related
I am storing an NSDate in a plist as a string, and at launch I am trying to convert the string from the plist back to an NSDate to compare it later.
This is how I am storing the value in my plist:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
In the log (When I convert the [NSDate date] to a proper string) it says this:
2013-04-13 22:47:57 +0000
This is how I am trying to convert the plist value back to an NSDate:
NSString *Checkdate= [InfoDic objectForKey:#"LastDate"];
NSDateFormatter *DateFormat=[[NSDateFormatter alloc]init];
[DateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *theday=[DateFormat dateFromString:Checkdate];
This is the error log on my iPhone 4S:
<Error>: -[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0'
*** First throw call stack:
(0x31e5c2a3 0x39b7997f 0x31e5fe07 0x31e5e531 0x31db5f68 0x327c213d 0x327c208d 0x327c237b 0x23fe1 0x33c83595 0x33cc3d79 0x33cbfaed 0x33d011e9 0x2385d 0x33cc4ad9 0x33cc4663 0x33cbc84b 0x33c64c39 0x33c646cd 0x33c6411b 0x3597a5a3 0x3597a1d3 0x31e31173 0x31e31117 0x31e2ff99
0x31da2ebd 0x31da2d49 0x33cbb485 0x33cb8301 0x235a1 0x23528)
Please note that when I install the app on my device it takes the first date and stores it in the plist. When I close the app and rerun it, it gives me the SIGABRT.
What should I do about this?
You are not storing a date as a string in the plist, you are storing it as a date.
The line:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
stores the actual NSDate object.
All you need to get it back out is to call:
NSDate *theDay = InfoDic[#"LastDate"];
BTW - the line:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
should be:
[InfoDic setObject:[NSDate date] forKey:#"LastDate"];
or just:
InfoDic[#"LastDate"] = [NSDate date];
You are overcomplicating things. What makes you think that storing a NSDate object you'll get back a NSString?
Just do
NSDate * checkDate = [InfoDic objectForKey:#"LastDate"];
Also, don't confuse KVC methods with NSDictionary methods.
You want to use setObject:forKey: instead of setValue:forKey if you don't want to face bad surprises.
Im making an iOS app to do with currency. My app receives the value of maybe: $4. This value the app receives is put into an NSNumber. The trouble is the value actualy has a $ in it. How do I trim out the $ in the NSNumber? Or would I be better of putting it into an NSString?
Use NSNumberFormatter:
// set up your number formatter
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
// get a string that you'll be converting to a NSNumber
NSString *myNumberString = [NSString stringWithFormat:#"$4"]
// convert then print to the console
NSNumber *myNumber = [numberFormatter numberFromString:myNumberString];
NSLog(#"myNumber: %#", myNumber);
This should accomplish what you're looking to do. myNumberString will need to be altered to contain whatever string you're receiving.
NSNumberFormatter Documentation: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html
I'm working on an iOS project, retrieving data from SQLite through JSON. The table has a datetime column called 'date'. When I retrieve the date in Xcode and printout the result, I see "2012-09-02T16:30:00Z". But when I try to convert it using NSDateFormatter, the result is null.
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMM" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *dateString = [dateFormatter stringFromDate:categoryAtIndex.date];
NSLog(#"%# => %#", categoryAtIndex.date, dateString);
The above outputs:
2012-09-02T16:30:00Z => (null)
in stead of a nicely formatted datestring.
Any thoughts?
Thanks to mask8 I found the issue was related to another part of my code. I found this post on stackoverflow to solve the way to change the format of the date that I retrieved through JSON. I also found another post on stackoverflow describing how to handle conversions from a string to NSDate and vice versa.
I was wondering how to convert latitude and longitude values of CLLocationCoordinate2D to numbers or string values.
Iver tried a few different ways but they arene't working:
CLLocationCoordinate2D centerCoord;
centerCoord.latitude = self.locModel.userLocation.coordinate.latitude ;
centerCoord.longitude = self.locModel.userLocation.coordinate.longitude;
NSString *tmpLat = [[NSString alloc] initWithFormat:#"%g", centerCoord.latitude];
NSString *tmpLong = [[NSString alloc] initWithFormat:#"%g", centerCoord.longitude];
NSLog("User's latitude is: %#", tmpLat);
NSLog("User's longitude is: %#", tmpLong);
This returns a warning by the compiler.
The warning is
warning: passing argument 1 of 'NSLog' from incompatible pointer type
How do I do this?
Any help would be appreciated.
thanks
You haven't mentioned what the warning is but it's most likely because you forgot the # in front of the NSLog strings:
NSLog(#"User's latitude is: %f", self.locModel.userLocation.coordinate.latitude );
NSLog(#"User's longitude is: %f", self.locModel.userLocation.coordinate.longitude );
Your updated code should be:
NSLog(#"User's latitude is: %#", tmpLat);
NSLog(#"User's longitude is: %#", tmpLong);
NSLog expects an NSString parameter which needs an # sign in front. Without the # sign, the string is a plain C string not an NSString object.
I am using a UIDatepicker with only "Time", I save the time in a NSDate object type, however when I try to store the Object in core data I get an error saying its not a NSDate type...
tt = [pickerTime date];
[myObject setValue:tt forKey:#"time"];
Thanks,
Hey, a see a mistake here:
tt = [pickerTime date];
tt's type is either an NSDate, or... You have to declare what tt is:
NSDate *tt = [pickerTime date];
Make sure that your using a date field in IB.