Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Defining and using constants in Objective C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Defining and using constants in Objective C



Hi Tharindu.

I would create two files: Constants.h and Constants.m. Just like all C code, you put your declarations in the .h and the definitions in the .m. They are going to be very similar, except the .m defines the values, so it only happens once. This is very important if you want to be able to compare NSString constants using pointer equality. You then have all of your other code import Constants.h

--------- Constants.h ---------------
extern const int Foo;
extern const float Bar;
extern const NSString *Suction;

--------- Constants.m ---------------
const int Foo = 42;
const float Bar = 4.2;
const NSString *Suction = @"Ball";

-Kenny


On Nov 29, 2009, at 9:30 AM, Tharindu Madushanka wrote:

> Hi
>
> When defining constants can I keep all important constants in one file and
> use it all over the application, rather defining same constant in several .m
> files. This might be a basic question, but just could not figure out how to
> do this.
>
> Thank you
> Tharindu Madushanka
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Defining and using constants in Objective C (From: Tharindu Madushanka <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.