Game Development Reference
In-Depth Information
This fact is used in the return statement in the ?: ternary operator. It reads: if number is
nil , return the statement after the question mark ( number.doubleValue ); otherwise,
return the statement after the colon ( 1.0 ). So if number is nil , it just returns a safe de-
fault value—in this case, the highest possible volume level of 1.0. The parentheses are op-
tional and used only to enhance readability, to clarify that the result of the expression is
returned rather than number .
Note The CGFloat type is defined as float on 32-bit devices (for example,
iPhone 5C) but is a double type on 64-bit devices (such as iPhone 5S). It is
good practice to use setDouble: and doubleValue when the type in-
volved is CGFloat . This prevents the value from being truncated in NSUser-
Defaults . It is not an issue to store a float value as double , nor is return-
ing a double that will then be truncated to float . In the same light, it is a
best practice to always use CGFloat in place of float and use double only
if the type has to be double even on 32-bit devices.
Without further ado, add the methods in Listing 7-13 to GameState.m just below the mu-
sic volume methods. They are the equivalent setter and getter methods for the effect-
sVolume property.
Listing 7-13 . The equivalent setter and getter methods for the effectsVolume property
static NSString* KeyForEffectsVolume = @"effectsVolume";
-(void) setEffectsVolume:(CGFloat)volume
{
[[NSUserDefaults standardUserDefaults] setDouble:volume
forKey:KeyForEffectsVolume];
}
-(CGFloat) effectsVolume
{
NSNumber* number = [[NSUserDefaults
standardUserDefaults]
objectForKey:KeyForEffectsVolume];
return (number ? number.doubleValue : 1.0);
}
Search WWH ::




Custom Search