Accessing Swift Extension From Objective-C

ios, objective-c, swift, swift-extensions

Solution

It is easy enough using simply a Dictionary

 20> extension Dictionary {
 21.     func toJSONString () -> String { return "my dictionary" }
 22. }
 23> ["a": 1, "b": 2].toJSONString()
$R10: String = "my dictionary"

The Apple documentation does not mention using extensions on Objective-C classes.

Problem

I'm having trouble accessing my swift extension from objective-c. I have the following code in a .swift file: ``` extension NSDictionary { func dictionaryAsJsonString() -> NSString { var err: NSError? var data = NSJSONSerialization.dataWithJSONObject(self, options: NSJSONWritingOptions.PrettyPrinted, error: &err) var string = NSString(data: data, encoding: NSUTF8StringEncoding) return string } } ``` I'm expecting to be able to do the following in my .m file: ``` [dictionary dictionaryAsJsonString]; ``` but it can't find my method and doesn't autocomplete. I know my imports are working fine because I'm able to access my other swift objects.

Original source