Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is legitimate way to typedef NSDictionary in Objective-C?

I am trying to use Objective-C API in Swift and I have to typecast Swift Dictionary to NSSDictionary but if some way I can declare the NSDictionary in below written format, I can skim out redundant typecasting.

typedef NSDictionary* NSDictionary<NSString *, id> *;

In my Objective-C API there are several dictionaries and I want all them converted into above typdef.

like image 396
Rahul Avatar asked Nov 04 '15 12:11

Rahul


1 Answers

You are trying to redefine all NSDictionary as NSDictionary<NSString *, id>. That's a big no-no. Instead, create your own type:

typedef NSDictionary<NSString *, id> MyDictionary;

// Usage
MyDictionary * dict = [MyDictionary dictionaryWithObjectsAndKeys:@1,@"one", @2,@"two", @3,@"three", nil];

Swift:

typealias MyDictionary = [String: Any]

let aDict: MyDictionary = [
    "one": 1, "two": 2, "three": 3
]

func doSomething(aDict: MyDictionary) {
    // ...
}
like image 161
Code Different Avatar answered Nov 17 '22 13:11

Code Different