Is it correct to use short syntax when initializing NSMutableArray?

前端 未结 3 1574
醉梦人生
醉梦人生 2021-01-21 08:58

Usually when we want to initialize NSMutableArray we use:

NSMutableArray *mArr = [[NSMutableArray alloc] initWithObjects: @\"one\", @\"two\", @\"thr         


        
相关标签:
3条回答
  • 2021-01-21 09:19

    I usually use arrayWithArray.

    NSMutableArray *foo = [NSMutableArray arrayWithArray:@[@"one", @"two", @"three"]];
    
    0 讨论(0)
  • 2021-01-21 09:22

    But I think the second way to be way more readable

    Personally, I find it more confusing, and even more confusing since you are using mutableCopy as if it was a property. Correct, correct, but totally misleading IMHO.

    Why not just take advantage of inheritance?

    NSMutableArray *ma = [NSMutableArray arrayWithObjects:@"foo", @"bar", nil];
    

    Sometimes collections which are mutable by default would be of more use. * sigh *

    0 讨论(0)
  • 2021-01-21 09:32

    Yes, it's ok to initialize your mutable array that way, if you are willing (as you indicated) to pay the performance cost. ARC will clean everything up appropriately. It won't leak.

    0 讨论(0)
提交回复
热议问题