mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
[UPDATE] Literals Examples
This commit is contained in:
parent
a29c4ee753
commit
0bd403fdb9
@ -29,33 +29,52 @@ int main (int argc, const char * argv[])
|
|||||||
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
||||||
|
|
||||||
// Use NSLog to print lines to the console
|
// 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';
|
NSNumber *theLetterZ = @'Z';
|
||||||
|
NSLog(@"%c", [theLetterZ charValue]);
|
||||||
|
|
||||||
// integral literals
|
// Integral literals
|
||||||
NSNumber *fortyTwo = @42;
|
NSNumber *fortyTwoNumber = @42;
|
||||||
NSNumber *fortyTwoUnsigned = @42U;
|
int fortyTwo = [fortyTwo intValue];
|
||||||
NSNumber *fortyTwoLong = @42L;
|
NSLog(@"%i", fortyTwo);
|
||||||
NSNumber *fortyTwoLongLong = @42LL;
|
|
||||||
|
|
||||||
// floating point literals
|
NSNumber *fortyTwoUnsignedNumber = @42U;
|
||||||
NSNumber *piFloat = @3.141592654F;
|
unsigned int fortyTwoUnsigned = [fortyTwoUnsigned unsignedIntValue];
|
||||||
NSNumber *piDouble = @3.1415926535;
|
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
|
// BOOL literals
|
||||||
NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES]
|
NSNumber *yesNumber = @YES;
|
||||||
NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO]
|
NSNumber *noNumber = @NO;
|
||||||
|
|
||||||
// strings
|
// Array object
|
||||||
NSString *helloString = @"hello";
|
NSArray *anArray = @[@1, @2, @3, @4];
|
||||||
|
NSNumber *thirdNumber = anArray[2];
|
||||||
|
NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3"
|
||||||
|
|
||||||
// array
|
// Dictionary object
|
||||||
NSArray *anArray = @[@1, @2];
|
NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
|
||||||
|
NSObject *valueObject = aDictionary[@"A Key"];
|
||||||
// dictionary
|
NSLog(@"Object = %@", valueObject); // Print "Object = (null)"
|
||||||
NSDictionay *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
|
|
||||||
|
|
||||||
// Clean up the memory you used into your program
|
// Clean up the memory you used into your program
|
||||||
[pool drain];
|
[pool drain];
|
||||||
|
Loading…
Reference in New Issue
Block a user