PDA

View Full Version : searching number in NSarray




andyiapan
Nov 27, 2010, 02:35 AM
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?



PatrickCocoa
Nov 27, 2010, 02:51 AM
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/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

probably start with one of the many indexOfObjectBlahBlah methods to see if they do what you want.

andyiapan
Nov 27, 2010, 10:59 AM
Thanks ~~~;)

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/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

probably start with one of the many indexOfObjectBlahBlah methods to see if they do what you want.

andyiapan
Dec 2, 2010, 03:33 AM
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

robbieduncan
Dec 2, 2010, 03:46 AM
Don't compare them as integers. Compare them as strings.

andyiapan
Dec 2, 2010, 04:03 AM
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.

robbieduncan
Dec 2, 2010, 04:05 AM
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 (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/hasPrefix:) for checking if one string starts with another.

PatrickCocoa
Dec 2, 2010, 03:50 PM
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"?