0

How can I cast an Object to Person to access its property something like

((Person)self.dataObject).firstName;

except that clang doesn't accept this syntax :

DataViewController.h

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.dataLabel.text = ((Person)self.dataObject).firstName;
}

DataViewController.h

#import <UIKit/UIKit.h>

@interface DataViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *dataLabel;
@property (strong, nonatomic) id dataObject;

@end

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property NSString* firstName;
@property NSString* lastName;

@end

1 Answer 1

3

You're not casting it as a pointer to an object. Instead you should be casting it as follows:

((Person *)self.dataObject).firstName;
Sign up to request clarification or add additional context in comments.

1 Comment

The lack of the asterisk does not make a type "primitive". Pointers, in fact, are primitive types themselves, but objects, which are not, are only accessed via pointers in ObjC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.