Expression is not assignable error with CGRect

590 views Asked by At

I'm trying to create a CGRect property.

@property (nonatomic) CGRect myRect;

Then later on, I set the frame:

self.myRect = self.frame;
self.myRect.size.height = 10; // Error

I get an error saying "Expression is not assignable". Why do I get that? Can't I change the size of a CGRect?

2

There are 2 answers

4
Rajesh Maurya On BEST ANSWER

Do it this way.

_myRect = self.frame;
_myRect.size.height = 10;
0
nielsbot On

self.myRect.size.height = 10 is equivalent to [ self myRect ].size.height = 10

You must read the CGRect property, modify it, then write it back:

CGRect r = [ self myRect ] ;
// modify r here
[ self setMyRect:r ] ;

or:

CGRect r = self.myRect ;
// modify r here
self.myRect = r ;