Where to place your typedef struct declaration file ?

Joined
Oct 7, 2009
Messages
14
Reaction score
0
Points
1
Location
Singapore
I just started playing with typedef struct

MyClass.h
Code:
#import <Cocoa/Cocoa.h>
@interface MyClass:NSObject
{
	NSRange Range;
}
-(void) Generic_Message;
@end

MyClass.m
Code:
#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 );
}
@end

Main.m
Code:
#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
Code:
Range = { 17 , 4 };
work ?

And secondly, how to I create my own typedef struct ?
For example I created this:
Code:
typedef struct _CustomType
{
	int width;
	int length;
	int height;
}
CustomType;
where do I put it ?
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
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
Code:
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:
Code:
#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:
Code:
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:
Code:
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!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top