Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "LValue" not mean what I think it means?

Tags:

In the following code:

_imageView.hasHorizontalScroller = YES;
_imageView.hasVerticalScroller = YES;
_imageView.autohidesScrollers = YES;

NSLog(@"scrollbar? H %p V %p hide %p", 
      &(_imageView.hasHorizontalScroller), 
      &(_imageView.hasVerticalScroller),
      &(_imageView.autohidesScrollers));

I'm getting the error:

Controller.m:143: error: lvalue required as unary '&' operand
Controller.m:144: error: lvalue required as unary '&' operand
Controller.m:145: error: lvalue required as unary '&' operand

Notice that I am USING those variables as lvalues directly before the & lines...

How can it complain that a value isn't an lvalue right after I assign to it with no error? does this have to do with the magical getters/setters that objective C creates?

I think I need to explain some context to explain WHY I'm trying to get the address:

in my previous SO post, I showed the same code, printing %d and finding that after the assignments, the properties were still 0 for some reason. So, I figured I'd try to get the addresses of the properties to see where they're being stored and maybe I can figure out why I'm not successfully assigning to them, and then this happened.

I think that as people have mentioned, yeah, it's probably that when I do the assigment obj-c is secretly replacing that with a call to the setter (and then some other magic because in another SO post, someone else mentioned that

BOOL b = [_imageView setHasVerticleScroller: YES]

fails, but

BOOL b = _imageView.hasVerticalScroller = YES;

works fine.

like image 350
Brian Postow Avatar asked Jan 14 '10 16:01

Brian Postow


1 Answers

I'm not 100% sure, but I'm going to take a stab at the answer.

Those properties are all BOOL types, which is (I believe) an unsigned char in Objective-C. (Maybe an int, I can't remember, but it's something like that.) So you're trying to take the address of (&) those properties. But you're not actually accessing the ivars directly; properties go through a method call to get their values. So you're trying to get the address of the BOOL return value of a method, but since you're not actually assigning the value to anything, there is no address -- you just have the value.

You'd have the same problem if you did this:

static int returnOne(void)
{
    return 1;
}

// Later...

NSLog(@"returnOne is %p", &returnOne());    // Oops, the return value of returnOne has no address!

Your log call should look like this:

NSLog(@"scrollbar? H %d V %d hide %d", 
  _imageView.hasHorizontalScroller, 
  _imageView.hasVerticalScroller,
  _imageView.autohidesScrollers);
like image 139
mipadi Avatar answered Oct 11 '22 14:10

mipadi