Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Simple Objective C initialization

As an assembly language and C programmer new to Objective C, I occasionally need an array of constants. This is easily created in C:

int list[5] = {1,2,3,4,5};

Is there a simple equivalent in Objective C?

Thanks for any help.
Post edited by Pitfall on

Replies

  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    You can of course use C-style arrays in Objective-C. I do that with constant arrays, where I build them at compile time and don't modify the contents, similar to the example you showed. You can put constant pointers to objects in them, of which the most likely, or maybe only, kind is NSString*

    I also sometimes will use an NSArray or NSDictionary for the same purpose. I'll have an accessor that builds the NSArray/NSDictionary lazily. These objects have some advantages over C-style arrays.
  • PitfallPitfall Posts: 7New Users
    Pitfall here again... Armed with the comment that it should work I made some progress. I was trying to init the constant array in the interface declaration where I establish my globals. It doesn't let me populate the array there.

    On the other hand, the C syntax works perfectly inside the implementation declaration, when used locally within a method.

    I could use NSArray, but it seemed overkill to alloc a memory region, initWithObjects, and then have to purge - all to make a 10 byte table.

    I guess it makes sense that Objective C won't let me embed a code-like construct in the declaration area. But being new to the language that didn't jump out at me.

    Thanks for the replies.
  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    A constant C-style array needs to be a file scope static or a function local variable. Normally a file scope static would be the way to do this. The static initialization can't go in a class declaration.

    I wouldn't worry about overkill in generating an NSArray, unless you have proof that this will be a problem. Your most precious resource is your time. Development time spent tracking down bugs in use of C-Style arrays is wasted if those bugs can't exist if you use an NSArray.
  • squashsquash Posts: 1New Users
    ok so if i stored a static array of integers in a class in java, how would i do this in objective c. for example:
    public class MyListClass
    {
    public static final int list[] = {1,2,3,4,5};
    }

    which would be accessed by simply MyListClass.list.
  • exorcyzeexorcyze Posts: 496Registered Users

    // in your .h
    @interface MyListClass : NSObject {
    NSArray *list;
    }
    @property (nonatomic, retain) NSArray *list;

    // in your .m
    @synthesize list;
    - (void) dealloc {
    [list release];
    [super dealloc];
    }
    // whereever you set it up ( init most likely ):
    NSArray *temp = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];
    self.list = temp;
    [temp release];
    My Apps on AppStore : gScale (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
  • Pedro ValentiniPedro Valentini Posts: 34Registered Users
    Realy a lof of code....

    NSArray *list = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:5], nil];

    We need to use ruby in iphone...

    In RUBY is:

    list = [1,2,3,4,5]
  • MobileGeorgeMobileGeorge Posts: 1New Users
    exorcyze;46337 said:

    // in your .h
    @interface MyListClass : NSObject {
    NSArray *list;
    }
    @property (nonatomic, retain) NSArray *list;

    // in your .m
    @synthesize list;
    - (void) dealloc {
    [list release];
    [super dealloc];
    }
    // whereever you set it up ( init most likely ):
    NSArray *temp = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];
    self.list = temp;
    [temp release];
    There is no need to create an array of NSNumber objects if you only want integer values.


    // in your .h
    @interface MyListClass : NSObject {
    const int *list;
    }
    @end

    // in your .m
    @implementation MyListClass
    static const int constList[5] = {1, 2, 3, 4, 5};
    ...and in init:
    list = constList;
    @end
  • yoichiyoichi Posts: 5New Users
    I had a similar problem as I am new to Objective-C. As others are saying, it is the same as the normal C but one difference. That is, you can use them as you expect, but not with Objective-C specific things.

    I figured out that C variable initializers can be used in the class interface, too, but not between @interface and the {...} or within the "{...}".

    Otherwise they can be declared as you do with normal C header files. They can be also declared outside the block surrounded by an @interface and the paring @end.

    E.g. This is OK and the emptyCoordinate can be used in the @implementation:

    @interface ...
    {
    ...
    }

    static const CLLocationCoordinate2D emptyCoordinate = {-1.0, -1.0};

    @property ...
    ...
    ...
    @end

    @implementation ...

    @synthesize myCoord;

    - (void)viewDidLoad
    {
    myCoord = emptyCoordinate;
    ...
    }

    @end


    Or:

    static const CLLocationCoordinate2D emptyCoordinate = {-1.0, -1.0};
    @interface ...
    ...
    @end


    Or:

    - (void)viewDidLoad
    {
    ...
    CLLocationCoordinate2D emptyLoc = {-1.0, -1.0};
    ...
    }
Sign In or Register to comment.