How to initialize an empty mutable array in Objective C

后端 未结 8 2186
谎友^
谎友^ 2021-02-12 13:02

I have a list of objects (trucks) with various attributes that populate a tableview. When you tap them they go to an individual truck page. There is an add button which will add

相关标签:
8条回答
  • 2021-02-12 13:30

    In modern Objective - C it could be even more shorter:

    NSArray *array = @[];
    
    0 讨论(0)
  • 2021-02-12 13:34

    listOfTrucks = [NSMutableArray array]; gives you a new mutable array.

    0 讨论(0)
  • 2021-02-12 13:36

    Update:

    With new syntax you can use:

    NSMutableArray *myArray = [NSMutableArray new];
    

    Original answer:

    for example:

    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    

    And here you find out why (difference between class and instance method)

    0 讨论(0)
  • 2021-02-12 13:39

    Basically, there are three options:

    First

    NSMutableArray *myMutableArray = [[NSMutableArray alloc] init];
    

    Second

    NSMutableArray *myMutableArray = [NSMutableArray new];
    

    Third

    NSMutableArray *myMutableArray = [NSMutableArray array];
    
    0 讨论(0)
  • 2021-02-12 13:39
    NSMutableArray *arr = [NSMutableArray array];
    
    0 讨论(0)
  • 2021-02-12 13:41
    NSMutableArray *arr = [NSMutableArray new];
    
    0 讨论(0)
提交回复
热议问题