Proper use of #define to aid in compile-time checking of strings (was KVC and KVO for arrays)
Proper use of #define to aid in compile-time checking of strings (was KVC and KVO for arrays)
- Subject: Proper use of #define to aid in compile-time checking of strings (was KVC and KVO for arrays)
- From: "John Dann" <email@hidden>
- Date: Fri, 15 Feb 2008 15:52:29 +0000
>> This is an easy one to fix. Just add:
>>#define ATTENDEES_KEY @"attendees"
>>above your implementation, along with your #import statements, or
you can define an external NSString as such:
>>// myclass.h
>>extern NSString *AttendeesKey;
>>//myclass.m
>>NSString *AttendeesKey = @"attendees";
>>Then in you code, calls like
>>[self setValue:anObject forKey:ATTENDEES_KEY]; // case 1
>>or
>>[self setValue:anObject forKey:AttendeesKey]; // case 2
>>will both get compile-time checked and properly syntax highlighted.
>
>The other version (extern NSString *attendeesKey;) is an external
>variable declaration (not a constant). I don't think that it is a very
>good idea to use external variables (especially in object-oriented
>programming).
>>Much of the Apple code I've seen use #define statements, but
>>I've seen the NSString elsewhere like in Hillegass's book.
>
>At its simplest, the preprocessor #define is for use by the programmer
>on the program source code and for the program source code itself, and
>prior to compilation, while NSString and C declarations are for
run-time
>and external and display use.
Thanks for the input, I appreciate it a lot.
In my case I'm now replacing the external NSString declarations with
#defines, but this means in my implementation files where I place
catergories on my own classes, which I've made to make my sourec easier
and to split up the logic, I have to use use #define again for a few of
my strings. For example I have my own NSDocument subclass and a category
just for handling an NSTask-related methods, both of which have a
#define SOURCE_LIST_COLUMN_ID @"sourceListColumn"
in their implementation files. If I want to change the name of the
NSOutlineView column to which the #define statement referrs at some time
in the future, I'll have to change both, and likely forget one. Is
there any way around this without placing the #define in my NSDocument
subclass's .h file?
Jon
This e-mail and any attachments may contain confidential and
privileged information. If you are not the intended recipient,
please notify the sender immediately by return e-mail, delete this
e-mail and destroy any copies. Any dissemination or use of this
information by a person other than the intended recipient is
unauthorized and may be illegal.
Please be aware that, under the terms of the Freedom of
Information Act 2000, Swansea NHS Trust may be required to make
public the content of any emails or correspondence received. For
further information on Freedom of Information, please refer to the
Swansea NHS Trust website at www.swansea-tr.wales.nhs.uk
_______________________________________________
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