assigning NSNumber object to NSString object won't cause any error or warning - objective-c

I've put NSNumber object into NSDictionary object, and then pop and assign it to a variable declared as NSString instance.
NSString *test;
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1000] forKey:#"key"];
test = [dict valueForKey:#"key"];
NSLog(#"%#, type of %#", test, NSStringFromClass([test class]));
return 0;
After running the above code, I've found that the type of test, declared as (NSString *) is __NSCFNumber. Why did it happen and why compiler did give no warning or errors?
Do I have to NSString constructor, such as NSStringWithFormat..., in order to keep test as NSString's instance?

The reason this did not fail or warn you is because valueForKey: returns an object of type id which you can assign to any Objective-C type. You need to be aware of what type of value you are getting back from your collections to safely use them. In this case you know it contains an NSNumber so you should expect an NSNumber and if you need a string you will need to do the proper conversions.
NSNumber *test = [dict valueForKey:#"key"];
NSLog(#"%#, type of %#", test, NSStringFromClass([test class]));
//If you need a string
NSString *testStr = [test stringValue];

Related

Objective C should these two return the same value

What is the difference of notificationIdToCancle1 and notificationIdToCancle2 in the code below:
NSDictionary* parameters = (NSDictionary* )parametersObject;
if (parameters != nil) {
NSString* notificationIdToCancle1 = (NSString* )[parameters objectForKey:#"id"];
NSString *notificationIdToCancle2 = [NSString stringWithFormat:#"%#",[parameters valueForKey:#"id"]];
}
Shouldn't both contain the same value?
NSString* notificationIdToCancle1 = (NSString* )[parameters objectForKey:#"id"];
This line is grabbing the object in the dictionary, casting it to an NSString whether it is or isn't.
[NSString* stringWithFormat:#"%#",[parameters valueForKey:#"id"]];
This line, I think you have a mistake, you probably don't want the first '*'. You also probably want to call 'objectForKey:' rather than 'valueForKey:'. objectForKey: will return the entry in the dictionary, while valueForKey: will use Key Value Coding to return a value.
So:
[NSString stringWithFormat:#"%#",[parameters objectForKey:#"id"]];
This takes the object in the dictionary, runs 'description' on it which returns an NSString instance. So you definitely get an NSString instance out of it.

-[__NSArrayI floatValue]: unrecognized selector sent to instance

_data = [NSMutableArray new];
NSNumber *value1 = [NSNumber numberWithFloat: 5.0f];
[_data setValue:value1 forKey:#"foothold"];
NSNumber *value2 = [_data valueForKey:#"foothold"];
NSLog(#"a foothold %f ",[value2 floatValue]);//error here
It's strange, but I don't see my error...
You have a few problems.
_data is mistakenly an NSMutableArray instead of an NSMutableDictionary.
Don't use setValue:forKey: and valueForKey: unless you mean to do KVC.
Use modern syntax (it's easier and it avoid issue #2).
Updated code:
_data = [NSMutableDictionary dictionary];
NSNumber *value1 = #5.0;
_data[#"foothold"] = value1;
NSNumber *value2 = _data[#"foothold"];
NSLog(#"a foothold %f ",[value2 floatValue]);
When you get an "unrecognized selector sent to instance" error, you'll be given the name of the method you're attempting to call (in this case floatValue) as well as the type of object you're calling it on (NSArray here).
So in this case, despite value2 being declared as an NSNumber, the value returned from [_data valueForKey:#"foothold"]; is an NSArray, which does not respond to the floatValue selector.
I'm surprised you weren't given other warnings. Given the syntax, it looks like you should be using an NSMutableDictionary rather than an array. In which case, try this:
NSMutableDictionary *_data = [NSMutableDictionary dictionary];
NSNumber *value1 = #5.0f;
_data[#"foothold"] = value1;
NSNumber *value2 = _data[#"foothold"];
NSLog(#"a foothold %f", [value2 floatValue]);

Does iOS coerce NSStrings into NSNumbers?

Subtitle: why does this code work? It seems to allow comparison of NSNumber with NSString types via some sort of coercion. I'm trying to compare a selection from a UISegmentedControl with a previously stored value.
- (IBAction)minSegmentedControlChanged:(id)sender // MINIMUM value
{
UISegmentedControl *s1 = (UISegmentedControl *)sender;
NSMutableArray *pD = [[GameData gameData].curData valueForKey:#"persData"];
// Must make sure max >= min
NSNumber *currMax = [pD objectAtIndex:1];
NSLog(#"%#", [currMax class]); // __NSCFString ?!
int ss1 = s1.selectedSegmentIndex;
NSNumber *SS1 = [NSNumber numberWithInt:ss1 + 2];
if (SS1 >= currMax) SS1 = currMax;
NSLog(#"%#", SS1); // Answer is correct, appears to be an integer
NSLog(#"%#", [SS1 class]); // __NSCFString ?!
[pD replaceObjectAtIndex:0
withObject:SS1];
[[GameData gameData].curData setObject:pD
forKey:#"persData"];
NSLog(#"%#", [[GameData gameData].curData valueForKey:#"persData"]);
}
I am particularly asking about:
NSNumber *currMax = [pD objectAtIndex:1];
NSLog(#"%#", [currMax class]); // __NSCFString ?!
which seems to return a string for a number. [[GameData gameData].curData valueForKey:#"persData"]; is initialized as follows:
_persData = [[NSMutableArray alloc] initWithObjects:#"2", #"8", #"TWL", #"0", #"0", nil];
which is a string at element 1. So why can I ask it for an NSNumber, which reports that it is actually a __NSCFString on which I can do arithmetic comparisons on? I've only been at objective-c for a few months but this seems strange.
Okay, let's walk through this one step at a time.
First of all, all of the elements in _persData are strings. Period. NSString is a class cluster, so the concrete classes of the various instances you inquire about may look weird, but that's to support toll-free bridging and other magic that's not relevant to this discussion.
NSNumber *currMax = [pD objectAtIndex:1];
This line is incorrect. You might think there's some sort of coercion going on, but actually you're just assigning an NSString * to an NSNumber *. Which is wrong, and will explode in your face at the earliest convenience. It so happens that objectAtIndex: returns an id, which is stripped of type information, so the compiler is trusting you to store it in the right kind of pointer, but that's not enforced until you try to send a message to it.
if (SS1 >= currMax) SS1 = currMax;
This is an extremely wily comparison. SS1 is most certainly an NSNumber, but currMax is an NSString. But we're not comparing the values of those objects. To do that, we'd use the compare: method. Instead, we're comparing them as pointers, looking only at their addresses in memory. By some accident of implementation, SS1 seems to always reside at a higher address than currMax.
If all of the foregoing is true, then SS1 is always of type NSString after the above line is executed, which explains why this line:
NSLog(#"%#", [SS1 class]);
Always indicates that SS1 is a string.
You actually initialize the NSMutableArray with NSString, not number.
When you pull object out of NSMutableArray with objectAtIndex, the return type of the function is id, which it will blindly cast to NSNumber, while the actual object is NSString.
It seems that the SS1 >= currMax statement is comparing the addresses of the objects instead of their values.
You can test it out with this snippet of code:
NSNumber *a = [[NSNumber alloc] initWithUnsignedInt: 34];
NSNumber *b = [[NSNumber alloc] initWithUnsignedInt: 3];
NSNumber *c = [[NSNumber alloc] initWithUnsignedInt: 34];
NSNumber *d = [[NSNumber alloc] initWithUnsignedInt: 234];
NSLog(#"%p %p %p %p %d %d", a, b, c, d, a >= b, c >= d);

Going crazy with UITextField

I'm literally going crazy whit these six rows of code.
NB: nome and prezzo are 2 textFields
NSString *itemName = (NSString *) [rowVals objectForKey:#"name"];
NSString *itemPrice = (NSString *) [rowVals objectForKey:#"price"];
nome.text = itemName;
nome.userInteractionEnabled = YES;
prezzo.text = itemPrice;
prezzo.userInteractionEnabled = YES;
Don't know why when itemPrice is copied in one of those label, the program go in SIGABRT.
Instead if I try to read the content with an NSLog(#"%#",itemPrice); it return the exact value, so it means that is a valid NSString.
The only solution I found is passing through a NSNumber:
NSNumber *itemPrice = (NSNumber *) [rowVals objectForKey:#"price"];
prezzo.text = [[NSString alloc] initWithFormat:#"%#", itemPrice];
There is another way to use directly the NSString?
Probably the value in the #"price" field is NSNumber, and not an NSString. The NSLog method will still provide a correct result, since %# is used for any NSObject subclass, not just NSString.
How about this:
NSString *itemPrice = [[rowVal objectForKey:#"price"] stringValue];
prezzo.text = itemPrice;
The problem might be the object type returned by [rowVals objectForKey:#"price"]. When you place the (NSString *) cast before the method call, you're telling the compiler what type of object is returned, but not actually converting it into an NSString. The line you use below does convert from NSNumber (or whatever other object) to a string: [[NSString alloc] initWithFormat:#"%#", itemPrice]
You might be storing NSNumber's object in NSDictionary instead of NSString.
There could be 2 ways: one would be to convert NSNumber to NSString while adding it to dictionary or the other way would be to convert NSNumber to NSString while assigning it to "itemName".
you may do the conversion for second option like:
NSString *itemPrice = [[rowVals objectForKey:#"price"]stringValue];

Objective C: convert a NSMutableString in NSString

I have an NSMutableString, how can I convert it to an NSString?
Either via:
NSString *immutableString = [NSString stringWithString:yourMutableString];
or via:
NSString *immutableString = [[yourMutableString copy] autorelease];
//Note that calling [foo copy] on a mutable object of which there exists an immutable variant
//such as NSMutableString, NSMutableArray, NSMutableDictionary from the Foundation framework
//is expected to return an immutable copy. For a mutable copy call [foo mutableCopy] instead.
Being a subclass of NSString however you can just cast it to an NSString
NSString *immutableString = yourMutableString;
making it appear immutable, even though it in fact stays mutable.
Many methods actually return mutable instances despite being declared to return immutable ones.
NSMutableString is a subclass of NSString, so you could just typecast it:
NSString *string = (NSString *)mutableString;
In this case, string would be an alias of mutalbeString, but the compiler would complain if you tried to call any mutable methods on it.
Also, you could create a new NSString with the class method:
NSString *string = [NSString stringWithString:mutableString];