Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 27, 2010, 03:35 AM   #1
andyiapan
macrumors newbie
 
Join Date: Feb 2010
searching number in NSarray

if i got number
123456
456786
232133
124456
143445

if i input 1,
it will search and display out 123456,124456,143445
if i input 12,
it will search and display out 123456,124456
if i keep on input 123,
it will search and display out 123456

how can i search like this?
andyiapan is offline   0 Reply With Quote
Old Nov 27, 2010, 03:51 AM   #2
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
Easy to be hard

I'm guessing you're asking for some low-level algorithm that compares your input to the array and returns the results you describe.

I'd take a different tack and use the instance methods in the NSArray class. The idea is that the Cocoa designers have already thought of most situations programmers would need and have created the appropriate methods.

Take a look at:
http://developer.apple.com/library/m...s/NSArray.html

probably start with one of the many indexOfObjectBlahBlah methods to see if they do what you want.
__________________
iMac 21.5", 3.06GHz, 4 GB, 2 TB HD.
iPod Touch 3G.
PatrickCocoa is offline   0 Reply With Quote
Old Nov 27, 2010, 11:59 AM   #3
andyiapan
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
Thanks ~~~

Quote:
Originally Posted by PatrickCocoa View Post
I'm guessing you're asking for some low-level algorithm that compares your input to the array and returns the results you describe.

I'd take a different tack and use the instance methods in the NSArray class. The idea is that the Cocoa designers have already thought of most situations programmers would need and have created the appropriate methods.

Take a look at:
http://developer.apple.com/library/m...s/NSArray.html

probably start with one of the many indexOfObjectBlahBlah methods to see if they do what you want.
andyiapan is offline   0 Reply With Quote
Old Dec 2, 2010, 04:33 AM   #4
andyiapan
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
i can't found out using compare,
because of array value is not exactly same as compare value,

how can i use NSarray and integer value compare in that way><

Many Thanks

1 = 12344
1 = 1234455
1 = 13433
1 = 13333

13 = 13333
13 = 13433

134 = 13433
andyiapan is offline   0 Reply With Quote
Old Dec 2, 2010, 04:46 AM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Don't compare them as integers. Compare them as strings.
robbieduncan is offline   0 Reply With Quote
Old Dec 2, 2010, 05:03 AM   #6
andyiapan
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
Quote:
Originally Posted by robbieduncan View Post
Don't compare them as integers. Compare them as strings.
if i compare them with string, will i compare
13 = 451322 because of 13 is same as 45(13)22.
andyiapan is offline   0 Reply With Quote
Old Dec 2, 2010, 05:05 AM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by andyiapan View Post
if i compare them with string, will i compare
13 = 451322 because of 13 is same as 45(13)22.
That all depends on how you compare them. NSString has a very obvious method for checking if one string starts with another.
robbieduncan is offline   0 Reply With Quote
Old Dec 2, 2010, 04:50 PM   #8
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
Swim in the Cocoa Sea

I agree with robbiedunan, who, as near as I can tell, is always right about everything.

It appears that your (andyiapan) issue is that you're:
1. Not familiar with Cocoa;
2. Not familiar with using pre-existing methods.

My advice is to use this current question as an opportunity to learn - open the NSArray documentation and just read through the various methods for 1/2 hour or an hour or until you get bored. Then do that a couple of times a day (expanding past NSArray to NSString or other classes) for a week or two.

Then the next time you have a question, instead of asking "what algorithm can I implement to get my answer", you'll ask "what methods are available that do what I want or almost what I want"?
__________________
iMac 21.5", 3.06GHz, 4 GB, 2 TB HD.
iPod Touch 3G.
PatrickCocoa is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
NSArray to NSMutableArray larswik Mac Programming 3 Feb 19, 2013 11:10 PM
Adding NSNumbers in a NSArray fstigre iPhone/iPad Programming 4 Nov 11, 2012 12:46 PM
NSArray LastObject larswik iPhone/iPad Programming 4 Nov 5, 2012 08:01 PM
What is the difference between NSArray and NSMutableArray anandkumar45 iPhone Tips, Help and Troubleshooting 1 Nov 2, 2012 06:09 AM
(NSArray)writeToFile: issue mdeh Mac Programming 3 Jul 15, 2012 07:38 PM

Forum Jump

All times are GMT -5. The time now is 10:08 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC