ghostrider
ghostrider

Reputation: 5259

accessing classes from another class in objective c

I have two classes.

GameData.h

#import "TeamData.h"

@property (assign, nonatomic) GameData* teamA;

TeamData.h

@interface TeamData : NSObject

@property (nonatomic, copy) NSString* teamName;     
-(void) printTeamData;

A number of questions :

  1. Inside GameData.m I have this code :

    TeamData* team = self.teamA; [team printTeamData];

The first line shows this warning :

Incompatible pointer types from TeamData* with an expression of type TeamData*
  1. In another class, I am including GameData.h and I want to set the teamA name. How do I access that? So I want to fetch the teamA property from the GameData class and set its name property.

Upvotes: 0

Views: 47

Answers (1)

user4151918
user4151918

Reputation:

In GameData.h, your property points to its own class, not to TeamData

@property (assign, nonatomic) GameData* teamA;

assign is meant for primitive types such as BOOL or NSInteger.

The parent class should hold a strong reference to a child object.

So your property would be better off as

@property (strong, nonatomic) TeamData* teamA;

As for setting the teamA property, you would call setTeamA: on your GameData instance:

[myGameData setTeamA:...];

Upvotes: 1

Related Questions