Search code examples
nsstringuilabelstring-formattingnsnumberprimitive

NSString and math?


I am newbie on Xcode. So i try to make a single app but i stuck somewhere actually i know where :D i can't write math codes i didnt understand their fundamentals please guys tell me how can i write correctly my code ;

I want to ;

dsonuc = boy text * boy text / kilo text

there is my code (i couldnt write just a simple multiplication code)

float dsonuc = dboy * 2; idealkilo.text = [NSString stringWithFormat:@" Kutle Endesiniz %f",dsonuc];

can someone tell me what are my wrongs ?

Thank you guys.


Solution

  • Correct. Numbers (primitives like int, float, etc..) in Objective C need to be wrapped with an NSNumber.

    Use this:

    NSNumber* dsonuc1= [NSNumber numberWithFloat:dsonuc];
    idealkilo.text = [NSString stringWithFormat:@" Kutle Endesiniz %f",dsonuc1];
    

    Also see this link:

    https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html

    Hope this helps.