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 07-20-2011, 01:27 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Arrow NSMutable Array Search Problem

Hello Everyone!

I currently loaded "car" objects from an SQL file using the tutorial from dBlog. Now that I have all of my car objects loaded into an NSMutable array. I wish to make a method to return a specific object from the NSMutable array based on two criteria. The first criteria is the type of the vehicle, and the other being the year of the vehicle which are the properties of the object.
I know that you can return an object based on index, but this would be too confusing.

How would I be able to return a specific object based on these criteria?

Note: the objects in the NSMutable array will not have the same vehicle type and year. However there are several vehicles of the same type, and several vehicles of the same year.

Thanks for your help.
Mystery is offline   Reply With Quote
Old 07-20-2011, 01:39 PM   #2 (permalink)
Awesome
 
Esko2300's Avatar
 
Join Date: Jun 2009
Location: New York, N.Y.
Posts: 389
Esko2300 is on a distinguished road
Default

Just search the indicies for the name of the car and the year of the model.
You can use a collection for loop(dont know if its the right term but its what i call them) and then just search if both the model and the year are equal to the search terms.

Code:
int index = 0;
int foundAtIndex;
for(CarModel *car in carArray){//this will search every index
    if([car.model isEqualToString:@"Model"] && car.year = yearSearchedFor){
         foundAtIndex = index;
         break;
    }
    else
         index++;
}

Last edited by Esko2300; 07-20-2011 at 01:43 PM.
Esko2300 is offline   Reply With Quote
Old 07-20-2011, 10:37 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 Mystery View Post
Hello Everyone!

I currently loaded "car" objects from an SQL file using the tutorial from dBlog. Now that I have all of my car objects loaded into an NSMutable array. I wish to make a method to return a specific object from the NSMutable array based on two criteria. The first criteria is the type of the vehicle, and the other being the year of the vehicle which are the properties of the object.
I know that you can return an object based on index, but this would be too confusing.

How would I be able to return a specific object based on these criteria?

Note: the objects in the NSMutable array will not have the same vehicle type and year. However there are several vehicles of the same type, and several vehicles of the same year.

Thanks for your help.
This is a bad way to use SQL. You are forcing the system to load all your car objects from disk into memory, and then do brute-force string compares on all the types. You will maximize the amount of memory required, AND end up with a very slow search, since the app will always need to read all of your records into memory. On a memory-starved iOS device that will fail for a moderately large table.

You should set up type and year to be keys in your table, and then use an SQL select statement to find the objects that matches your criteria. That way SQL can use a hash function to find the keys, without loading any of the records into memory.

I haven't used SQL directly from iOS yet, so I can't give you the specifics. I've used Core Data, which offers some very useful features on top of SQL.
__________________
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 07-21-2011, 12:54 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
This is a bad way to use SQL. You are forcing the system to load all your car objects from disk into memory, and then do brute-force string compares on all the types. You will maximize the amount of memory required, AND end up with a very slow search, since the app will always need to read all of your records into memory. On a memory-starved iOS device that will fail for a moderately large table.

You should set up type and year to be keys in your table, and then use an SQL select statement to find the objects that matches your criteria. That way SQL can use a hash function to find the keys, without loading any of the records into memory.

I haven't used SQL directly from iOS yet, so I can't give you the specifics. I've used Core Data, which offers some very useful features on top of SQL.
What you are saying makes perfect sense. However, I'm new to SQL and fairly new to obj c so I've been trying to find tutorials/readings on how to read data from an SQL database. The only two tutorials that I have found first put the data in an NSMutablearray and then loaded the data onto a table view by going through all of the objects in the NSMutablearray. I am not trying to put the data into a table view. I'm simply trying to display the properties of a car in labels.

I do want to do it your way, but i'm not sure how to set up the year and type as keys or how to use a hash function for SQL. I've been looking around for readings but I don't seem to find any. I'm trying to use the year and type to find the specific car and all the data that comes along with it such as wheel size, engine size, number of pistons etc. I'm trying to make it as easy as label.text = Car.engineSize, label2.text = Car.pisons.....

Are there any readings, articles or tutorials you know of that can allow me to do read SQL data more efficiently?

Tutorials I've been using so far:
SQLite Tutorial ? Selecting Data
iPhone SDK Tutorial: Reading data from a SQLite Database | dBlog.com.au

Last edited by Mystery; 07-21-2011 at 12:57 PM.
Mystery is offline   Reply With Quote
Old 07-21-2011, 01:08 PM   #5 (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

"New to Obj-C" + "New to SQL" = "Bad Combination"

Worry about basics first. Start with plists, graduate to Core Data if you need to. No point in forcing SQL right now. Or possibly, ever.
__________________
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 07-21-2011, 02:58 PM   #6 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 16
Mystery is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
"New to Obj-C" + "New to SQL" = "Bad Combination"

Worry about basics first. Start with plists, graduate to Core Data if you need to. No point in forcing SQL right now. Or possibly, ever.
Sorry I guess new to objective c wasn't completely accurate. I have worked with plists and core data before. I'm just new to SQL.
Mystery is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutable array, object, search

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: 408
15 members and 393 guests
7twenty7, blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, n00b, nyoe, pungs, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

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