-3

I want to incorporate a .rtfd (...or html) file into a NSAttributedString.

I've created some class helper methods within a NSAttributedString(category) written in Objective-C.

My Question: how do I access these Objective-C methods from Swift?

The following is how I would do it in Objective-C:

NSURL *rtfURL = [[NSBundle mainBundle]  URLForResource: @"Recursion" withExtension:@"rtfd"];
NSAttributedString *attrString = [NSAttributedString attributeStringFromRTFURL:rtfURL];

Here's the Objective-C class method:

+ (NSAttributedString *)attributeStringFromRTFURL:(NSURL *)rtfURL {
NSAttributedString *stringWithRTFAttributes =
   [[NSAttributedString alloc] initWithFileURL:rtfURL
   options:@{NSDocumentTypeDocumentAttribute:NSRTFDTextDocumentType}
   documentAttributes:nil
   error:nil];
   return stringWithRTFAttributes;
}

Here's what I have in the .swift file:

let rtfURL = NSBundle.mainBundle().URLForResource("Recursion", withExtension: "rtfd")
let attrString = [NSAttributedString attributeStringFromRTFURL:rtfURL];

Question: What's the Swift equivalent of [NSAttributedString attributeStringFromRTFURL:rtfURL];?

Note: I'm seeking the syntax of calling a class vs instantiated method.

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • I included the bridge header file which has the #import "NSAttributedString+Extra.h". The examples are about accessing an Obj-C INSTANTIATED vs CLASS method. But I'm still having trouble converting the Obj-C NSAttributedString CLASS method access to its Swift equivalent per Question stated above. – Frederick C. Lee Aug 04 '14 at 04:21

2 Answers2

0

I believe I've been too subjective. With a bit of fresh air...
There are two (2) possible solutions:
1) Use the Swift extension for NSAttributedString rather than focus on the current Objective-C category {NSAttributedString+Extra}. Or...
2) Follow the other examples and access an instance vs class of NSAttributedString via Swift syntax.

I'm currently following #2. <-- I got it working!

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
0

I'm seeking the syntax of calling a class vs instantiated method

The syntax for calling a class method is the same as that for an instance method. The only difference is that you use the class name instead of an instance of the class as the recipient of the message:

var foo = NSString.string()  // same as Obj-C: NSString *foo = [NSString string]
Caleb
  • 124,013
  • 19
  • 183
  • 272