Question: NSMutableDictionary* dictionary = @{}.mutableCopy VS NSMutableDictionary * dictionary = [NSMutableDictonary new]

Question

NSMutableDictionary* dictionary = @{}.mutableCopy VS NSMutableDictionary * dictionary = [NSMutableDictonary new]

Answers 1
Added at 2016-12-28 06:12
Tags
Question

I just want to know the difference between these two, I have searched but could not found anything on this, any body here can help please ?

NSMutableDictionary* dictionary = @{}.mutableCopy 

VS

NSMutableDictionary * dictionary = [NSMutableDictonary new]
Answers
nr: #1 dodano: 2016-12-28 08:12

The first one creates a NSDictionary instance and send mutableCopy method to it, yield a NSMutableDictionary instance.

The second one creates a NSMutableDictionary instance directly. I prefer the second one by the way :)

As @Itai Ferber mentioned:

@{} doesn't construct a new NSDictionary instance, it reuses a singleton.

This is implementation detail but it's right, you can test using the code below:

NSLog(@"%p, %p", @{}, @{});

It will print the same address.

Source Show
◀ Wstecz