Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Calcuccino Programmers' Calculator
($2.99)

DataFon(Build Apps on Windows)
(free)

Infinote Pinboard for Todos and Notes
(free)

picplz
(free)

poG
($2.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2008, 06:15 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 7
Default 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.
Pitfall is offline   Reply With Quote
Old 11-11-2008, 06:29 PM   #2 (permalink)
New Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Default

It's the same I believe.
RickMaddy is offline   Reply With Quote
Old 11-11-2008, 06:47 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

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.
PhoneyDeveloper is offline   Reply With Quote
Old 11-11-2008, 07:44 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 7
Default Zeroing in on the difference...

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.
Pitfall is offline   Reply With Quote
Old 11-11-2008, 09:56 PM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

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.
PhoneyDeveloper is offline   Reply With Quote
Old 01-31-2009, 11:48 PM   #6 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 1
Default

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:
Code:
public class MyListClass
{
	public static final int list[] = {1,2,3,4,5};
}
which would be accessed by simply MyListClass.list.
squash is offline   Reply With Quote
Old 02-01-2009, 08:54 AM   #7 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

Code:
// 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), eMaze, eMaze Lite, eTimesheet

Last edited by exorcyze; 02-01-2009 at 08:56 AM.
exorcyze is offline   Reply With Quote
Old 02-06-2009, 06:50 AM   #8 (permalink)
Registered Member
 
Pedro Valentini's Avatar
 
Join Date: Oct 2008
Location: Rio de Janeiro, Brazil
Posts: 25
Default

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]
__________________
tecnalta.net
Pedro Valentini is offline   Reply With Quote
Old 01-12-2010, 08:52 PM   #9 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 1
Default

Quote:
Originally Posted by exorcyze View Post
Code:
// 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.

Code:
// 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

Last edited by MobileGeorge; 01-12-2010 at 09:13 PM.
MobileGeorge is offline   Reply With Quote
Old 02-02-2010, 09:27 AM   #10 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 5
Default

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:

Code:
@interface ...
{
 ...
}

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

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

@implementation ...

@synthesize myCoord;

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

@end
Or:

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

Code:
- (void)viewDidLoad
{
   ...
   CLLocationCoordinate2D emptyLoc = {-1.0, -1.0};
   ...
}
yoichi is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 51,374
Threads: 52,830
Posts: 225,477
Top Poster: BrianSlick (3,576)
Welcome to our newest member, bob8caldwell
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:27 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0