我有一个项目仍然有很多Objective-c代码,比如这个:
+ (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];
}
这对我的Swift 2.2项目(该项目同时包含Swift和Objective-c)工作正常,并且总是返回一个值。现在,我已将我的代码迁移到Swift 3.0,由于某种原因,上面的方法返回一个可选值。如何使上面的代码再次返回非可选值?