-3

Please help me, I am trying to convert UITextField to NSDate. Where is the mistake?

.h

@property (weak, nonatomic)   UITextField   *finishDateField;
@property (strong,nonatomic)  NSDate        *endDate;

.m

-(void) getDate{

    SaveAndLoadClass* Loader = [[SaveAndLoadClass alloc] init];
    [Loader loadSettings];
    self.finishDateField = [Loader dateField];
    [self firstCalculate];
}
-(void)firstCalculate{
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd MMMM yyyy"];
        self.endDate = [dateFormat dateFromString: self.finishDateField.text];
        NSLog(@"datefromField:%@",[dateFormat stringFromDate: self.endDate]);
}

-[__NSCFString text]: unrecognized selector sent to instance 0x61000002c620 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString text]: unrecognized selector sent to instance 0x61000002c620'

sLav9n
  • 1
  • 4
  • `finishDateField` not an IBOutlet, then how you are initialising it ? – Midhun MP Apr 04 '17 at 09:25
  • I got it from another class; `self.finishDateField = [Loader dateField];` The Loader the object of another class – sLav9n Apr 04 '17 at 09:32
  • if `[Loader dateField]` returning a string, then the above is incorrect please use `[currentlySelectedTextField setText:[Loader dateField]];` this line to set text. It may remove your build errors. – arunjos007 Apr 04 '17 at 09:41
  • So `[Loader dateField];` returns an UITextField ? – Midhun MP Apr 04 '17 at 09:41
  • Possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](https://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Cœur Jul 08 '19 at 05:53

3 Answers3

1

It's simple... You forgot to add .text after self.finishDateField

self.endDate = [dateFormat dateFromString:self.finishDateField.text];//error here
Pratik Mistry
  • 2,905
  • 1
  • 22
  • 35
0

dateFormat dateFromString:self.finishDateField is the textfiled itself. You have to assign the content of the field:

self.endDate = [dateFormat dateFromString:self.finishDateField.text]
Jens
  • 67,715
  • 15
  • 98
  • 113
0

You are passing your textField instead of string, your code should be like,

 self.endDate = [dateFormat dateFromString:self.finishDateField.text];

And second thing your string that you enter in your textfield is in dd MMMM yyyy format as your date formatter is like this, otherwise you will not get date!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75