Bad access code when allocating a class containing a CGPoint and initializing it

StackOverflow https://stackoverflow.com/questions/20378698

  •  29-08-2022
  •  | 
  •  

문제

I have been trying to figure out what is the problem with the code I've written and I have not the slightest clue why is it wrong. Another thing I noticed was that Xcode is treating my CGPoint as a pointer, preventing me from using arrow notation. I need it to be a property for the purposes of my program in .h file

@property (nonatomic) CGPoint* directionUsed;

in controller file

// up is just an instance of the class direction.
// Direction is a class that returns itself

self.up =    [[Direction alloc]initWithX:0 y: -1]; 

designated initializer in .m file:

-(id)initWithX:(int)x y:(int)y{

self = [super init];

if(self){
    self.directionUsed->x = x; //not letting me use dot notation
    self.directionUsed->y = y;
}
return self;

}

Thanks for the help!

도움이 되었습니까?

해결책

The problem is that you defined your CGPoint as pointer

just change your code

from

@property (nonatomic) CGPoint* directionUsed;

to

@property (nonatomic) CGPoint directionUsed;

Edit

In order to assign a value to directionUsed you need to allocate the struct first You need to change your code

from

self.directionUsed->x = x;
self.directionUsed->y = y;

to

CGPoint point;
point.x = x;
point.y = y;
self.directionUsed = point;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top