Access fast expansion from Objective-C

I am having trouble accessing my quick extension with 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 expect I can do the following in my .m file:

 [dictionary dictionaryAsJsonString]; 

but he cannot find my method and is not autocomplete.

I know that my import is working fine because I can access my other fast objects.

+7
ios objective-c swift swift-extensions
source share
2 answers

It's easy just using the dictionary.

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

Apple's documentation does not mention the use of extensions in Objective-C classes.

0
source share

It was probably a mistake or an incomplete implementation in the earlier version that you tried in June. It is great for extending NSDictionary in the latest beta. For example, this very complex extension and usage works as expected with code completion:

 extension NSDictionary { func myFooBar() { println("gaaah") } } // elsewhere... var d = NSDictionary(object: "bar", forKey: "foo") d.myFooBar() 
+1
source share

All Articles