Type Method

arrayWithObjects:

Creates and returns an array containing the objects in the argument list.

Declaration

+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ...;

Parameters

firstObj

The first object for the array.

...

A comma-separated list of additional objects, ending with nil.

Return Value

An array containing the objects in the argument list.

Discussion

The following code example creates an array containing three different types of element:

NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = @(5);
NSString *aString = @"hello";
 
NSArray *array = [NSArray arrayWithObjects:aDate, aValue, aString, nil];

Alternatively, you can use array literal syntax in Objective-C or Swift to create an array containing given objects:

NSArray *array = @[@"alpha", @"bravo", @"charlie"];

See Also

Creating an Array

+ array

Creates and returns an empty array.

+ arrayWithArray:

Creates and returns an array containing the objects in another given array.

+ arrayWithContentsOfFile:

Creates and returns an array containing the contents of the file specified by a given path.

Deprecated
+ arrayWithContentsOfURL:

Creates and returns an array containing the contents specified by a given URL.

Deprecated
+ arrayWithObject:

Creates and returns an array containing a given object.

+ arrayWithObjects:count:

Creates and returns an array that includes a given number of objects from a given C array.