Swift 3 getting an optional value from objective-c class? - objective-c

I have a project that still has a lot of objective-c code, like this one:
+ (NSString *)getDaysFromDateString:(NSString *)dateString
{
// Creating and configuring date formatter instance
NSLocale *ptBRLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"pt_BR"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
[dateFormatter setLocale:ptBRLocale];
// Retrieve NSDate instance from stringified date presentation
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
// Retrieve date with increased days count
NSDate *newDate = [dateFromString dateByAddingTimeInterval:-30*24*60*60];
return [dateFormatter stringFromDate:newDate];
}
This was working fine with my Swift 2.2 project (this project has both Swift and Objective-c) and always returned a value. Now, I have migrated my code to Swift 3.0 and for some reason the method above returns an optional value. How can I make the code above return a non-optional value again?

Related

Why does this function return the same value regardless of input?

I have a method that formats a date string. I've encountered an issue where it returns only the formatted version of the first NSString I pass to it..
Code:
self.lastUpdatedLabel.text = [self convertTime:lastupdated];
self.expiryDate.text = [self convertTime:expiryDate];
Method
- (NSString *)convertTime:(NSString *)date{
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"HH:mm - dd/MM/yyyy"];
[timeFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
date = [timeFormatter stringFromDate:[[NSDate alloc]init] ];
return date;
}
Both lastUpdatedLabel and expiry date are set to the value of expiryDate. Surely the method runs every time it is called, producing a different output as the input is different
It looks like you need to update your code to use your argument, date.
As people have mentioned in comments, you are using the current time, [[NSDate alloc] init] instead of the date that was passed in.
If lastUpdated and expiryDate are NSDates, then this will work.
- (NSString *)convertTime:(NSDate *)date{
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"HH:mm - dd/MM/yyyy"];
[timeFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
return [timeFormatter stringFromDate:date];
}
If lastUpdated and expiryDate are NSStrings, which is what it looks like you have, then you need to convert your NSStrings to NSDates, using an NSDateFormatter that understands the format of the NSString that you have ... and then pass that NSDate to the code above to create a properly formatted string as output.

NSDateFormatter dateFromString:stringDate returning nil in iOS 8.3

I have this code
NSString *stringDate = #"2015-07-09 7:00 AM";
NSString *stringDateFormat = #"yyyy-MM-dd h:mm a";
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:stringDateFormat];
NSDate *dateFormatted = [dateFormatter dateFromString:stringDate];
The problem now is dateFormatted keeps getting nil value. It is OK when I try it in Simulator iPhone5s 8.2 but, the problem occurs when I run it with device iPhone5s 8.3.
Can someone help me find why is dateFormatted is getting nil value?
It's almost a canned response but: QA1480. Set your formatter's locale to en_US_POSIX or its behaviour will be dependent upon the user's settings.

iOS create NSDate from output of Python datetime.isoformat()?

My python backend uses the isoformat() method on UTC date times, which results in strings that look like 2014-01-14T18:07:09.037000. Following other examples, I'm trying to create NSDates from those strings (passed up in JSON packets):
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:S"];
NSLog(#"cycle_base %#", myFields[#"cycle_base"]);
self.cycleBase = [dateFormatter dateFromString: myFields[#"cycle_base"]];
NSLog(#"cycleBase %#", self.cycleBase);
I've tried variants on the S part (which is supposed to be fractional seconds?) of the format string, but to no avail. I always get a nil back. What am I doing wrong?
iOS 7 follows the Unicode Technical Standard #35, which is a list of format patterns.
In this document you will find that the format string for fractional seconds is capitalized S.
NSString *string = #"2014-01-14T18:07:09.037000";
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.S";
NSDate *date = [formatter dateFromString:string];
NSLog(#"%#", date);
This will net you a valid NSDate object. Don't forget to set the proper time zone and locale on your NSDateFormatter object.

Include Framework to get date information Objective-C

I am new to objective-C and I am trying to insert NSdate but I don't know which library to include in order to call it. I tried the following but without success. Thanks for your help
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#include <CoreFoundation/CoreFoundation.h>
// Get current datetime
NSDate *currentDateTime = [NSDate date];
// Instantiate a NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Set the dateFormatter format
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
// Get the date time in NSString
NSString *dateInString = [dateFormatter stringFromDate:currentDateTime];
// Release the dateFormatter
[dateFormatter release];
You have included every necessary header and you added every necessary framework (all you need for NS* classes is Foundation). The problem is that your statements don't have a scope. Wrap them, for example, in the main() function.

How can I Convert a NSString to a valid NSDate (iOS)

I'm passing a string from javascript to Objective-C in the form of "2012-02-17 14:21:30 +0000".
My code is as follows:
NSString *firingDate = [_parameters objectForKey:#"fire"];
NSDate *notificationDate = [NSDate dateWithString:firingDate];
The issue is that I ended up reading the OS X reference instead of the iOS docs (doh!) so this throws a warning as dateWithString isn't present in iOS. In theory I suppose that this shouldn't work at all but it does, albeit with that warning.
What is the Correct way to convert the string to a NSDate?
The correct way is to use NSDateFormatter as a factory to create dates from strings (and vice versa).
Try:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"YYYY-MM-dd HH:mm:ss Z";
NSDate *notificationDate = [formatter dateFromString:firingDate];
Try this:
NSString *firingDate = [_parameters objectForKey:#"fire"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *notificationDate = [dateFormatter dateFromString:firingDate];
Check out the reference for parsing dates from multiple regions.
Don't forget to release your formatter when finished.