Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between array[index] and [array objectAtIndex:index]?

I noticed that both array[index] and [array objectAtIndex:index] work with mutable arrays. Could someone explain the difference between them? in terms of performance, and which one is the best practice?

like image 621
Hamad AlGhanim Avatar asked Mar 28 '15 15:03

Hamad AlGhanim


People also ask

What is difference between array element and array index?

An array is an indexed collection of data elements of the same type. 1) Indexed means that the array elements are numbered (starting at 0). 2) The restriction of the same type is an important one, because arrays are stored in consecutive memory cells. Every cell must be the same type (and therefore, the same size).

What is the difference between index and element in Python?

An Overview of Indexing in Python As mentioned, lists are a collection of items. Specifically, they are an ordered collection of items and they preserve that set and defined order for the most part. Each element inside a list will have a unique position that identifies it. That position is called the element's index.

What is swift Nsarray?

An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.


1 Answers

None. That's part of the clang extensions for objective-c literals, added 2-3 years ago.

Also:

Array-Style Subscripting

When the subscript operand has an integral type, the expression is rewritten to use one of two different selectors, depending on whether the element is being read or written. When an expression reads an element using an integral index, as in the following example:

NSUInteger idx = ...; id value = object[idx];

It is translated into a call to objectAtIndexedSubscript:

id value = [object objectAtIndexedSubscript:idx]; 

When an expression writes an element using an integral index:

object[idx] = newValue;

it is translated to a call to setObject:atIndexedSubscript:

[object setObject:newValue atIndexedSubscript:idx];

These message sends are then type-checked and performed just like explicit message sends. The method used for objectAtIndexedSubscript: must be declared with an argument of integral type and a return value of some Objective-C object pointer type. The method used for setObject:atIndexedSubscript: must be declared with its first argument having some Objective-C pointer type and its second argument having integral type.

like image 120
uraimo Avatar answered Oct 29 '22 16:10

uraimo