Rahul
Rahul

Reputation: 2100

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.

Upvotes: 6

Views: 2640

Answers (1)

Code Different
Code Different

Reputation: 93161

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) {
    // ...
}

Upvotes: 12

Related Questions