diff options
author | Yannick Loriot <yannick.loriot@gmail.com> | 2013-08-13 15:25:47 +0200 |
---|---|---|
committer | Yannick Loriot <yannick.loriot@gmail.com> | 2013-08-13 15:25:47 +0200 |
commit | a29c4ee753894d1b58fa398d9f49567d098d6221 (patch) | |
tree | c9f6c9cce97c07fae2149d14aadb476deea6b70e /objective-c.html.markdown | |
parent | 48fcef441fce2235e5dcd0d7c052b44f315504a5 (diff) |
[Refactoring] Literals
Diffstat (limited to 'objective-c.html.markdown')
-rw-r--r-- | objective-c.html.markdown | 80 |
1 files changed, 44 insertions, 36 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 63aa64f1..2b8e9874 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -28,72 +28,80 @@ int main (int argc, const char * argv[]) // Create an autorelease pool to manage the memory into your program NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - // Print "Hello World!" to the console - NSLog(@"Hello World!"); + // Use NSLog to print lines to the console + NSLog(@"Hello World!"); // Print "Hello World!" - // Clean up the memory you used into your program - [pool drain]; - - // End your program - return 0; -} + // character literals + NSNumber *theLetterZ = @'Z'; -// all the primitive variable types are the same as in C -// char, int, long, double, float + // 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; -// Simple, common classes -// number -NSNumber *firstNumber = @1; -NSNumber *secondNumber = @23.0; -NSNumber *boolNumber = @YES; + // BOOL literals + NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES] + NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO] -// string -NSString *aString = @"some string"; + // strings + NSString *helloString = @"hello"; -// array -NSArray *array = @[ @1, @2]; + // array + NSArray *anArray = @[@1, @2]; -// dictionary -NSDictionay *dictionary = @{ @"aKey" : @"aValue", @"aKey2" : @"aValue2" }; + // dictionary + NSDictionay *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; -// Import headers with #import -#import <UIKit/UIKit.h> -#import "SomeAppDelegate.h" + // Clean up the memory you used into your program + [pool drain]; + + // End your program + return 0; +} -##Coding classes +/////////////////////////////////////// +// Classes And Functions +/////////////////////////////////////// // Declare your class in a header(.h) file: -@interface UserObject : NSObject{ -// instance variables +@interface UserObject : NSObject +{ + // instance variables } // Class method - + (NSString*) ClassMethod; ++ (NSString *)classMethod; // Instance method - - (NSString*) instanceMethodWithParmeter:(NSString*)string; +- (NSString *)instanceMethodWithParmeter:(NSString *)string; @end -// Add class methods in an implementation (.m) file: +// Implement the methods in an implementation (.m) file: @implementation UserObject -+ (NSString*) ClassMethod{ - return @"SomeString"; ++ (NSString *)classMethod +{ + return @"SomeString"; } -- (NSString*) instanceMethodWithParmeter:(NSString*)string; +- (NSString *)instanceMethodWithParmeter:(NSString *)string { - return @"New string"; + return @"New string"; } -- (NSString*) otherMethodWithString:(NSString*)string; +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number { - return [NSString stringWithString:string]; + return @42; } + @end // Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. |