Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2010, 04:15 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Question Absolute beginner's Objective-C question

I'm fine with Java, C++ and other languages but I'm absolutely new to Objective-C. I've been poking around with several tutorials but I can't seem to get the following code right. Can anyone help? Thanks.

#include <stdio.h>
#include <Foundation/Foundation.h>

@interface Employee
{
id name = [NSString string];
int social;
}
@property id name;
@property int social;

+ (void)displayListOfEmployees;
- (void)displayEmployee;

@end

@interface FullTimeEmployee : Employee
{
float salary;
}

- (float)changeSalaryBOOL)up byfloat)percent;

@end

#import "main.h"


@implementation Employee

+ (void)displayListOfEmployees
{
printf("This is a list of all employees\n");
}

- (void)displayEmployee
{
printf("%s %d\n", name, social);
}

@synthesize name;
@synthesize social;

@end


@implementation FullTimeEmployee : Employee

- (float)changeSalaryBOOL)up byfloat)percent
{
float temp;
if (up) {
temp = salary * (1.0 + percent);
} else {
temp = salary * (1.0 - percent);
}
return temp;
}

@end

int main(int argc, char *argv[])
{
id employee;
employee = [FullTimeEmployee new];

[FullTimeEmployee displayListOfEmployees];
employee.name = @"Barry";
employee.social = 1234567890];
[employee displayEmployee];

[employee free];
return EXIT_SUCCESS;
}
bburd is offline   Reply With Quote
Old 03-20-2010, 04:21 PM   #2 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Are you getting compile errors?
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 03-20-2010, 04:39 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Default

Quote:
Originally Posted by ZunePod View Post
Are you getting compile errors?
id name = [NSString string]; //expected ":", ",", ";", or "}" or '__atribute__' before "=" token

@property id name; //no 'assign', 'retain' or 'copy attribute is specified -- 'assign' is assumed

printf("%s %d\n", name, social); // 'name' undeclared

@synthesize name; // synthesized property 'name' must either be named as the same as a compatible ivar or must explicitly name an ivar
bburd is offline   Reply With Quote
Old 03-20-2010, 04:52 PM   #4 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Put this between the {} in your .h

Code:
NSString *name;
Then have this in your .m:

Code:
id name = [NSString string]; 
name = @"TheNameYouWant";

printf("%s %d\n", name, social); // 'name' undeclared
NSLog("%@ %d\n", name, social);
Bolded is the correct code, the other two lines are not needed.

BTW: What is social, which data type?
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 03-20-2010, 05:04 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Default

#include <stdio.h>
#include <Foundation/Foundation.h>

@interface Employee
{
NSString *name;
int social;
}
@property id name; /Users/bburd/XCodeProjects/EmployeeConsole/main.h:9:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.h:9: warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed


@property int social;

+ (void)displayListOfEmployees;
- (void)displayEmployee;

@end

@interface FullTimeEmployee : Employee
{
float salary;
}

- (float)changeSalary:(BOOL)up by:(float)percent;

@end

#import "main.h"


@implementation Employee

+ (void)displayListOfEmployees
{
printf("This is a list of all employees\n");
}

- (void)displayEmployee
{
NSLog("%@ %d\n", name, social); /Users/bburd/XCodeProjects/EmployeeConsole/main.m:13:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.m:13: warning: passing argument 1 of 'NSLog' from incompatible pointer type

}
name = @"TheNameYouWant"; /Users/bburd/XCodeProjects/EmployeeConsole/main.m:15:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.m:15: warning: data definition has no type or storage class


@synthesize name;
@synthesize social;

@end


@implementation FullTimeEmployee : Employee

- (float)changeSalary:(BOOL)up by:(float)percent
{
float temp;
if (up) {
temp = salary * (1.0 + percent);
} else {
temp = salary * (1.0 - percent);
}
return temp;
}

@end

int main(int argc, char *argv[])
{
id employee;
employee = [FullTimeEmployee new]; /Users/bburd/XCodeProjects/EmployeeConsole/main.m:40:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.m:40: warning: 'FullTimeEmployee' may not respond to '+new'


[FullTimeEmployee displayListOfEmployees];
employee.name = @"Barry"; /Users/bburd/XCodeProjects/EmployeeConsole/main.m:43:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.m:43: error: request for member 'name' in something not a structure or union


employee.social = 1234567890;
/Users/bburd/XCodeProjects/EmployeeConsole/main.m:44:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.m:44: error: request for member 'social' in something not a structure or union



[employee displayEmployee];

[employee free];
/Users/bburd/XCodeProjects/EmployeeConsole/main.m:47:0 /Users/bburd/XCodeProjects/EmployeeConsole/main.m:47: warning: no '-free' method found



return EXIT_SUCCESS;
}
bburd is offline   Reply With Quote
Old 03-20-2010, 05:34 PM   #6 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

I think you need to go right back to the basics.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 260
18 members and 242 guests
14DEV, @sandris, ADY, ArtieFufkin10, ckgni, Dani77, HemiMG, IphoneSdk, jakerocheleau, JasonR, MACralik, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0