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

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.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 07-24-2008, 12:03 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2008
Location: Minneapolis, MN
Posts: 208
Default What is the equivalent to Arrays that other programming languages use?

I'm just looking to do an array that has a few BOOLs. The NS[Mutable]Array doesn't seem to do the trick, it only takes objects and I don't want to make a class that just holds a BOOL.

For example, here's how I would do it in PHP:

$myArray = array(true, false, true, false);

Then to access the members, I could simply...

$value = $myArray[0];
$value2 = $myArray[1];

Or in Javascript... myArray = new Array(); myArray.push(true); myArray.push(false); someValue = myArray[0];

I could give more examples in other languages, but hopefully you get the point. What is the best way to implement a very basic array in Objective-C?

Cheers!
Scuba is offline   Reply With Quote
Old 07-24-2008, 12:12 AM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 802
Default

Objective-C supports normal C so you could do a BOOL MyArray[10] or use malloc and index in.
scottiphone is offline   Reply With Quote
Old 07-24-2008, 02:56 AM   #3 (permalink)
New Member
 
Join Date: Jul 2008
Location: Bern, Switzerland
Posts: 9
Default

Hi scuba

I think what you're looking for is a dynamic array. Since dynamic arrays work with objects (they must be moved around via pointers), you have to convert your BOOL's in an object. You can do this e.g. with NSNumber.
Code:
NSNumber* myvalue = [NSNumber numberWithBool: TRUE];
If you compare Objective-C with javascript you have to be aware, that in Objective-c you're very close to the hardware. In js everything is boxed in an object. It's all simulated in a virtual machine and a garbage collector's there collecting all the objects you lost. Here you're dealing with real memory and you need to take care yourself of everything.
Welcome to the real world

Like Scotty mentioned, you could use C-Arrays. But thats not very comfortable in my opinion because you can't dynamically extend the array.
If you need just a fixed size array, do this:
Code:
BOOL myValues[] = {TRUE, FALSE, TRUE};
Daniel
daniel is offline   Reply With Quote
Old 07-24-2008, 01:04 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2008
Location: Minneapolis, MN
Posts: 208
Default

Works great, thanks for the help guys =)
Scuba is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, objective-c

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
» Online Users: 263
19 members and 244 guests
14DEV, @sandris, ADY, ArtieFufkin10, ckgni, Dani77, HemiMG, IphoneSdk, jakerocheleau, JasonR, jimbo, MACralik, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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