bodacious
bodacious

Reputation: 6705

Can someone please explain this Objective-C syntax?

I'm having trouble making sense of the following:

UILabel *label = (UILabel *)[cell viewWithTag:1000];

I understand that UILabel is a class. So we're creating a pointer named *label that points to an instance of UILabel.

Right of the equals sign, I understand that [cell viewWithTag:1000]; is passing cell a method named viewWithTag with the argument 1000.

What does the (UILabel *) before that mean?

Upvotes: 0

Views: 155

Answers (2)

Peter Kelly
Peter Kelly

Reputation: 14401

It is casting the result to a UILabel. The syntax is the same as in C - (Objective-C is a super-set of C).

You can also check the type before using it (although not really necessary if you are confident of what is being returned) using isKindOfClass

Upvotes: 1

Dan F
Dan F

Reputation: 17732

It means to cast the object type to a UILabel*. By default, viewWithTag: returns a UIView*

Upvotes: 2

Related Questions