newbie question about constants in C/objC

jspoon
Unregistered
 
Post: #1
I took a class in Java a few years ago. Now I'm sort of messing around with Objective C. Most of the syntax is familiar and I'm figuring some stuff out pretty quickly but I've run into a bit of a dilemma.

I'm working on something that's not so much a game as a demo (in the graphics sense of the word) to learn about the drawing commands. In any case, I have some color custom objects that are used by more than one class. One of these classes has an arbitrary number of instances. I've tried two methods to get everything to work.

First I defined all the colors in one class which created the other objects and passed the (id) of the colors as parameters to their init methods. That's kind of unwieldy when one gets up to five or more colors, makes it hard to type and hard to read. It did have the advantage of being able to change a color everywhere in the program by editing the code once though.

Then I typed out the colors in the init method for each class. This has two disadvantages I can think of: it is abhorrent from the efficiency standpoint (if I have 1000 instances of the object and 5 colors each that makes 5000 color objects) and, worse, if I want to tweak the colors I have to do it separately in each class which is another way to introduce errors.

Ideally, I'd like to define the colors in a file of their own, maybe as constants. I don't know how to do this and make sure each of my classes knows about it, however. So, for example, in any of my classes I could use the term "wallColor" and it would refer to the same color. Could anyone tell me how to do this? I think it's my lack of knowledge of C working against me-writing the code is hard enough but I don't have an instinct for how to break things up in separate files, beyond the little I know about Objective C from this book.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Try something like this:

MyColors.h:
Code:
#import <Cocoa/Cocoa.h>

@interface MyColors : NSObject
{
}

+ (NSColor *)wallColor;

@end

MyColors.m:
Code:
@implementation MyColor

+ (NSColor *)wallColor
{
    static NSColor* color = nil;
    if (color == nil)
    {
        color = [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.5 alpha:1.0] retain];
    }
    return color;
}

@end

then you can use it like this:

Code:
NSColor *thatColor = [MyColors wallColor];

of course, you can add as many as you want to the MyColors class.
Quote this message in a reply
jspoon
Unregistered
 
Post: #3
Ok…follow up confusion. You say I can do this:

Code:
NSColor *thatColor = [MyColors wallColor];


If I understand correctly, since wallColor is a class method I can call it with no instance objects having been made of the class. I haven't worked with class methods on my own and looked over only a few examples so correct me if I'm wrong.

Then the real question: Is 'thatColor' pointing to an NSColor object that was created and retained by the class object myColors? So that any object that called the method could get a pointer to the same NSColor object? From how I'm reading your example it seems like that's the case.

All I'd seen of class methods previously made me think they were more like regular C functions so that whatever object was calling them method would own 'thatColor' and there'd be a separate copy for every call made to MyColors.

I can see how it would be very useful to do the first way.

I hope that all made sense. Thanks for the help.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
exactly right.
Quote this message in a reply
jspoon
Unregistered
 
Post: #5
Ok, weekend is here and I'm back to muddling through this thing. I still don't quite understand your example. I think I understand what you did and why but I don't quite get how that code works.

Code:
+ (NSColor *) wallColor;
{
    static NSColor* color = nil;
    if (color == nil)
    {
        color = [[NSColor colorWithCalibratedRed:0.196
                                           green:0.196
                                            blue:0.196
                                           alpha:1.0] retain];
    }
    return color;
}


The bit I don't get is the first line. I suppose the problem might be just that I don't know what the keyword static does. From what I read, it seems to make a variable available from outside the object. I might be understanding this wrong, because obviously you're making it available to the outside through the method itself.

Then, it seems like by setting color to nil you destroy the link to any color we created previous times the method was called and making a new identical one. Which doesn't make sense. The code seems to be intended to check if it already exists but I don't see how it works. If it's NOT intended to check and it just passes the color back, the if statement is just redundant.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
A static variable's scope is "forever", not "the call-time of the function". The assignment with nil will only happen once; the comparison to nil will happen each time the function is called.

A static variable outside a function is available to all functions in the file, a static variable within a function is available only to that particular function.
Quote this message in a reply
Moderator
Posts: 770
Joined: 2003.04
Post: #7
When you declare something as static, the assignment in the declaration happens only once, so in this case:

PHP Code:
static NSColorcolor nil


color is assigned nil ONLY the first time, from then on it keeps whatever value it previously had. You can think of color as being a global accesible only from within the wallColor method.

EDIT: Uh... OSC beat me to this post... Sneaky
Quote this message in a reply
jspoon
Unregistered
 
Post: #8
Ok, again that's useful knowledge, not contained in the brief tutorials of C that I perused before embarking on this Objective C adventure. I suspected that was what it might mean simply by elimination but wanted it explained in so many words in case my basic assumptions were wrong. Thanks again, both of you.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Newbie question: Cocoa and Carbon - which for what and how to get started? BliTz 3 3,121 Jul 27, 2006 10:07 PM
Last Post: OneSadCookie
  Newbie question about libraries and unix stuff... WhatMeWorry 8 3,889 Jun 25, 2005 04:17 PM
Last Post: OneSadCookie
  ObjC/c code error with lists ededed 5 3,231 Jan 31, 2003 06:56 AM
Last Post: w_reade