วิธี convert string to int/float, int/float to string ใน Objective C
พอดีว่าง ๆ เลยหาโหลด IPhone SDK มาลองเล่น ๆ ดู เขียนไปมึน ๆ ไปก็มัน ๆ ดี
สมมุติว่า เรามี TextField(Windows เรียก TextBox) อันนึง TagId = 1
แล้วเราต้องการ Convert ไปใส่ในตัวแปล Int หรือ Float
UITextField *txtNumber = (UITextField *)[window viewWithTag:1];
float fNumber;
fNumber = [[txtNumber text] floatValue];
float nNumber;
nNumber = [[txtNumber text] intValue];
และถ้าเราอยาก Convert จาก Int/Float เป็น String ลงใน text field
txtShowF.text = [NSString stringWithFormat:@"%f", fNumber];
txtShowN.text = [NSString stringWithFormat:@"%n", nNumber];
Reference :
Tags: Cocoa, convert int to string, IPhone SDK, Objective C, string to int, textfield cocoa