Code:CGRect bounds = label1.bounds; bounds.size = [label1.text sizeWithFont:label1.font]; label1.bounds = bounds;
why can't I write label1.bounds.size = [label1.text sizeWithFont:label1.font]; ?
I get error expression is not not assignable ?
Size is not a property of UIView. Size is a member of CGRect (a C struct), which is a property of the UIView, i.e., it's frame. Actually, size is not at all an Objective C property 😉 So you have to set it's frame.
I'm not exactly sure this is accurate for two reasons: a) he's not asking for view.size, he's asking for view.bounds.size which is exactly what you describe, and b) the problems isn't what you imply, rather it's that bounds.size is not a lvalue.
It's really that latter issue that is the problem. You can't write bounds.size because it's not a lvalue (that is, a resolvable address that you can write on the left-hand side of an expression), it's the resolution of a method call which is typically a rvalue (that is, not writable). The reason you CAN write bounds is because it is a lvalue (or in particular there is a defined setBounds: method that is part of UIView).
Remember that the instance variables (ivars) in the object are accessed via get & set methods. So if you know that, you know that label1.bounds is referencing a get method, not a variable. Given that, you should now understand why you can't do what you ask. A method is not open to assignment.
Remember that the instance variables (ivars) in the object are accessed via get & set methods. So if you know that, you know that label1.bounds is referencing a get method, not a variable. Given that, you should now understand why you can't do what you ask. A method is not open to assignment.
Interface
@property (monatomic, copy) NSString *apple;
Implementation...
@synthesize apple;
self.apple = [[NSString alloc] initWithString:@"I have just assigned apple variable to this string"];
If what you say is true, then is the following not true?
Code:Interface @property (monatomic, copy) NSString *apple; Implementation... @synthesize apple; self.apple = [[NSString alloc] initWithString:@"I have just assigned apple variable to this string"];
-(void) setApple: (NSString *) anObject;
If what you say is true, then is the following not true?
Code:Interface @property (monatomic, copy) NSString *apple; Implementation... @synthesize apple; self.apple = [[NSString alloc] initWithString:@"I have just assigned apple variable to this string"];
- (void)setApple:(NSString *)newApple {
[apple release]; //Release the old value
apple = [newApple copy]; //Assign the new value
}
@synthesize apple = _apple;
// So,
// _apple = instance variable
// [self apple] = getter
// [self setApple] = setter
NSLog(@"%@", self.apple);
- (NSString *)apple {
return _apple;
}