The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   n00b obj-c question @property (http://hintsforums.macworld.com/showthread.php?t=100119)

messels 03-25-2009 10:22 PM

n00b obj-c question @property
 
hi, the following is my code (three sep. files). i'm getting an error stating that i have a parsing error before the '@' symbol. i'm just trying to wrap my head around all this and i can't find my mistake (copying from a book for now). :|

please help! thx! :)


//
// Fraction.h
// FractionTest
//
// Created by Adria Mooney on 3/25/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
//the Fraction class
@interface Fraction : NSObject{
int numerator;
int denominator;
}

@property int numerator, denominator;

-(void) print;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
-(void) add: (Fraction *) f;
@end

//
// Fraction.m
// FractionTest
//
// Created by Adria Mooney on 3/25/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Fraction.h"


@implementation Fraction

@synthesize numerator, denominator;

-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator/denominator;
else
return 1.0;
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}

-(void) add: (Fraction *) f
{
//to add two fractions:
//a/b + c/d = ((a*d) + (b*c))/ (b*d)
numerator = numerator * f.denominator + denominator * f.numerator;
denominator = denominator * f.denominator;
}

@end

#import "Fraction.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];

//creat two fractions 1/4 and 1/2 and then add them togehter
[aFraction setTo: 1 over: 4];
[bFraction setTo: 1 over: 2];

//display fractions
[aFraction print];
NSLog (@"+");
[bFraction print];
NSLog (@"=");

[aFraction add: bFraction];
[aFraction print];
[aFraction release];
[bFraction release];

[pool release];
return 0;
}

robinwmills 03-26-2009 12:15 AM

I didn't see any problem with your code. It built and ran first time on XCode 3.1.1

Code:

2009-03-25 21:11:42.137 Fractions[2525:10b] 1/4
2009-03-25 21:11:42.139 Fractions[2525:10b] +
2009-03-25 21:11:42.139 Fractions[2525:10b] 1/2
2009-03-25 21:11:42.140 Fractions[2525:10b] =
2009-03-25 21:11:42.141 Fractions[2525:10b] 6/8

Robin

blb 03-26-2009 03:20 AM

What's the name of the third file (containing main())? Make sure it has a .m extension or the compiler will assume C instead of Objective-C, where an @ will probably cause errors.

messels 03-26-2009 11:24 AM

hey everyone,

thanks for looking the code over. unfortunately, it's not compiling for me! :|

does it matter that i'm using xCode 2.5? i'm not on an intel-based mac. :eek:

i checked the file extensions. xyz.m so it _should_ be picking up correctly.

i even tried putting all the data into one file (so no import other htan an #import <Foundation/Foundation.h>)...

erg.

robinwmills 03-26-2009 11:43 AM

Hi M

Yes. I think you'll need XCode 3 to compile that code. And I think you have to use Leopard to run that. However I believe XCode 3 does run on PPC.

I think the @property syntax isn't available on the ObjC on 2.5 - although it's easy to work around that. Let me know if you're stuck and I'll fix it this evening for you after work.

Robin

messels 03-26-2009 06:32 PM

for some reason i was under the impression that xcode 3.x wouldn't run on ppc mac's...is that only the iphone sdk (that won't run on a ppc mac)?

thanks for your help thus far! :)

Mikey-San 03-26-2009 07:29 PM

Quote:

Originally Posted by robinwmills (Post 525915)
Hi M

Yes. I think you'll need XCode 3 to compile that code. And I think you have to use Leopard to run that. However I believe XCode 3 does run on PPC.

I think the @property syntax isn't available on the ObjC on 2.5 - although it's easy to work around that. Let me know if you're stuck and I'll fix it this evening for you after work.

Robin

This is not accurate. Xcode 2.5 can compile Objective-C 2.0 on Leopard just fine, which includes properties.

messels:

First, you didn't actually say what line the error happened on. That is important information. Second, at this point it would probably be easiest if you zipped up a copy of your project folder and posted it here for us to attempt to build. We cannot reproduce your problem, so it's probably project-specific.

robinwmills 03-26-2009 11:20 PM

I've zipped the code to build and run on on XCode 2.5 on my elderly PPC Powerbook running Tiger 10.4.11 and on XCode 3 on my super-dooper x86 iMac running Leopard:

http://clanmills.com/files/messels.zip

You'll find two directories:
Fractions2 - for XCode 2.5
Fractions3 - for XCode 3
Please see ReadMe.txt with a little discussion about the @property and @synthesize keywords.

Hope that helps.

Robin

messels 03-27-2009 11:59 AM

Quote:

Originally Posted by robinwmills (Post 526016)
I've zipped the code to build and run on on XCode 2.5 on my elderly PPC Powerbook running Tiger 10.4.11 and on XCode 3 on my super-dooper x86 iMac running Leopard:

http://clanmills.com/files/messels.zip

You'll find two directories:
Fractions2 - for XCode 2.5
Fractions3 - for XCode 3
Please see ReadMe.txt with a little discussion about the @property and @synthesize keywords.

Hope that helps.

Robin


hey robin, it certainly is a big help. thanks! what i don't understand is that you're not actually using the @property or @synthesize in the fraction classes for xCode 2.x, forcing you to create the getters and setters, right? you also used the cocoa (#import <Cocoa/Cocoa.h>) whereas i was under the impression that i was suppose to change that to <Foundation/Foundation.h>. why did you use cocoa and the book suggests foudnation? is that becuase you're working in xcode 2.5 and need to use cocoa in that instance or is it that the libraries in foundation are also present in cocoa?

thanks again! :D

Mikey-San 03-27-2009 01:39 PM

Quote:

Originally Posted by messels (Post 526098)
hey robin, it certainly is a big help. thanks! what i don't understand is that you're not actually using the @property or @synthesize in the fraction classes for xCode 2.x, forcing you to create the getters and setters, right? you also used the cocoa (#import <Cocoa/Cocoa.h>) whereas i was under the impression that i was suppose to change that to <Foundation/Foundation.h>. why did you use cocoa and the book suggests foudnation? is that becuase you're working in xcode 2.5 and need to use cocoa in that instance or is it that the libraries in foundation are also present in cocoa?

thanks again! :D

Both versions build and run on Leopard in Xcode 2.5. Only one will build and run on Tiger. Here is how properties work in the Objective-C 2.0 runtime.

He's #importing and linking to Cocoa needlessly. The project only needs Foundation.

robinwmills 03-27-2009 10:43 PM

Hi M

I'm very happy to discuss this subject with you off-line at robin@clanmills.com

Robin

Mikey-San 03-27-2009 11:30 PM

Quote:

Originally Posted by robinwmills (Post 526175)
Hi M

I'm very happy to discuss this subject with you off-line at robin@clanmills.com

Robin

Or you guys could discuss it here so everyone can learn.

robinwmills 03-27-2009 11:40 PM

Mikey-San

I respect your many and very wise contributions to this forum. I promise to report back to this thread when we have reached a positive conclusion.

Robin

Mikey-San 03-27-2009 11:48 PM

Quote:

Originally Posted by robinwmills (Post 526183)
Mikey-San

I respect your many and very wise contributions to this forum. I promise to report back to this thread when we have reached a positive conclusion.

Robin

About what? This is not productive.

robinwmills 03-28-2009 12:07 AM

Mikey-San

Please forgive me. 100% apology to you.

Robin

robinwmills 03-28-2009 12:40 PM

Mike-san is quite correct. It isn't productive to discuss things off-line.

To answer messels question about Cocoa/Cocoa.h and Foundation/Foundation.h. I quite unaware of this. I used the wizard to create a project from which to call his code. The wizard wrote that code.

I believe Mike-san's correct that @property feature was added to Obj-C some time ago. None-the-less messel's code does not compile on 2.5. I got the same error message about the '@'. Something interesting to investigate here. I'll report back.

I'd like to challenge to messels. Why is the result wrong? 1/2 + 1/4 = 6/8. When I was in elementary school in Scotland, 1/2+1/4=3/4.

Mikey-San 03-29-2009 02:26 AM

Quote:

Originally Posted by robinwmills (Post 526246)
I believe Mike-san's correct that @property feature was added to Obj-C some time ago. None-the-less messel's code does not compile on 2.5. I got the same error message about the '@'. Something interesting to investigate here. I'll report back.

If it's not working, I think I might know why your results differ from my earlier assertion, and it's not a technical issue . . . because the last time I had 2.5 installed, I had it alongside 3.0. $10 says I'm simply confused.

hayne 03-29-2009 04:08 AM

The release notes for Xcode 3.0 (http://developer.apple.com/releaseno...ode/index.html) say:
Quote:

Originally Posted by above document
Objective-C has been enhanced with support for properties ...


Mikey-San 03-29-2009 04:14 AM

Quote:

Originally Posted by hayne (Post 526368)
The release notes for Xcode 3.0 (http://developer.apple.com/releaseno...ode/index.html) say:

I think just I had both versions installed some time back and thought the addition of the updated compiler, combined with the proper runtime, validated my memory. Thus proving yet again why memory should not be trusted.

robinwmills 03-29-2009 07:19 AM

Quote:

Originally Posted by Mikey-San (Post 526369)
I think just I had both versions installed some time back and thought the addition of the updated compiler, combined with the proper runtime, validated my memory. Thus proving yet again why memory should not be trusted.

Mikey-san:

Are you saying that you are mistaken and @property was not supported with XCode 2.5 ?

Mikey-San 03-29-2009 10:13 AM

Well, it looks that way. Does changing the compiler affect the target's ability to build? If not, there's your answer.

robinwmills 03-29-2009 01:46 PM

I believe that XCode is a UI which uses gcc and gdb to do the heavy lifting. So it's possible that when you install XCode 2.5 and 3.x on the same machine, they both use the same gcc and gdb. If the error message about '@' is coming from gcc (which I believe is the case), XCode 2.5 may be able to successfully build the project when both are installed. However on a "pure" XCode 2.5, it cannot. I said there was something interesting to investigate here. Goodness that's not obvious.

messels 03-29-2009 11:42 PM

Quote:

Originally Posted by robinwmills (Post 526246)
I'd like to challenge to messels. Why is the result wrong? 1/2 + 1/4 = 6/8. When I was in elementary school in Scotland, 1/2+1/4=3/4.

well, tehre's nothing reducing it to 3/4. that's an extra logic step. "reduction" that is.

anyway, sounds like i need to update to leopard at least...so i can just learn the latest version and not worry so much about what's been enhanced and what's not. i just want to learn this stuff.

thx'

Mikey-San 03-30-2009 12:39 PM

Quote:

Originally Posted by messels (Post 526465)
well, tehre's nothing reducing it to 3/4. that's an extra logic step. "reduction" that is.

anyway, sounds like i need to update to leopard at least...so i can just learn the latest version and not worry so much about what's been enhanced and what's not. i just want to learn this stuff.

thx'

You need to read the Objective-C 2.0 documentation. The newer runtime is not available on Tiger, and properties are part of the new runtime version.

robinwmills 03-30-2009 08:37 PM

Indeed I get a segmentation fault when I try to execute the Leopard build of Fractions.app on Tiger. The original issue is the compiler's error message which is due to @property not being supported in 2.5. So for both the compiler and the runtime, Tiger and @property don't live happily together.

It's a little odd that XCode 2.5 can build @property on Leopard when 3.0 has also been installed. And for sure you need the Leopard run-time system to execute it.

So I think we've beaten this one to death. M's thoughts to update to Leopard seem like a good idea I wish him every success in learning Cocoa. A journey worth the effort.

Mikey-San 03-30-2009 09:23 PM

Quote:

Originally Posted by robinwmills (Post 526584)
It's a little odd that XCode 2.5 can build @property on Leopard when 3.0 has also been installed.

Do we actually know this for certain, though? (Did you try?) It was just speculation on my part, since I'm unable to reconcile my memory otherwise.

robinwmills 03-31-2009 12:32 AM

Quote:

Originally Posted by Mikey-San (Post 526587)
Do we actually know this for certain, though? (Did you try?) It was just speculation on my part, since I'm unable to reconcile my memory otherwise.

I don't know for certain - I took your word for it!

However I don't think it really matters much whether this statement is true or not. Compiling @property on 2.5 doesn't really get anybody anywhere. As you have pointed out they still need Leopard to execute the code. So I think that is the conclusion - we're encouraging M to upgrade to Leopard and XCode 3.:)

Mikey-San 03-31-2009 12:52 AM

Quote:

Originally Posted by robinwmills (Post 526606)
I don't know for certain - I took your word for it!

What word? I think I made it pretty clear that I can't remember what I did.


All times are GMT -5. The time now is 05:36 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.