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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 12-23-2010, 07:39 PM   #1 (permalink)
Creator of From A to B
 
fudgie_no1's Avatar
 
Join Date: Nov 2010
Location: Wales
Posts: 45
fudgie_no1 is on a distinguished road
Question NSMutableArray vs Multiple NSStrings, NSIntegers, etc

Evening/morning campers. I'm moving ever closer to the release of my first app but just wanted some oppinions on data storage/use. I'm a bit OCD when it comes to code and like things to be as neat and tidy as possible.

In your oppinion is it better to store values accross several NSStrings, NSSintegers, foats etc or keep them all in one NSMutable array?

for example, instead of having the following 3 items:
NSString * ballhit;
NSInteger batpower;
NSString * cabbage;

I could just have one NSMutablearray and store all of the values in there. would this have any performance issues (with a lot more than 3 items of course)?? I know my code would look a lot neater but Do NSMutable arrays use more memory or cause more performance issues than using multiple NSStrings and NSIntegers?? My gut feeling is that one array would be more efficient but I would like your oppinions and experiences?

Thanks very much.
fudgie_no1 is offline   Reply With Quote
Old 12-23-2010, 07:48 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

1) You can't put NSIntegers into NS*Arrays

2) I fail to see how using the array would clean up your code. Instead of simply doing [self cabbage] you would have to change that to [[self variableList] objectAtIndex:2]. And if you're smart about it, you'll declare constants to keep track of this stuff, so add #define kCabbageIndex 2 in there somewhere, too. That's hardly cleaner.

And good luck to you when you return to this project in 6 months after having worked on other stuff.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-23-2010, 07:49 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by fudgie_no1 View Post
Evening/morning campers. I'm moving ever closer to the release of my first app but just wanted some oppinions on data storage/use. I'm a bit OCD when it comes to code and like things to be as neat and tidy as possible.

In your oppinion is it better to store values accross several NSStrings, NSSintegers, foats etc or keep them all in one NSMutable array?

for example, instead of having the following 3 items:
NSString * ballhit;
NSInteger batpower;
NSString * cabbage;

I could just have one NSMutablearray and store all of the values in there. would this have any performance issues (with a lot more than 3 items of course)?? I know my code would look a lot neater but Do NSMutable arrays use more memory or cause more performance issues than using multiple NSStrings and NSIntegers?? My gut feeling is that one array would be more efficient but I would like your oppinions and experiences?

Thanks very much.
Arrays take a small amount of memory, and add a layer of indirection to fetching their contents. They will be marginally slower than accessing your strings directly.

I also think they will make the code more complex and harder to read than using strings directly.

How would code like this:

Code:
label.text = [stringArray objectAtIndex: 3];
be cleaner and easier to read than code like this:

Code:
label.text = playerName;

One time where I think arrays make sense is if you need to reference them by numeric index, like if you are setting a whole bunch of labels with a sequence of text strings, e.g:

Code:
for (i = 0; i < num_fields; i++)
{
  UILabel* aLabel = [self.view viewWithTag: i];
  aLabel.text = [labelStrings objectAtIndex: i];
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 12-23-2010, 07:51 PM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Arrays are really for storing a collection of the same type of thing. Sure, since they're untyped in Obj-C, you can use them to store one of this, two of the other, etc., but that's not really what they're for. If you're starting to wonder how you should store a group of unrelated things, look into structures or classes. That's what they (structures, classes) are designed to do.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 12-23-2010, 07:59 PM   #5 (permalink)
Creator of From A to B
 
fudgie_no1's Avatar
 
Join Date: Nov 2010
Location: Wales
Posts: 45
fudgie_no1 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Arrays take a small amount of memory, and add a layer of indirection to fetching their contents. They will be marginally slower than accessing your strings directly.

I also think they will make the code more complex and harder to read than using strings directly.

How would code like this:

Code:
label.text = [stringArray objectAtIndex: 3];
be cleaner and easier to read than code like this:

Code:
label.text = playerName;

One time where I think arrays make sense is if you need to reference them by numeric index, like if you are setting a whole bunch of labels with a sequence of text strings, e.g:

Code:
for (i = 0; i < num_fields; i++)
{
  UILabel* aLabel = [self.view viewWithTag: i];
  aLabel.text = [labelStrings objectAtIndex: i];
}
Thank you both for the quick response.

I was thinking more along the lines of the .h file as opposed to the .m file.

Although there would be slightly more code in the .m file, there would only be one line in the .h file as opposed several lines? Am I taking my OCD a bit too far? Am I just making life harder for myself in the long run with no extra benefit? I think that's what Brian is trying to say with the 6 month thing - although I do keep extensive notes as to what's contained in my arrays?
fudgie_no1 is offline   Reply With Quote
Old 12-23-2010, 08:11 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Well, I'm saying a couple of things. You are fooling yourself if you think that masking all of your variables behind a single array is cleaning anything up. The information needs to be somewhere. Whether you are declaring constants for your indexes, or writing down the order of the variables on a sheet of paper, you have to know (and really should document) what order the items are in the array. You've transferred your "clutter" somewhere else, but haven't actually removed it.

Plus, you will have completely disguised your design intent. Your design needs 2 strings and an integer, that's why you have them. Your design does not need an array, otherwise you would already have one. So you are advertising - to other classes, to other people, to yourself - that this class contains only a single array, but that isn't accurate. In 6 months when you've forgotten how this project works, and want to work on it again, you'll have to go digging through whatever documentation you have in order to figure out what is important in this class. I'm not saying that separate variables will completely avoid the relearning curve, but they sure will shorten it.

Apply your OCD towards making your code accurate, and easy to read and understand.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-23-2010, 08:23 PM   #7 (permalink)
Creator of From A to B
 
fudgie_no1's Avatar
 
Join Date: Nov 2010
Location: Wales
Posts: 45
fudgie_no1 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Well, I'm saying a couple of things. You are fooling yourself if you think that masking all of your variables behind a single array is cleaning anything up. The information needs to be somewhere. Whether you are declaring constants for your indexes, or writing down the order of the variables on a sheet of paper, you have to know (and really should document) what order the items are in the array. You've transferred your "clutter" somewhere else, but haven't actually removed it.

Plus, you will have completely disguised your design intent. Your design needs 2 strings and an integer, that's why you have them. Your design does not need an array, otherwise you would already have one. So you are advertising - to other classes, to other people, to yourself - that this class contains only a single array, but that isn't accurate. In 6 months when you've forgotten how this project works, and want to work on it again, you'll have to go digging through whatever documentation you have in order to figure out what is important in this class. I'm not saying that separate variables will completely avoid the relearning curve, but they sure will shorten it.

Apply your OCD towards making your code accurate, and easy to read and understand.
A very fair and valid point, just what I was looking for. I will leave the code as it is and agree that it will only cause me more work. It's the old adage "If it aint broke, don't fix it". Thanks for your time Brian, I can tell that even the thought of me doing this has annoyed you so I know now for sure it's a bad idea as I can clearly see you're a man who knows his stuff. Thanks once again for your advice.
fudgie_no1 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsinteger, nsmutablearrays, nsstring, performance

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: 386
8 members and 378 guests
chemistry, daudrizek, jeroenkeij, Kirkout, PavelMik, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

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