Concatenation of two arrays in Objective-C

前端 未结 3 1472
我在风中等你
我在风中等你 2021-02-06 21:42

How to concatenate two arrays into a single array in Objective-C?

相关标签:
3条回答
  • 2021-02-06 22:00
    NSArray* newArray = [firstArray arrayByAddingObjectsFromArray:secondArray];
    

    Or using mutable array version just add to it all objects from another array:

    [myMutableArray addObjectsFromArray:secondArray];
    
    0 讨论(0)
  • 2021-02-06 22:01
    NSSArray *theArrayIReallyWant = [oneArrayIDontReallyWant arrayByAddingObjectsFromArray:otherArrayIDontReallyWant];
    

    If you need to, retain theArrayIReallyWant so it stays in memory, just be sure to release it when you are done. That by far is the easiest method :)

    0 讨论(0)
  • 2021-02-06 22:15

    With immutable arrays:

    NSArray *arr1 = @[@(1), @(2), @(3)];
    NSArray *arr2 = @[@(4), @(5), @(6)];
    NSArray *arr3 = [arr1 arrayByAddingObjectsFromArray:arr2];
    

    or adding onto a mutable array:

    NSArray *arr1 = @[@(1), @(2), @(3)];
    NSArray *arr2 = @[@(4), @(5), @(6)];
    NSMutableArray *arr3 = [NSMutableArray arrayWithArray:arr1];
    
    [arr3 addObjectsFromArray:arr2];
    
    0 讨论(0)
提交回复
热议问题