Set objectAtIndex to be the integer in an NSString? - iphone

I've got this:
if ([[currentPlayer rankicon] isEqualToString:#"0"]) {
self.casteImage.image = [casteImages objectAtIndex:0];
}
Which works. However, I have some 16 images to assign, and since the NSString and the index id are the same, I'd like to find a more efficient way to do this. I've also tried:
NSInteger i = [[currentPlayer rankicon] integerValue];
self.casteImage.image = [casteImages objectAtIndex:[i]];
But that doesn't work. Could be a bug elsewhere in my code(or some syntax error in the above), of course, but was wondering if I'm anywhere near the right track.

Remove [] around iin the second line.

Related

sticky for loop?

I am having a problem with a for loop. Most probably it is a silly mistake somewhere that i cant catch. I have a fast enumeration loop that looks like this:
for (NSNumber *number in sums) {
int n = [number intValue];
NSArray *array = [self getResultForTarget:n];
for (NSNumber *num in array) {
NSLog(#"%i",[num intValue]);
}
}
the value for [num intValue] is the same for every enumeration of for (NSNumber *number in sums) when it is impossible for it to be the same. It is giving the value it calculates in the first enumeration. The method getResultForTarget: cannot return the same result for the different (int) arguments it is taking.... what might be going wrong???
p.s. i tried to manually enter the arguments of sums in the method and it returned correct results.
If you manually entered sums and got the right results there is a good chance that the issue is that at run time sums does not contain what you think it contains.
ok i got it to work... apparently i have an initialized array that should be emptied before each call ...

why does '[self.pickerSubArray indexOfObject:self.txtSubCategory.text]' return '2147483647'?

why does '[self.pickerSubArray indexOfObject:self.txtSubCategory.text]' return '2147483647'; while the same string value argument '[self.pickerSubArray indexOfObject:#"Mark"]' brings up 4, as desired?
The Apple docs for NSArray (which I assume your object is, based on the name) say that indexOfObject: returns NSNotFound if the object does not match any in the array. NSNotFound is itself defined as NSIntegerMax which, as others have pointed out, is the value that you are getting back.
indexOfObject: uses isEqual: to compare the items, so in theory if the text is the same then it should be working. Perhaps the text is actually different in some way that you haven't noticed, such as case ("Mark" vs. "mark") or extra padding ("Mark" vs. "Mark ").
indexOfObject: returns NSNotFound if it can't find your exact object. NSNotFound is defined as NSIntegerMax, which is 2147483647.
Why is it doing that? I'm pretty sure indexOfObject: tests for an identical object, not an object with identical content.
e.g.
NSString *mark1 = [NSString stringWithString:#"Mark"];
NSString *mark2 = [NSString stringWithString:#"Mark"];
mark1 is not necissarily equal to mark2, because they're two different objects.
NSString *mark1 = [NSString stringWithObject:#"Mark"];
NSString *mark2 = mark1;
mark1 is equal to mark2;
BUT! Since the compiler is trying to minimize the memory footprint, it turns all literal strings in your code into one constant string. Which is why [[NSArray arrayWithObject:#"Mark"] indexOfObject:#"Mark"] works, but [[NSArray arrayWithObject:#"Mark"] indexOfObject:textField.text] doesn't work even if the text in textField.text is "Mark".
How do you fix it... well, indexOfObject: from the docs it looks like indexOfObject: is based on isEqual: so you should test if [self.txtSubCategory.text isEqual:#"Mark"]. to rule out the wrong value or a disconnected outlet, etc. After that, you may have to refactor to not use indexOfObject:
Just a guess about the number origin - it's a bad integer conversion. It was very probably meant to return -1.
That kindof leads me to believe that you might have found some badness in underlying libraries/languages.

What is the fastest routine to convert NSArrays to C-style (double) arrays?

I have 2 NSArray (Mutable, actually) that I am trying to convert to a C-style double array for a c routine i am passing them to.
Here is my Objective-C routine:
NSMutableDictionary *childDictionary = [myParentDictionary objectForKey:resort_code];
latitudeArray = [childDictionary objectForKey:#"lat"];
longitudeArray = [childDictionary objectForKey:#"lon"];
int nvert = [latitudeArray count];
double laArray[nvert];
double loArray[nvert];
for(int i=0; i<nvert; i++) {
double dLat = [[latitudeArray objectAtIndex:i]doubleValue];
double dLon = [[longitudeArray objectAtIndex:i]doubleValue];
laArray[i] = dLat;
loArray[i] = dLon;
}
This takes upwards of 3-8 seconds on the 3G iPhone (instantaneous on the simulator -- yet another reason to test on the device )
is there faster way? I have to end up with laArray[i] and loArray[i] as c-style arrays of doubles.
(to expand on the question for the benefit of a commenter):
Each array consists of #"38.448745" (lat) and #"-122.9847684" (lon) style content. I do this cos to be pushed onto an NSArray, the lat and lon need to be objects. I simply used:
[latitudeArray addObject:[NSString stringWithFormat: #"%.10f",dlat]];
[longitudeArray addObject:[NSString stringWithFormat: #"%.10f",dlon]];
I suppose I could change that to:
[latitudeArray addObject:[NSNumber numberWithDouble: #"%.10f",dlat]];
[longitudeArray addObject:[NSNumber numberWithDouble: #"%.10f",dlon]];
...which may reduce the conversion time of
double dLat = [[latitudeArray objectAtIndex:i]doubleValue];
but wouldn't I still need that exact line to convert from NSString to double? It just may work faster?
thx
dlat is a double, right?
So instead of:
[latitudeArray addObject:[NSString stringWithFormat: #"%.10f",dlat]];
Do:
[latitudeArray addObject:[NSNumber numberWithDouble:dlat]];
They both respond to doubleValue but the NSNumber should not have to do any string parsing since it's stored as a numeric value already. And you never have to go to a string at all.
I suspect you have an array of strings like #"213.12385" that need to be parsed and converted when you call doubleValue on them. If that is where the issue is, the C arrays have nothing to with this.
Only thing I would add here is to throw Shark on this and see where it's spending it's time. If it's spending time in doubleValue find a different way to parse the strings with preprocessing in background or something. If it's in objectAtIndex: perhaps fast enumeration would help. If it's somewhere else entirely then you know it's not this snippet that's slow.
For the general case of converting an NSArray to a C array, you can use getObjects:. In this case, though, want you actually want is not to convert the NSArray, but to derive an array of doubles from an NSArray of some unspecified object type.
One obvious way to speed things up would be fast enumeration rather than sending a message to get the object for each iteration of the loop. I suspect the real solution, though, is outside your algorithm. The slowness probably comes from transforming whatever objects the array contains into doubles, in which case you'll need to find a way around that — maybe store doubles all along, maybe do the conversion in the background, etc. If you're creating the arrays yourself and there isn't some compelling reason for the objects to be strings, you should use NSNumbers instead. Those should be quite a bit faster.
The best solution is probably to make sure those values never end up in an NSArray as NSString values. I would attack this at the source.
So you edited your question and added that you are actually building those arrays. So why not use native arrays of doubles or floats from the start? I usually recommend against this but in your case it sounds like there is a huge performance gain.
Possibly using fast iteration, but I doubt that will really speed up your loop.

objective-c converting strings to usable numbers

I have strings that look about like this:
stringA = #"29.88";
stringB = #"2564";
stringC = #"12";
stringD = #"-2";
what is the best way to convert them so they can all be used in the same mathmatical formula?? that includes add, subtract.multiply,divide etc
Probably floatValue (as it appears you want floating-point values), though integerValue may also be of use (both are instance methods of NSString).
[stringA doubleValue]
These are all wrong, because they don't handle errors well. You really want an NSNumberFormatter.
If you have the string #"abc" and try to use intValue or floatValue on it, you'll get 0.0, which is obviously incorrect. If you parse it with an NSNumberFormatter, you'll get nil, which is very easy to distinguish from an NSNumber (which is what would be returned if it was able to parse a number).
Assuming that you have NSString variables.
NSString *stringA = #"29.88";
NSString *stringB = #"2564";
NSString *stringC = #"12";
NSString *stringD = #"-2";
suppose, you want to convert a string value to float value, use following statement.
float x=[stringA floatValue];
suppose, you want to convert a string value to integer value, use following statement.
NSInteger y = [stringC intValue];
Hope, it helps to you.

How do I pull an integer out of a NSDictionary and put it in an integer?

I've got an NSDictionary that was initialized with a plist that, among other things, contained count followed by 32 and I want to get at the value for count.
How?
I know how to get it into an object via
[dictionaryObjectName objectForKey:#"count"]
But to me, the value I'm obtaining is not an object.
If I have to specify an object, what would be the best one to use (FYI the value will always be unsigned) and I need to get it into a true int.
Do I do something like
NSNumber *num = [dictionaryObjectName objectForKey:#"count"];
int theValue = [num intValue];
[num release];
Is the release on num a good thing to do since this for an iPhone with no garbage collector?
And nicer form is:
int theValue = [[dictionaryObjectName objectForKey:#"count"] intValue];
EDIT
Since I see that people still arrive to this page, let's clarify that these days you simply do
int theValue = [dictionaryObjectName[#"count"] intValue];
Yes, you pull it out as an NSNumber, then grab the int(eger)Value but there's no need to release it. You didn't retain, alloc, or copy the number, so you don't have to worry about releasing it.