With variables being so highly tied to their data types—and Objective-C operations being exceedingly picky about the types of data they work on—there will be many times when you need to convert one data type to another. This happens largely in the realm of numbers and strings, but you also have the added complexity of specific number types being incompatible with each other. You can’t send an
int to do a float’s job.For simple conversions of basic number values, you can use a technique called casting. A cast forces a value to perform a conversion based on strict rules established for the C language. Most of the rules dictate how conversions between numeric types (e.g., long and short versions of
int and float types) are to behave during such conversions.Specify a cast by placing the desired output data type in parentheses before the original value. For example, the following changes an
int to a float:float myValueAsFloat = (float)myValueAsInt;
One of the rules that could impact you is that when a
float or double is cast to an int, the numbers to the right of the decimal (and the decimal) are stripped off. No rounding occurs. You can see how casting works for yourself in Workbench by modifying the runMyCode: method as follows:- (IBAction)runMyCode:(id)sender {
double a = 12345.6789;
int b = (int)a;
float c = (float)b;
NSLog(@"\ndouble = %f\nint of double = %d\nfloat of int = %f", a, b, c);
}When you click the button in the running Workbench app, the console reveals the following log result:
double = 12345.678900 int of double = 12345 float of int = 12345.000000
Take special notice that the placeholders in
NSLog() specify integer (%d) and floating-point (%f) values where needed. If you try to assign a value of one type to a % placeholder of a different type, you will receive unexpected or erroneous results.Casting also works with complex classes. For example, you can cast the
runMyCode: method’s argument from an id type to a UIButton type as follows:UIButton *myButton = (UIButton *)sender;
At compile time, Xcode will validate subsequent messages sent to
myButton against the list of methods defined for that class.To convert a string type to a number type, you can use one of several
NSString methods, each of which returns a number of a very specific data type (doubleValue, floatValue, intValue, integerValue, longLongValue, and boolValue). For example, in the following code, a string object is created so that it begins with numerals but ends with nonnumeric characters. Then the doubleValue method returns as much of a double type number as it can pull from the string (much like Javascript’s parseFloat() function does):NSString *myString = @"123.45abc"; double a = [myString doubleValue]; NSLog(@"double = %f", a);
Due to the precision of a double to six decimal places, the result in the console is as follows:
double = 123.450000
To convert a numeric data type to a string, you can create a string via the same type of format specifier you’ve been seeing in
NSLog() throughout the preceding chapters. The format expression is an argument to the stringWithFormat: class method of NSString. Most commonly, you will need to integrate a calculated value into a longer string. In the following example, an integer is plugged into a format specifier within a string:int myAge = 34;
int until100 = 100 - myAge;
NSString *result =
[NSString stringWithFormat:@"You have %d years to go to reach 100.", until100];
NSLog(@"%@", result);
Cocoa Touch provides the
NSNumber class, which lets you express any of the basic numeric values as full-fledged Objective-C objects. Such numeric objects are essential if you need to store a number in an Objective-C collection, such as an NSArray. For general math operations, however, basic values are easier to work with and are memory-efficient. And if you need to migrate a value between an NSNumber object and basic value, the NSNumber class offers a full set of methods that let you jump from one form to the other.Is it possible for Javascript programmers to learn the iPhone SDK and live to tell the tale? Technology guru Danny Goodman did, and in this book he leaves a well-marked trail for you to follow. Goodman understands the challenges you might face with the SDK, and he introduces Xcode, Objective-C, and Cocoa Touch in a context you'll readily understand. If you're a Javascript programmer and want to take advantage of the iPhone and iPad, iOS 4 SDK is your tool -- and this is your book. Includes full coverage of iOS SDK 4.2.




Help







