diff options
author | Yannick Loriot <yannick.loriot@gmail.com> | 2013-08-13 15:50:09 +0200 |
---|---|---|
committer | Yannick Loriot <yannick.loriot@gmail.com> | 2013-08-13 15:50:09 +0200 |
commit | 0bd403fdb935331c3c391ffd79f0245032dee3b5 (patch) | |
tree | f407c9f1459662f6d129afff7c73c9fe372e747f /objective-c.html.markdown | |
parent | a29c4ee753894d1b58fa398d9f49567d098d6221 (diff) |
[UPDATE] Literals Examples
Diffstat (limited to 'objective-c.html.markdown')
-rw-r--r-- | objective-c.html.markdown | 63 |
1 files changed, 41 insertions, 22 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 2b8e9874..284eca92 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -29,33 +29,52 @@ int main (int argc, const char * argv[]) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // Use NSLog to print lines to the console - NSLog(@"Hello World!"); // Print "Hello World!" + NSLog(@"Hello World!"); // Print the string "Hello World!" - // character literals + // String object + NSString *worldString = @"World"; + // %@ is an object + NSLog(@"Hello %@!", worldString); // Print "Hello World!" + + // Character literals NSNumber *theLetterZ = @'Z'; + NSLog(@"%c", [theLetterZ charValue]); - // integral literals - NSNumber *fortyTwo = @42; - NSNumber *fortyTwoUnsigned = @42U; - NSNumber *fortyTwoLong = @42L; - NSNumber *fortyTwoLongLong = @42LL; - - // floating point literals - NSNumber *piFloat = @3.141592654F; - NSNumber *piDouble = @3.1415926535; + // Integral literals + NSNumber *fortyTwoNumber = @42; + int fortyTwo = [fortyTwo intValue]; + NSLog(@"%i", fortyTwo); + + NSNumber *fortyTwoUnsignedNumber = @42U; + unsigned int fortyTwoUnsigned = [fortyTwoUnsigned unsignedIntValue]; + NSLog(@"%u", fortyTwoUnsigned); + + NSNumber *fortyTwoLongNumber = @42L; + long fortyTwoLong = [aLong longValue]; + NSLog(@"%li", fortyTwoLong); + + // Floating point literals + NSNumber *piFloatNumber = @3.141592654F; + float piFloat = [piFloat floatValue]; + NSLog(@"%f", piFloat); + + NSNumber *piDoubleNumber = @3.1415926535; + piDouble = [piDouble doubleValue]; + NSLog(@"%f", piDouble); // BOOL literals - NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES] - NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO] - - // strings - NSString *helloString = @"hello"; - - // array - NSArray *anArray = @[@1, @2]; - - // dictionary - NSDictionay *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; + NSNumber *yesNumber = @YES; + NSNumber *noNumber = @NO; + + // Array object + NSArray *anArray = @[@1, @2, @3, @4]; + NSNumber *thirdNumber = anArray[2]; + NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3" + + // Dictionary object + NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; + NSObject *valueObject = aDictionary[@"A Key"]; + NSLog(@"Object = %@", valueObject); // Print "Object = (null)" // Clean up the memory you used into your program [pool drain]; |