Why does this add M_PI as a string “3.141593” to an NSMutableArray? How can I add M_PI as a float to the array?
- (void)pushOperand:(float)operand
{
[self.operandStack addObject:[NSNumber numberWithFloat:operand]];
}
[self pushOperand:M_PI];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As others have said, you can’t store a float directly in an
NSMutableArray. Your code is already inserting the value as an instance ofNSNumber, however, and this is what you want. Later, when you pull the object back out of the array, you can restore it to a POD type with something like this:Kudos, by the way, for doing the Stanford iOS course 🙂