1

I had many Class Extensions written before, like :

My header file, NSDate+convenience.h:

@interface NSDate (Convenience)
+ (NSDate *)dateFromMyString:(NSString *)dateString 
           withDateFormatStr:(NSString *)dateFormatStr;
@end

My Obj-C implementation file, NSDate+convenience.m:

@implementation NSDate (Convenience)
+ (NSDate *)dateFromMyString:(NSString *)dateString
              withDateFormatStr:(NSString *)dateFormatStr {

   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   [formatter setLocale:[NSLocale currentLocale]];
   [formatter setTimeZone:[NSTimeZone localTimeZone]];
   [formatter setDateFormat:dateFormatStr];

   return [formatter dateFromString:dateString];
}
@end

I use my NSDate Class Extension in objective c by :

[NSDate dateFromMyString:[NSDate date] withDateFormatStr:@"yyyyMMdd-HHmmss-SSS"]

But when want to use it in Swift something like :

NSDate.dateFromMyString(dateString: dateStr, DateFormatStr: "yyyyMMdd-HHmmss-SSS")

or

NSDate.dateFromMyString(bla bla bla ...)

Anyway, the NSDate has no member like 'dateFromMyString', I have many many class extension which I wrote before. Do I need to implement these Class Extension in Swift again.

I have to underline that, I can use my custom normal Obj-C classes properties and methods in Swift, but I can't use a obj-C file that is an extension of the Obj-C. So please do not give me a link 'How to call Objective-C code from Swift' . Please understand my question.

Nijat2018
  • 863
  • 2
  • 10
  • 26
  • is this a typo? Shouldn't it be `NSDate.dateFromMyString(dateString: dateStr, withDateFormatStr: "yyyyMMdd-HHmmss-SSS")` – Shamas S Feb 19 '19 at 04:02
  • I say something like that, anyway, it cannot find any member like 'dateFromMyString bla bla bla ...' – Nijat2018 Feb 19 '19 at 04:08
  • Have you any bridge header file on your project? – Faysal Ahmed Feb 19 '19 at 04:27
  • Possible duplicate of [How to call Objective-C code from Swift](https://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – Faysal Ahmed Feb 19 '19 at 04:28
  • I have a bridge header file that I can use all my other custom Obj-C classes properties and methods in Swift. I only cannot access extension of the Obj-C. – Nijat2018 Feb 19 '19 at 04:32
  • 1
    @Nijat2018 I think the point is `NSDate+convenience.h` in the bridging header? – trojanfoe Feb 19 '19 at 08:18
  • 1
    You should call it in Swift like this: `let date = NSDate(fromMyString: "String", withDateFormatStr: "yyyy.MM.dd")`. And, of course, the extension header must be imported in the bridging header – BadCodeDeveloper Feb 19 '19 at 08:23
  • Not working, my Class Extension is inside a framework, and I put the framework header inside bridging header – Nijat2018 Feb 20 '19 at 01:33

0 Answers0