|
|
#1 |
|
[self set] or just = ?
Hi,
1. Suppose i have an instance variable named "tmp" Is there a difference between [self setTmp:nil] and tmp=nil? 2. besides the getter and setter thing, is there a difference between a property instance variable and a non-property instance variable? Thanks. |
|
|
|
0
|
|
|
#2 |
|
1) Yes. One is a method call and the other is just a statement. More seriously if you don't release whatever tmp was pointing to when you set it to nil then you leak memory. The setter (if correctly written or synthesised) will do this.
2) In real terms I don't think so. You can use the object.variable = newVal syntax with properties but this just calls [object setVariable:newVal] really.
__________________
Sponsor me to cycle 100Km round London in the dark |
|
|
|
0
|
|
|
#3 |
|
nil was just an example...
About 1, nil was just an example, the question wasn't about release, suppose i have an instance variable named "tmp", is there a functional difference between [self setTmp:tmp2] and tmp=tmp2?
|
|
|
|
0
|
|
|
#4 |
|
Yes: one is a method call the other isn't.
__________________
Sponsor me to cycle 100Km round London in the dark |
|
|
|
0
|
|
|
#5 |
|
ok
Besides that it's exactly the same?
|
|
|
|
0
|
|
|
#6 |
|
It depends on what setTmp: does. You seem to assume that it only ever releases the current pointed at object and sets the pointer to the new object. There is nothing preventing a programmer writing all sorts of actions into setTmp:
In general one object should never have direct access to the state variables on another object. So what this means is that an object can call tmp=whatever to set it's own property or instance variable but another object should never call that. Other objects should always call setTmp: as they should not make assumptions about the internal state of the other object. If we look at a concrete example: Code:
@interface Rectangle
{
int width;
int height;
int area;
}
-(void) setWidth:(int) newWidth;
-(void) setHeight:(int) newHeight;
-(int) area;
@end
In this case we might think that the implementation of area calculates the area when called. But this is not necessarily true and we cannot make this assumption from outside the object. It's just as possible that setting width or height (via setWidth: or setHeight calculates the area at that point and stores it in the ivar.So if we assume that the implementation of Rectangle is as below: Code:
@implementation Rectangle
-(void) setWidth:(int) newWidth
{
width = newWidth;
area = width * height;
}
-(void) setHeight:(int) newHeight
{
height = newHeight;
area = width * height;
}
-(int) area
{
return area;
}
@end
Code:
@interface Square : Rectangle
@end
@implementation Square
-(void) setWidth:(int) newWidth
{
width = newWidth;
height = newWidth;
}
-(void) setHeight:(int) newHeight
{
width = newWidth;
height = newWidth;
}
@end
Code:
@interface Square : Rectangle
@end
@implementation Square
-(void) setWidth:(int) newWidth
{
[super setWidth:newWidth];
[super setHeight:newWidth];
}
-(void) setHeight:(int) newHeight
{
[super setWidth:newWidth];
[super setHeight:newWidth];
}
@end
Note that I've typed all this straight into the reply box. It's probably got mistakes.
__________________
Sponsor me to cycle 100Km round London in the dark |
|
|
|
0
|
|
|
#7 | |
|
Quote:
The OOP religious fanatics make the assumption that you will become forgetful about the current code, mindlessly reuse the code without reading it, or that your project will eventually be maintained by a large team, and the implementation will get changed, which will screw up the simple assignment, but not the setter call. You can decide on the odds of this type of error happening (they are non-zero, but usually not 100% either), and whether spending/wasting time on writing this doctrinal excellence/nonsense is a reasonable tradeoff for your sense of risk. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| Faulty ram: replace the set, or just the one with errors? | digitalhen | Mac Pro | 5 | Sep 25, 2011 12:58 PM |
| Does Lion self install or do you kick it off after download | wilywagtail | Mac OS X 10.7 Lion | 3 | Jul 21, 2011 01:13 AM |
| Box Set or No Box Set | phantom5251 | Mac OS X | 9 | Sep 15, 2009 09:32 PM |
| one set or two sets of contacts? | drawstring | iPhone Tips, Help and Troubleshooting | 0 | Dec 1, 2007 08:43 PM |
All times are GMT -5. The time now is 08:46 AM.







calculates the area at that point and stores it in the ivar.
Linear Mode

