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?
Related
i'm trying to learn programming, and the language i chose to start is objective c!
i'm currently studying from : Programing in Objective-C Fourth edition, and am a bit stuck at the classes chapter.
we have the following code :
#import <Foundation/Foundation.h>
#interface Fraction: NSObject;
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
#end
#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
int main (int argc, char * argv[])
{
#autoreleasepool {
Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [Fraction init];
[myFraction setDenominator:1];
[myFraction setNumerator:3];
NSLog(#"The value of myFraction is :");
[myFraction print];
}
return 0;
}
I copied the code from the book, in hopes to help me understand it better, but for some reason when i try to run i get the following message :
2014-04-25 22:23:28.374 cocoTerminal[1751:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[Fraction<0x1000011d8> init]: cannot init a class object.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8f5a825c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff86403e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f5ab490 +[NSObject(NSObject) dealloc] + 0
3 cocoTerminal 0x0000000100000dde main + 110
4 libdyld.dylib 0x00007fff8d43a5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
If someone would be so kind to explain what and why is causing the error, to a complete noob i will be forever grateful !
You need to initilise the object after allocating it.
myFraction = [[Fraction alloc] init];
For a good explanation on why you need to do so, check here.
This line:
myFraction = [Fraction init];
is what is giving rise to the cannot init a class object exception. It should be:
myFraction = [myFraction init];
You send alloc messages to classes, but init messages to instances of classes. You can't send init to a class object, as the error clearly states. Sending alloc to a class gives you an instance of that class, and that instance is what you should be sending init to.
As others have said, combining the two lines into Fraction * myFraction = [[Fraction alloc] init]; is the more usual way of doing it, but the way you're doing it is not actually wrong, once you send the init message to the correct thing.
I'm learning Objective-c programming and there are two errors I cannot solve. Could you tell me what's wrong?
#import "Fraction.h"
int main (int argc, char * argv[]) {
#autoreleasepool {
Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];
[aFraction setTo: 1 over: 4];
[bFraction setTo: 1 over: 2];
[aFraction print];
NSLog (#"+");
[bFraction print];
NSLog (#"=");
[aFraction add: bFraction]; /*error 1: No visible #interface for 'Fraction' declares the selector 'add:'*/
[aFraction reduce];
[aFraction print]; }
return 0; }
#import <Foundation/Foundation.h>
#interface Fraction : NSObject
#property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) add: (Fraction *) f;
-(void) reduce; /*error 2: Expected identifier or '(' */
#end
Grab TextWrangler from App Store
Open the offending .h file
Select Text -> Zap Gremlins...
Check "Substitute with •"
[ZAP!]
...
-(double) convertToNum;
••••••••••••••••••••••••••••••••••••••-(void) add: (Fraction *) f;
-(void) reduce; /*error 2: Expected identifier or '(' */
...
There's your problem right there!
Phillip is most likely correct. And I bet it is a ctrl+return. Try this:
Go to the end of the line that declares the reduce: method, then hit ctrl+a.
The cursor will likely jump back to the beginning of the line that declares the add:. This happens if you happen to hit ctrl+return at the end of a line.
To fix, go to the beginning of the line with reduce:, hit backspace, then hit return.
Example:
-(void) add: (Fraction *) f;
-(void) reduce; <*** cursor here, hit ctrl-a
After pressing ctrl-a, the cursor will be at the beginning of the line declaring add:. That means you have a bad return character at the end of the add: line.
Check the line before and after, too. Philip said he saw funky characters at the beginning of the add: line. Also, Xcode does have a "show hidden characters" feature. That might help but, in my experience, the bad newlines are invisible.
There seems to be some strange character or characters at the beginning of the -(void) add: (Fraction *) f; line. If I paste your interface section into Xcode, I get the same error as you do in the .h file. If I delete that line and re-type it, the error goes away.
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.
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>
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