PAcan
PAcan

Reputation: 869

Objective-c expression explanation

What do these lines mean?

   for(ContactInfo *item in fullContactsArray) {
        if(item->contactId || item->listId)
            [contactsArray addObject:item];
    }

Especially, I want to know what -> means. Please, help.

Upvotes: 0

Views: 96

Answers (2)

Caleb
Caleb

Reputation: 125037

The -> operator works the same way as in C or C++... item->contactId is the same as (*item).contactId. (The dot in this case is the member access operator, not the property access operator.)

  • The for loop is an example of fast iteration through an Objective-C container. It simply examines every object in the array fullContactsArray, assigning each object in turn to the loop variable item.

  • item is a pointer to an object of type ContactInfo, and each time through the loop it points to a different object. The if statement uses the -> operator to check the value of the contactId and listId instance variables.

  • If either of those variables is non-zero, the object that item points to is added to contactsArray.

It's a little unusual to see -> in Objective-C code, especially these days, because properties and property accessors are often preferred to direct instance variable access.

Upvotes: 2

Arun C
Arun C

Reputation: 9035

When working with pointers to structure-based data types a special dereferencing syntax allows you to deference the pointer and access a specific field within the structure in a single step. To do this we use the -> operator, as demonstrated below:

struct box * p = ...; 
p->width = 20; 

The -> operator demonstrated on the second line dereferences the pointer p and then accesses the width field within the structure. While following a pointer to read or alter the value it points at, it is also helpful at times to compare two pointers to check if they point to identical values.

Pointers in Objective-C

Upvotes: 3

Related Questions