Results 1 to 2 of 2
  1. #1
    Where to place your typedef struct declaration file ?
    Bracer's Avatar
    Member Since
    Oct 07, 2009
    Location
    Singapore
    Posts
    14
    Where to place your typedef struct declaration file ?
    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 ?

  2. #2
    Where to place your typedef struct declaration file ?
    nabl's Avatar
    Member Since
    Dec 13, 2007
    Location
    United States of America
    Posts
    256
    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!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Vip file, hmo file hmp file
    By stuartavfc72 in forum Music, Audio, and Podcasting
    Replies: 4
    Last Post: 10-26-2013, 12:47 AM
  2. Help with file management - deleting files without losing my place
    By TheConverted in forum OS X - Operating System
    Replies: 1
    Last Post: 04-07-2012, 05:04 PM
  3. Mac Forum - first place or last place to visit
    By j0nb0y32 in forum Schweb's Lounge
    Replies: 13
    Last Post: 11-08-2011, 02:44 PM
  4. Best place to get this?
    By Shinobi in forum Apple Desktops
    Replies: 2
    Last Post: 11-22-2009, 04:43 PM
  5. Putting one type of file in one place
    By ShadyCowboy in forum OS X - Operating System
    Replies: 0
    Last Post: 05-24-2007, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •