Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Xcode say the following expression is not assignable?

 self.messageView.frame.size.height = self.messageView.frame.size.height + notesHeight;

notesHeight is a CGFloat.

like image 208
jini Avatar asked Nov 28 '22 22:11

jini


2 Answers

You are able to assign the frame, but not the fields of frame

CGRect temp = self.messageView.frame;
temp.size.height = temp.size.height + notesHeight;
self.messageView.frame = temp;
like image 157
Jesse Black Avatar answered Dec 21 '22 20:12

Jesse Black


Dot syntax is just syntax sugar.

In this case, this code:

self.messageView.frame.size.height = 0;

Dropping the dot syntax, which causes confusion in some cases about exactly what's happening, it looks like this:

[[self messageView] frame].size.height = 0;

In C terms, that's like this:

ViewGetFrame(GetMessageView(self)).size.height = 0;

You can't set a field of a function result structure this way. (And even if you could, it wouldn't affect the original but only a copy.)

Note that Apple does handle some other things that don't seem like they should work based on these rules automatically (though they didn't when dot syntax was first introduced).

Assuming foo is a NSInteger property, for instance:

self.foo++;

The Wikipedia article on Value has a section on assignment that might clarify the strut problem:

  • Value (computer science), Assignment: l-values and r-values

I'm not aware of any place where Apple defines things that work anyway…

like image 24
Steven Fisher Avatar answered Dec 21 '22 21:12

Steven Fisher