Why instance variables of two unique objects reference the memory location? - objective-c

I'm using Stephen Kochan's examples in his book "Programming in Objective-C, edition 4" to learn the program language using the Windows based GNUstep environment. I'm using the gnustep mysys-system-0.29.0, gnustep-core-0.29.0 and gnustep-devel-1.4.0 installs.
Now to the program in chapter 3 of Kochan's book. When I execute the source code,
I'm getting the same instance variable values printed out for the two unqiue objects,
'frac1' and 'frac2'. It appears that 'frac1' object's memory locations for instance variables numerator and denominator reference the same memory locations for the 'frac2' object.
When I set then numerator to 3 and the denominator to 7 in 'frac2', the numerator and denominator in 'frac1' are overwriten. Why? I do not know why?. I get the same fraction 3/7 displayed when printing unique instance variables for 'frac1' and 'frac2'.
Below is the source program that I've compiled:
//========================
// ##copied from "Programming in Objective C" pages 30-44 - edition 4
#import <Foundation/Foundation.h>
// --- #interface section ----
#interface Fraction:NSObject
- (void)print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
#end
//----#implementation----
#include <stdio.h>
#implementation Fraction
//{
int numerator;
int denominator;
//}
- (void)print
{
NSLog (#"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
#end
//--- program section ----
int main(int argc, const char * argv[])
{
//Fraction *myFraction;
// Create an instance of a Fraction
//myFraction = [Fraction alloc];
//myFraction = [myFraction init];
Fraction *frac1;
frac1 = [Fraction alloc];
frac1 = [frac1 init];
Fraction *frac2 = [[Fraction alloc] init];
// Set 1st fraction to 2/3
[frac1 setNumerator: 2];
[frac1 setDenominator: 3];
// Set 2nd fraction to 3/7
[frac2 setNumerator: 3];
[frac2 setDenominator: 7];
// Display the fractions
// 'frac1' print should display fraction 2/3, however 3/7 is displayed
NSLog (#"first print is:");
[frac1 print];
// 'frac2' print displays fraction 3/7, as expected.
NSLog (#"second print is:");
[frac2 print];
return 0;
}
//========================

You have commented out the { and } surrounding the declarations of numerator and denominator. My guess is you did this because you were getting compiler errors when you left the braces in.
I deduce that Kochan's book is written for programmers using Apple's current Objective-C compiler and runtime, for iOS and 64-bit Mac OS X. This is also called the "new runtime" or the "modern runtime". This version of the compiler and runtime has a feature called “non-fragile instance variables”, which lets you put your instance variables in your class implementation, instead of in your class interface. Kochan's code uses this non-fragile instance variable feature.
The GNU compiler and runtime that you are using don't support this feature.
There is a new GNU Objective-C runtime that does support this feature, but the GNU compiler doesn't support it. You would have to use Apple's compiler (clang) with the new GNU runtime. I don't know how easy it is to get that setup working on Windows.
You can learn more about this mess here: http://wiki.gnustep.org/index.php/ObjC2_FAQ
The workaround is to declare your instance variables in your interface. The answers from Tim Dean and Andrew Madsen show the syntax.

Your problem is that you declare the numerator and denominator values in the #implementation of the class rather than in the #interface. This makes them into global variables rather than being instance variables for you class. Update your interface to be like this:
#interface Fraction:NSObject {
int numerator;
int denominator;
}
- (void)print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
#end
and then remove the declarations from your #implementation section. It should then behave as you are expecting it to.

The problem is that Fraction's two instance variables, numerator and denominator, aren't actually declared as instance variables, rather they're being declared as globals. You should declare them in the #interface like this:
#interface Fraction:NSObject
{
int numerator;
int denominator;
}
-(void)print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
#end
Global variables can be seen and set by any part of your program. When you declare the variables as instance variables in the #interface section of your class, each instance of the class gets its own set of those variables, and other instances can't see/change them directly.

Related

Objective C private instance variables error

I am just starting out with Objective C trying out a sample program about fractions from the Programming in Objective C book:
#import <Foundation/Foundation.h>
//---- #interface section ----
#interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
#end
//---- #implementation section ----
#implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog (#"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
#end
//---- program section ----
I keep getting the inconsistent instance variable specification error for the instane variables numerator and denominator.If I move the declarations to the #interface section, the error goes away but I want to know why I am getting this error? I am using Xcode 3.6.2 on Snow Leopard 10.6.8
You're getting an error because ivars needs to be declared in the interface.
By the way, you're using a veeeeery old version of xcode (probably too old to submit to the appstore), so you might want to look into getting xcode 4.6. There's also been many updates to the Objective-C language. In modern objective-c, there is no longer a need to manually declare ivars like this.
If you REALLY would like to keep ivars out of the header file, you can declare them in an empty category interface (also called a "class extension") like this, in the .m file:
#interface YourClass() {
ivars here
}
#end
Instance variables in #implementation blocks are only supported since Xcode 4.2 (LLVM compiler version 3.0). You must upgrade your development tools to take advantage of this feature.
The motivation for this language feature is to achieve better information hiding in your class structure. That means that clients of your interface (your .h file) may not need to be aware of the ivars the class is using, so they are "hidden" in the implementation file.
Another way to hide ivar is to include them in a class extension in your implementation (.m) file:
// Fraction.m
#interface Fraction () {
int numerator;
int denominator;
}
#end
#implementation Fraction
// ...
#end
But it also requires Xcode 4.2+ and a modern runtime environment.
For complete compatibility with old compilers, ivars can only be placed in the interface section of the .h file.

Beginners confusion about objective c, assignment, properties etc

Sorry I couldn't be more descriptive in the title. I know C++ and C#, and a bit of Java, and I am doing some Objective C tutorials and can't explain what is going on here.
I will show you an example:
Here is the header/interface of the rectangle class:
#import <Foundation/Foundation.h>
#class XYPoint;
#interface Rectangle : NSObject
#property int width, height;
-(int) area;
-(int) perimeter;
-(void) setW:(int)w andH:(int)h;
-(XYPoint *) origin;
-(void) setOrigin:(XYPoint*)pt;
#end
Here is the implementation:
#import "Rectangle.h"
#implementation Rectangle{
XYPoint *origin;
}
#synthesize width, height;
-(void) setW:(int)w andH:(int)h
{
width = w;
height = h;
}
-(int) area{
return width*height;
}
-(int) perimeter{
return (width+height)*2;
}
-(XYPoint *) origin{
return origin;
}
-(void) setOrigin:(XYPoint*)pt{
origin = pt;
}
#end
And here is my main program:
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "XYPoint.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Rectangle *r = [[Rectangle alloc]init];
XYPoint *p = [[XYPoint alloc]init];
[p setX:100 andY:300];
[r setW:6 andH:8];
r.origin = p;
NSLog(#"Width and height is %i %i", r.width, r.height);
NSLog(#"Origin is at %i,%i", r.origin.x, r.origin.y);
NSLog(#"Arear and perimeter are %i and %i", [r area], [r perimeter]);
}
return 0;
}
The line r.origin = p; confuses me. Exactly which origin is being referred to here? The getter method as defined in the interface, or the actual member variable which is declared in the implementation?
Note origin is not a property.
To be honest I expected an error. The main program should only see the getter method, but it doesn't seem like a getter method that returns something could be on the left hand side of an assignment operator. Or is the system somehow mapping that syntax to a call to setOrigin?
I had expected the only possible way to set the origin would be something like [r setOrigin: p]
I can see how something like r.width = 5 would work, as width as declared in the header as being a property, but I cannot see how r.origin = p works.
Can someone explain what is going on here?
Thanks.
Your guess is right - that syntax, called dot notation, is translated into a call to [r setOrigin:p] by the compiler. The general idea is that just like most other languages, you have getters and setters for object properties; in Objective-C, these are usually written as:
- (type)variableName;
- (void)setVariableName:(type)aVariable;
So long as your own code conforms to these conventions, you can use dot notation to both get and set variables on your classes, and trust that your methods are called.
There's obviously a lot more going on behind the scenes, so I hope this brief explanation is enough for you for now. If you're interested, though, you can look further into:
Synthesizing properties and what actually happens - methods of the form shown above are generated by the compiler, along with instance variables for actual data storage
Key-value coding, which takes advantage of these principles and conventions
Special cases for some types (for example, BOOL variables' getters are usually written as -isVariableName rather than -variableName), and how you can manage them with #property attributes
Yes, property accessors invoked by the dot notation are automagically traansformed to method calls by the compiler; thus
variable = object.property;
is equivalent to
variable = [object property];
and
object.property = variable;
is equivalent to
[object setProperty:variable];

gcc compile errors in an basic example objc program

Hey all, I'm new to programming and going through an objective-c book to learn the language and programing fundamentals. I've looked through the code repeatedly, went back to the book's example, and attempted to understand the gcc comple errors. Here's my code:
#import <stdio.h>
#import <objc/Object.h>
#interface Point: Object
{
int xaxis;
int yaxis;
}
-(void) print;
-(void) setx: (int)x;
-(void) sety: (int)y;
#end
#implementation Point;
-(void) print
{
printf("(%i,%i)", xaxis, yaxis);
}
-(void) setx: (int) x
{
xaxis = x;
}
-(void) sety: (int) y
{
yaxis = y;
}
#end
int main (int argc, char *argv[])
{
Point *myPoint;
myPoint = [Point alloc];
myPoint = [myPoint init];
[myPoint setx: 4];
[myPoint sety: 5];
printf("The coordinates are: ");
[myPoint print];
printf("\n");
[myPoint free];
return 0;
}
Then the compile errors from gcc look like this:
urban:Desktop alex$ gcc point.m -o point -l objc
point.m: In function ‘main’:
point.m:38: warning: ‘Point’ may not respond to ‘+alloc’
point.m:38: warning: (Messages without a matching method signature
point.m:38: warning: will be assumed to return ‘id’ and accept
point.m:38: warning: ‘...’ as arguments.)
point.m:40: error: ‘mypoint’ undeclared (first use in this function)
point.m:40: error: (Each undeclared identifier is reported only once
point.m:40: error: for each function it appears in.)
point.m:49: warning: ‘Point’ may not respond to ‘-free’
Where am I going wrong?
btw I'm going through "Programming in Objective-C" by Stephen Kochan if you wanted to know.
First the base class should be NSObject, not Object
the normal way to do the initialization is to write the alloc and init in the same statement. You would typically have an -(id)init; method in your class:
-(id)init
{
if ( ( self = [super init] ) )
{
; // additional initialization goes here
}
return self;
}
and
int main (int argc, char *argv[])
{
Point *myPoint = [[Point alloc] init];
Properties are better used, then you get the setter and getter automatically generated for you
instead of
#interface Point: Object
{
int xaxis;
int yaxis;
}
write
#interface Point : NSObject
{
}
#property int xaxis;
#property int yaxis;
then when you assign you can either write
[myPoint setXaxis:4]
or
myPoint.xaxis = 4;
when you release the object write release, not free
[myPoint release];
hth
You have warnings and an error. The warnings seem to suggest that Object, which you are subclassing, doesn't implement alloc, init or free. Normally, on an Apple platform, you'd subclass NSObject, which does implement these, but without knowing which platform you're on, it's not possible to advise the correct option.
Secondly, you had a typo, but that now seems to be corrected. This
point.m:40: error: ‘mypoint’ undeclared (first use in this function)
suggests that you had mypoint in your code, rather than myPoint.
You forgot to include the header Foundation.h:
#import <Foundation/Foundation.h>

Class in ObjectiveC doesn't seem to recognize inherited messages

I'm learning ObjectiveC with "Programming in Objective C" by Stephen G Kochan and I'm having some difficulties with an example. It creates a simple Fraction class that inherits from Object. That's where I get into trouble, when I try to send messages that are understood by Object instead of Fraction, such as init, alloc or free (see code below):
// Fraction
#import <stdio.h>
#import <objc/Object.h> // base object
// #interface section
#interface Fraction: Object
{
int numerator;
int denominator;
}
- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
#end
// #implementation section
#implementation Fraction;
-(void) print
{
printf(" %i/%i ", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
#end
// Program section
int main( int argc, char *argv[])
{
Fraction *myFraction;
// Create Instance of fraction
myFraction = [Fraction alloc];
myFraction = [Fraction init];
// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
// Display the fraction
printf("The value of my fraction is: ");
[myFraction print];
printf("\n");
// Destroy the instance
[myFraction free];
return 0;
}
When I compile it:
gcc fraction.m -o fraction -l objc
I get the following warnings:
fraction.m: In function ‘main’:
fraction.m:47: warning: ‘Fraction’ may not respond to ‘+alloc’
fraction.m:47: warning: (Messages without a matching method signature
fraction.m:47: warning: will be assumed to return ‘id’ and accept
fraction.m:47: warning: ‘...’ as arguments.)
fraction.m:48: warning: ‘Fraction’ may not respond to ‘+init’
fraction.m:62: warning: ‘Fraction’ may not respond to ‘-free’
fernando#McFofo ~/code/learning objective c:: ./fraction
objc[1678]: Fraction: Does not recognize selector forward::
and the program, when run, complains about an illegal instruction…
Anybody knows what's going on?
Classes in Cocoa (the most popular Objective-C library, they are almost synonymous) tend to inherit from NSObject, the Cocoa root class. It is documented here.
There is also the NSObject protocol, implemented by NSProxy. It forms the basis of Cocoa's support for Distributed Objects. That said, we may be getting ahead of ourselves with that!
I also notice that you are trying to do the following:
Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [Fraction init];
You are allocating memory for your object and then losing it by overwriting the pointer with another one. The standard practice is to do it on one line as follows:
Fraction *myFraction = [[Fraction alloc] init];
If you must do it one multiple lines, you must do the following:
Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [myFraction init];
// myFraction is now ready to go!
I stumbled across a site that refers to what appears to be the book you're reading. The examples highlighted on the website inherit from NSObject and import <Foundation/NSObject.h>. Is there any particular reason you eschewed this and decided upon the Object class provided by the runtime?

what does it mean to set a method as an object or even setting a method argument as an object?

if i have a class named
#interface myClass : NSObject {
int firstnum;
int secondnum;
}
//an a method that looks like this
-(myClass *) myMethod (myClass *) f;
#end
what does that method return as? im used to seeing something like (int) myMethod knowing that it returns an integer. but when its returning an object such as the classes name here what can it possibly return? if you'd like ill write out the whole project im working/studying on. (some methods have been truncated to keep the question simple. but if you'd like ill post it let me know. thnx
#import <Foundation/Foundation.h>
#interface Fraction : NSObject {
int numerator;
int denominator;
}
#property int numerator, denominator;
-(Fraction *) add: (Fraction *) f;
#end
#import "Fraction.h"
#implementation Fraction
#synthesize numerator, denominator;
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
#end
The question might be: what is an object? An object is some memory set aside to hold a bunch of values, and it's associated with some methods to interact with those values. You very rarely want to copy that whole chunk of memory. Instead, you pass around pointers to the memory location. So when you pass in or return an object, you are actually just passing around a pointer to the object's location in memory. Pointers are basically just integer values, so that's what's being returned.
In most languages, you don't even have to think about this, and in fact you rarely have to do it in Objective-C either. Just remember that MyClass* is the type of objects that are of class ´MyClass´ and that they respond to all messages that are declared on that class.
That method returns just what it claims to: a pointer to an instance of myClass. In the case of your add: method, it returns the pointer result that you created.
Incidentally, unless you have Garbage Collection enabled, you should add [result autorelease] to that method before returning; otherwise your result fraction will be around forever and you'll leak memory.
CocoaDevCentral's Objective-C Tutorial will help with that memory management, and also might get you more comfortable with methods returning pointers to objects.
it looks like you're working through a copy of the Programming in Objective-C book. In my copy, there is a section at the end of Chapter 13 (Underlying C Language Features) called "How Things Work". I think you are looking for "Fact #2: An Object Variable is Really a Pointer". In your case, the add method is returning a pointer to an instance of the Fraction class. (The asterisk, *, means pointer, the class name before it says what sort of thing it is pointing to) There is more about pointers earlier in the chapter if you don't mind skipping about.
I'm going to be picky and try and rewrite this a bit
#import <Foundation/Foundation.h>
#interface Fraction : NSObject {
// You should really use the recommended type instead of int
NSInteger numerator;
NSInteger denominator;
}
// Don't try and keep these on one line.
// Also, don't depend on default values for assign, retain, or copy when declaring
#property (assign, nonatomic) numerator;
#property (assign, nonatomic) denominator;
// declare the add: function which takes a pointer to a Fraction as a parameter
// and returns a pointer to the restulting fraction
- (Fraction *)add:(Fraction *)aFraction;
#end
#import "Fraction.h"
#implementation Fraction
// Again, don't try and keep these on one line.
// It's easier to visually note the number of synthesised properties matches
// the number of declared properties.
#synthesize numerator;
#synthesize denominator;
// Assume that there is an init function.
- (Fraction *)add:(Fraction *)aFraction
{
Fraction *result = [[Fraction alloc] init];
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
result.numerator = self.numerator * aFraction.denominator
+ denominator * aFraction.numerator;
result.denominator = self.denominator * aFraction.denominator;
[result reduce]; // I assume this is implemented.
return result; // Assuming garbage collection, otherwise use
// return [result autorelease];
}
#end