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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 01-06-2011, 04:55 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 1
ksiedzulek is on a distinguished road
Default why it doesn't work with 'id'?

Hi here is a code:

Code:
#import <Foundation/Foundation.h>


// --------------------------------------------------

@interface Tire : NSObject
@end // Tire


@implementation Tire

- (NSString *) description
{
    return (@"I am a tire. I last a while");
} // description

@end // Tire



// --------------------------------------------------

@interface Engine : NSObject
@end // Engine


@implementation Engine

- (NSString *) description
{
	return (@"I am an engine.  Vrooom!");
} // description*/

@end // Engine


// --------------------------------------------------

@interface Car : NSObject
{
	Engine *engine;
	Tire *tires[4];
	int d;
}
@property int d;
- (void) print;

@end // Car


@implementation Car
@synthesize d;
- (NSString *) description
{
return [NSString stringWithFormat:@"ID: %d",d];
}
- (id) init
{
	if (self = [super init]) {
		engine = [Engine new];
		
		tires[0] = [Tire new];
		tires[1] = [Tire new];
		tires[2] = [Tire new];
		tires[3] = [Tire new];
	}
	
	return (self);
	
} // init

- (void) print
{
	NSLog (@"%@", engine);
	
	NSLog (@"%@", tires[0]);
	NSLog (@"%@", tires[1]);
	NSLog (@"%@", tires[2]);
	NSLog (@"%@", tires[3]);
	
} // print

@end // Car


// --------------------------------------------------

int main (int argc, const char * argv[]) 
{
	Car *car; // car.d works
	//id car;		// car.d doesn't work..why?
	car = [Car new];
	
	car.d=5;// ????
	NSLog(@"car nalezy do: %@",[car class]);
	[car print];
	NSLog(@"%@",car);//
	
	return (0);
	
} // main

and question: why if I use [Car*car] (in main) it differs from using id car? when I use 'id car' and disable car.d=5 it works proper (why?) I check class by using [method class] and for 'id car' and Car*car it returns Car class...
ksiedzulek is offline   Reply With Quote
Old 01-09-2011, 12:46 PM   #2 (permalink)
Registered Member
 
god0fgod's Avatar
 
Join Date: Jul 2010
Posts: 278
god0fgod is on a distinguished road
Default

From experience id doesn't work with certain things like the dot syntax for properties. Use the set method instead.
__________________
god0fgod is offline   Reply With Quote
Old 01-09-2011, 11:34 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

In order for dot syntax to work, the COMPILER must know the type of the object AT COMPILE TIME. If you declare the method as "Car *car", it knows how to create the methods calls. If you declare it "id car" then the compiler does not know the type of the object.

You can still access the property, but not with the dot syntax. Calling [car setD:5] should work fine either way.
__________________

Free Games!
smasher 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: 480
15 members and 465 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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