New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus • Advice and insight from world-class Apple enthusiasts • Exclusive access to members-only contests, giveaways and deals

Join today!

Start a Discussion

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Where to place your typedef struct declaration file ?

Post Reply New Thread Subscribe

Thread Tools

Bracer's Avatar
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road

Bracer is offline
I just started playing with typedef struct

#import <Cocoa/Cocoa.h>
@interface MyClass:NSObject
	NSRange Range;
-(void) Generic_Message;
#import "MyClass.h"
#import <stdio.h>
#import <Foundation/Foundation.h>

@implementation MyClass
-(void) Generic_Message
	//Range = { 17 , 4 }; //For some reason, this doesn't work :(
	Range=NSMakeRange (17,4) ;
	NSLog (@"\nRange location: %d\nlength: %d",  Range.location, Range.length );
#import "MyClass.m"

int main(int argc, char * argv[])
	id ClassInstance = [ [MyClass alloc] init];
	[ClassInstance Generic_Message];
	[ClassInstance release];
	return 0;
I have two questions, why doesn't
Range = { 17 , 4 };
work ?

And secondly, how to I create my own typedef struct ?
For example I created this:
typedef struct _CustomType
	int width;
	int length;
	int height;
where do I put it ?
QUOTE Thanks

nabl's Avatar
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09

nabl is offline
1. NSRange is just a C struct. To set its value, you have to set the components individually. For your example, you’d have to do
Range.location = 17;
Range.length = 4;
NSMakeRange does this for you and returns the resulting NSRange.

2. This is largely your choice. I would personally put it either in MyClass.h (outside the interface block) if you’ll only be using it in MyClass.m, or if you plan on using it in various other files, perhaps create an entirely separate header (for example, Definitions.h) and then import that into your other classes as necessary.

I would also like to point out some stylistic conventions; it’s not that your code is wrong, but most Objective-C code that you see will follow these. Typically, only class names begin with capital letters while instances of those classes, their instance variables, and method names begin with lowercase letters. In addition, rather than using underscores, most Objective-C code uses “camelCase,” where you simply place a capital letter where a new word begins.

As an example, I would change your main.m code to this:
#import "MyClass.m"

int main(int argc, char * argv[])
	id classInstance = [ [MyClass alloc] init];
	[classInstance genericMessage];
	[classInstance release];
	return 0;
Of course, you’d also have to make the appropriate changes to the header and class files (for example, changing the Range instance variable to range).

One more recommendation: don’t get into the habit of using id too much. There are times when it is a lifesaver, but using it everywhere makes the code harder to read and removes some of the benefits of compile-time checking (explained below), leaving errors to show up during runtime.

For example, when you’re initializing an instance of MyClass in your code, try this:
MyClass *classInstance = [ [MyClass alloc] init];
When you know that a variable will only hold one type of data for its entire life, you’ll typically want to initialize it like this, which is called static typing.

As for what I mentioned about compile-time checking, say you were to accidentally send the generalMessage message to your instance of MyClass. As you can see, generalMessage is not defined in MyClass.h. If you initialize the instance with MyClass *classInstance, the compiler will see that you are sending a message that MyClass doesn’t know, and the compiler will warn you. However, if you initialize the instance with id classInstance, the compiler will not recognize the error, and the program will run fine until the program reaches that line, at which point it will throw an exception and crash—something like this:
2009-10-10 17:14:02.083 Untitled[610:a0f] -[MyClass generalMessage]: unrecognized selector sent to instance 0x10010c650
2009-10-10 17:14:02.086 Untitled[610:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyClass generalMessage]: unrecognized selector sent to instance 0x10010c650'
For more information, check out The Objective-C 2.0 Programming Language.

Hope that helps!
QUOTE Thanks

Post Reply New Thread Subscribe

« Jgrasp on Mac OS X | C++ on leopard/snow leopard »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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 Off
Forum Jump

Similar Threads
Thread Starter
Last Post
File Manager - New Mac User crimedogg OS X - Operating System 7 01-20-2009 10:24 AM
Garbled attached file Selwyn OS X - Apps and Games 3 10-30-2008 04:33 PM
My PowerMac G5 is acting screwy! aedolch OS X - Operating System 5 08-01-2008 06:12 PM
Flash file not working in iWeb 08 mac_min Web Design and Hosting 0 02-11-2008 09:17 PM
Putting one type of file in one place ShadyCowboy OS X - Operating System 0 05-24-2007 07:33 PM

All times are GMT -4. The time now is 10:11 AM.

Powered by vBulletin
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.

Welcome to

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!

(4 digit year)

Already a member?