Search code examples
iosobjective-cnsdateautomatic-ref-countingsemantics

objective c error "No visible @interface for 'NSString' declares the selector 'timeIntervalSinceDate:'"


Sorry for posting about this but I have been wrestling with it for sometime, I have been trying to use the timeIntervalSinceDatebut its not behaving. This is the m file:

#import "ViewController.h"

@interface ViewController()

   @end

   @implementation ViewController //warning: Method definition for 'timeIntervalSinceDate:' not found

-(IBAction) setButtonTapped:(id)sender {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSLog( @"Set button tapped : %@", dateTimeString );

NSDate* currentDate = [NSDate date];
NSTimeInterval secs = [dateTimeString timeIntervalSinceDate:currentDate]; //error: No visible @interface for 'NSString' declares the selector 'timeIntervalSinceDate:’
NSLog(@"Seconds %f", secs);

[self scheduleLocalNotificationWithDate: dateTimePicker.date];

[self presentMessage:@"succesfully set!"];

}

 - (void)didReceiveMemoryWarning
 {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

and this is the h file:

#import <UIKit/UIKit.h>
#import <AvFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
    IBOutlet UIDatePicker *dateTimePicker;
}
- (NSTimeInterval)timeIntervalSinceDate:dateTimePicker;
-(IBAction) setButtonTapped:(id)sender;

@end

Also what would the method be for timeIntervalSinceDate? I couldn't find any info on it and as I'm new to this want sure how to work it out for myself.

Thanks


Solution

  • Your are trying to perform a method of NSDate on an NSString object (dateTimeString). Convert it to NSDate format first and then call the 'timeIntervalSinceDate:' method on the NSDate object.

    Refer this post on how to convert NSString to NSDate.

    Converting NSString to NSDate (and back again)