Rafael Marques
Rafael Marques

Reputation: 825

Objective C Input is a number

Hey I'm learning Objective C and I have one question. I Have one Text Field the Input should be a Number with this number i want to calculate something but how do I do that ?

NSNumber *test;

if ([prospekt_1 intValue] == 0) {
    test = [prospekt_1 intValue] * 2.50;
    [ergebnis_prospekt_1 setIntValue:test];
}

Thanks for your help. :)

Upvotes: 1

Views: 440

Answers (1)

AWF4vk
AWF4vk

Reputation: 5890

NSNumber test is an object pointer. You're assigning an int to an object, essentially making it a pointer to an invalid memory address.

You want test = [NSNumber numberWithInt:[prospekt_1 intValue] * 2.50];

This creates a new object (NSNumber) and assigns it to the pointer test.

or

You could skip the NSNumber object entirely and just assign the int value directly. Why waste memory on an object? An int is smaller memory footprint.

Replace NSNumber * with int and it will also work and be more memory efficient.

Upvotes: 4

Related Questions