objective c - Difference between literals and class methods for NSMutableArray and NSMutableDictionary -
this question has answer here:
when started osx/ios used
nsmutablearray * a1 = [nsmutablearray arraywithcapacity:123] ; nsmutabledictionary * d1 = [nsmutabledictionary dictionarywithcapacity:123] ;
then discovered 'simpler' version
nsmutablearray * a2 = [nsmutablearray array] ; nsmutabledictionary * d2 = [nsmutabledictionary dictionary] ;
i have moved to:
nsmutablearray * a3 = [@[] mutablecopy] ; nsmutabledictionary * d3 = [@{} mutablecopy] ;
functionally, seem identical: once initialized either way, can used regardless of how created. differences?
in particular, should assume d3/a3 more similar d2/a2 d1/a1 in terms of memory pre-allocation (or lack thereof) ?
or matter of style?
form 1 class factory method optimization useful when know how large collection needs or know size constant content may change.
form 2 class factory method equivalent calling new. may or may not same alloc followed init, same.
form 3 implicitly same alloc followed initwitharray: or initwithdictionary: respectively. it's convenient generates unneeded immutable instance discarded under arc may not clear when discarded.
use form 1 or form 2 if not going ever use immutable instance again elsewhere.
Comments
Post a Comment