From 4283f0c964f55295f024e535a2373ef8d04e4069 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:32:20 +0200 Subject: [FIX] filename --- objective-c.html.markdown | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 objective-c.html.markdown (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown new file mode 100644 index 00000000..787a9219 --- /dev/null +++ b/objective-c.html.markdown @@ -0,0 +1,99 @@ +--- +language: Objectiv-C +author: Eugene Yagrushkin +author_url: www.about.me/yagrushkin +filename: learnc.Objectiv-C +--- + +Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. +It's is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. + +```Objective-C +// Single-line comments start with // + +/* +Multi-line comments look like this. +*/ + +##Basic types +// all the primitive variable types are the same as in C +// char, int, long, double, float + + +// Simple, common classes +// number +NSNumber *firstNumber = @1; +NSNumber *secondNumber = @23.0; +NSNumber *boolNumber = @YES; + +// string +NSString *aString = @"some string"; + +// array +NSArray *array = @[ @1, @2]; + +// dictionary +NSDictionay *dictionary = @{ @"aKey" : @"aValue", @"aKey2" : @"aValue2" }; + +// Import headers with #import +#import +#import "SomeAppDelegate.h" + +##Coding classes + +// Declare your class in a header(.h) file: + +@interface UserObject : NSObject{ +// instance variables +} + +// Class method + + (NSString*) ClassMethod; + +// Instance method + - (NSString*) instanceMethodWithParmeter:(NSString*)string; + +@end + +// Add class methods in an implementation (.m) file: + +@implementation UserObject + ++ (NSString*) ClassMethod{ + return @"SomeString"; +} + +- (NSString*) instanceMethodWithParmeter:(NSString*)string; +{ + return @"New string"; +} + +- (NSString*) otherMethodWithString:(NSString*)string; +{ + return [NSString stringWithString:string]; +} +@end + +// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. +UserObject *someObject = [[UserObject alloc] init]; + +##Calling Methods + +// The Objective-C model of object-oriented programming is based on message passing to object instances. +// In Objective-C one does not simply call a method; one sends a message. + +[someObject instanceMethodWithParmeter:@"Steve Jobs"]; + +##Nested Messages +// nested messages look like this: + +[someObject instanceMethodWithParmeter:[someObject otherMethodWithString:@"Jony Ive"]]; + +``` +## Further Reading + +[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) + +[Objectively Speaking: A Crash Course in Objective-C](http://www.raywenderlich.com/12444/objectively-speaking-a-crash-course-in-objective-c) + +[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) -- cgit v1.2.3 From 5b182eb5e99be7d04071cdb04466ccdae7b2fde9 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:38:39 +0200 Subject: [UPDATE] Authors --- objective-c.html.markdown | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 787a9219..df789677 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -1,8 +1,11 @@ --- -language: Objectiv-C -author: Eugene Yagrushkin -author_url: www.about.me/yagrushkin -filename: learnc.Objectiv-C + +language: Objective-C +contributors: + - ["Eugene Yagrushkin", "www.about.me/yagrushkin"] + - ["Yannick Loriot", "https://github.com/YannickL"] +filename: LearnObjectiveC.m + --- Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. -- cgit v1.2.3 From d842eb4f819568c1503c26e494e271f1cd179542 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:45:49 +0200 Subject: minor fixes --- objective-c.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index df789677..21460632 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -8,8 +8,8 @@ filename: LearnObjectiveC.m --- -Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. -It's is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. +Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch. +It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. ```Objective-C // Single-line comments start with // @@ -97,6 +97,6 @@ UserObject *someObject = [[UserObject alloc] init]; [Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) -[Objectively Speaking: A Crash Course in Objective-C](http://www.raywenderlich.com/12444/objectively-speaking-a-crash-course-in-objective-c) +[Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/) [iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) -- cgit v1.2.3 From 48fcef441fce2235e5dcd0d7c052b44f315504a5 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 14:56:09 +0200 Subject: [ADD] Hello World! --- objective-c.html.markdown | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 21460632..63aa64f1 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -18,7 +18,26 @@ It is a general-purpose, object-oriented programming language that adds Smalltal Multi-line comments look like this. */ -##Basic types +// Imports the Foundation headers with #import +#import + +// Your program's entry point is a function called +// main with an integer return type. +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!"); + + // Clean up the memory you used into your program + [pool drain]; + + // End your program + return 0; +} + // all the primitive variable types are the same as in C // char, int, long, double, float -- cgit v1.2.3 From a29c4ee753894d1b58fa398d9f49567d098d6221 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 15:25:47 +0200 Subject: [Refactoring] Literals --- objective-c.html.markdown | 80 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'objective-c.html.markdown') 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 -#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. -- cgit v1.2.3 From 0bd403fdb935331c3c391ffd79f0245032dee3b5 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 15:50:09 +0200 Subject: [UPDATE] Literals Examples --- objective-c.html.markdown | 63 ++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 22 deletions(-) (limited to 'objective-c.html.markdown') 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]; -- cgit v1.2.3 From 947c137680c2699bdb118b63460c560bac1fdd3c Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 15:52:11 +0200 Subject: minor fixes --- objective-c.html.markdown | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 284eca92..fc4b2900 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -37,29 +37,30 @@ int main (int argc, const char * argv[]) NSLog(@"Hello %@!", worldString); // Print "Hello World!" // Character literals - NSNumber *theLetterZ = @'Z'; - NSLog(@"%c", [theLetterZ charValue]); + NSNumber *theLetterZNumber = @'Z'; + char theLetterZ = [theLetterZNumber charValue]; + NSLog(@"%c", theLetterZ); // Integral literals NSNumber *fortyTwoNumber = @42; - int fortyTwo = [fortyTwo intValue]; + int fortyTwo = [fortyTwoNumber intValue]; NSLog(@"%i", fortyTwo); NSNumber *fortyTwoUnsignedNumber = @42U; - unsigned int fortyTwoUnsigned = [fortyTwoUnsigned unsignedIntValue]; + unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; NSLog(@"%u", fortyTwoUnsigned); NSNumber *fortyTwoLongNumber = @42L; - long fortyTwoLong = [aLong longValue]; + long fortyTwoLong = [fortyTwoLongNumber longValue]; NSLog(@"%li", fortyTwoLong); // Floating point literals NSNumber *piFloatNumber = @3.141592654F; - float piFloat = [piFloat floatValue]; + float piFloat = [piFloatNumber floatValue]; NSLog(@"%f", piFloat); NSNumber *piDoubleNumber = @3.1415926535; - piDouble = [piDouble doubleValue]; + piDouble = [piDoubleNumber doubleValue]; NSLog(@"%f", piDouble); // BOOL literals -- cgit v1.2.3 From 0040ce616b4e27a182834cd2fd03aacc2561a198 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:04:20 +0200 Subject: [UPDATE] Object Declaration --- objective-c.html.markdown | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index fc4b2900..c3df514b 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -25,15 +25,28 @@ Multi-line comments look like this. // main with an integer return type. int main (int argc, const char * argv[]) { - // Create an autorelease pool to manage the memory into your program + // Create an autorelease pool to manage the memory into the program NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // Use NSLog to print lines to the console NSLog(@"Hello World!"); // Print the string "Hello World!" - // String object - NSString *worldString = @"World"; + /////////////////////////////////////// + // Types & Variables + /////////////////////////////////////// + + // Primitive declarations + int myPrimitive; + + // Object declarations + // Put the * in front of the variable names for strongly-typed object declarations + MyClass *myObject1; // Strong typing + id myObject2; // Weak typing // %@ is an object + NSLog(@"%@ and %@", myObject1, myObject2); // Print "(null) and (null)" + + // String + NSString *worldString = @"World"; NSLog(@"Hello %@!", worldString); // Print "Hello World!" // Character literals @@ -50,6 +63,10 @@ int main (int argc, const char * argv[]) unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; NSLog(@"%u", fortyTwoUnsigned); + NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42]; + short fortyTwoShort = [fortyTwoShortNumber shortValue]; + NSLog(@"%hi", fortyTwoShort); + NSNumber *fortyTwoLongNumber = @42L; long fortyTwoLong = [fortyTwoLongNumber longValue]; NSLog(@"%li", fortyTwoLong); @@ -77,10 +94,14 @@ int main (int argc, const char * argv[]) NSObject *valueObject = aDictionary[@"A Key"]; NSLog(@"Object = %@", valueObject); // Print "Object = (null)" + /////////////////////////////////////// + // Operators + /////////////////////////////////////// + // Clean up the memory you used into your program [pool drain]; - // End your program + // End the program return 0; } -- cgit v1.2.3 From 25f4c7e80a4682db35daed75d42ca91eb1504736 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:07:58 +0200 Subject: minor fixes --- objective-c.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index c3df514b..ad2bedf9 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -36,14 +36,16 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Primitive declarations - int myPrimitive; + int myPrimitive1; + long myPrimitive2; // Object declarations // Put the * in front of the variable names for strongly-typed object declarations MyClass *myObject1; // Strong typing id myObject2; // Weak typing // %@ is an object - NSLog(@"%@ and %@", myObject1, myObject2); // Print "(null) and (null)" + // 'description' is a convention to display the value of the Objects + NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)" // String NSString *worldString = @"World"; -- cgit v1.2.3 From 0970cb8010e590332b86de26a4746c7202c22363 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:09:38 +0200 Subject: minor update --- objective-c.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index ad2bedf9..7f87da6f 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -36,13 +36,13 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Primitive declarations - int myPrimitive1; - long myPrimitive2; + int myPrimitive1 = 1; + long myPrimitive2 = 234554664565; // Object declarations // Put the * in front of the variable names for strongly-typed object declarations - MyClass *myObject1; // Strong typing - id myObject2; // Weak typing + MyClass *myObject1 = nil; // Strong typing + id myObject2 = nil; // Weak typing // %@ is an object // 'description' is a convention to display the value of the Objects NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)" -- cgit v1.2.3 From 24d9cde4883202d4b7c5db7dad8981b4a4000125 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:22:40 +0200 Subject: [NEW] Statements --- objective-c.html.markdown | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 7f87da6f..ebae2fc7 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -99,6 +99,78 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Operators /////////////////////////////////////// + + // The operators works like in the C language + // For example: + 3 == 2; // => 0 (NO) + 3 != 2; // => 1 (YES) + 1 && 1; // => 1 (Logical and) + 0 || 1; // => 1 (Logical or) + ~0x0F; // => 0xF0 (bitwise negation) + 0x0F & 0xF0; // => 0x00 (bitwise AND) + 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) + + /////////////////////////////////////// + // Control Structures + /////////////////////////////////////// + + // If-Else statement + if (NO) + { + NSLog(@"I am never run"); + } else if (0) + { + NSLog(@"I am also never run"); + } else + { + NSLog(@"I print"); + } + + // Switch statement + switch (2) { + case 0: + { + NSLog(@"I am never run"); + } break; + case 1: + { + NSLog(@"I am also never run"); + } break; + default: + { + NSLog(@"I print"); + } break; + } + + // While loops exist + int ii = 0; + while (ii < 4) + { + NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. + } // => prints "0, + 1, + 2, + 3," + + // For loops too + int jj; + for (jj=0; jj < 4; jj++) + { + NSLog(@"%d,", ii++); + } // => prints "0, + 1, + 2, + 3," + + // Foreach + NSArray *values = @[@0, @1, @2, @3]; + for (NSNumber *value in values) + { + NSLog(@"%@,", value); + } // => prints "0, + 1, + 2, + 3," // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 5d800b25e427417c589f3cf2c3b19c18c178a11f Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:32:16 +0200 Subject: [NEW] Try-Catch-Finally --- objective-c.html.markdown | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index ebae2fc7..479d9ad5 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -102,6 +102,8 @@ int main (int argc, const char * argv[]) // The operators works like in the C language // For example: + 2 + 5; // => 7 + 4.2f + 5.1f; // => 9.3f 3 == 2; // => 0 (NO) 3 != 2; // => 1 (YES) 1 && 1; // => 1 (Logical and) @@ -127,7 +129,8 @@ int main (int argc, const char * argv[]) } // Switch statement - switch (2) { + switch (2) + { case 0: { NSLog(@"I am never run"); @@ -142,36 +145,50 @@ int main (int argc, const char * argv[]) } break; } - // While loops exist + // While loops statements int ii = 0; while (ii < 4) { NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. - } // => prints "0, - 1, - 2, - 3," + } // => prints "0," + "1," + "2," + "3," - // For loops too + // For loops statements int jj; for (jj=0; jj < 4; jj++) { NSLog(@"%d,", ii++); - } // => prints "0, - 1, - 2, - 3," + } // => prints "0," + "1," + "2," + "3," - // Foreach + // Foreach statements NSArray *values = @[@0, @1, @2, @3]; for (NSNumber *value in values) { NSLog(@"%@,", value); - } // => prints "0, - 1, - 2, - 3," + } // => prints "0," + "1," + "2," + "3," + // Try-Catch-Finally statements + @try + { + // Your statements here + @throw [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil]; + } @catch (NSException * e) + { + NSLog(@"Exception: %@", e); + } @finally + { + NSLog(@"Finally"); + } // => prints "Exception: File Not Found on System" + "Finally" + // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 50c3526247810078b6c63e2cbaf79bc226e873dc Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:34:12 +0200 Subject: minor updates --- objective-c.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 479d9ad5..6eac69a8 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -151,9 +151,9 @@ int main (int argc, const char * argv[]) { NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. } // => prints "0," - "1," - "2," - "3," + // "1," + // "2," + // "3," // For loops statements int jj; @@ -161,9 +161,9 @@ int main (int argc, const char * argv[]) { NSLog(@"%d,", ii++); } // => prints "0," - "1," - "2," - "3," + // "1," + // "2," + // "3," // Foreach statements NSArray *values = @[@0, @1, @2, @3]; @@ -171,9 +171,9 @@ int main (int argc, const char * argv[]) { NSLog(@"%@,", value); } // => prints "0," - "1," - "2," - "3," + // "1," + // "2," + // "3," // Try-Catch-Finally statements @try @@ -187,7 +187,7 @@ int main (int argc, const char * argv[]) { NSLog(@"Finally"); } // => prints "Exception: File Not Found on System" - "Finally" + // "Finally" // Clean up the memory you used into your program [pool drain]; -- cgit v1.2.3 From 0d41a6405627b50f2839df3a3b019836f361ecc0 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:46:16 +0200 Subject: [UPDATE] Object Creation --- objective-c.html.markdown | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 6eac69a8..f43081cf 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -201,27 +201,56 @@ int main (int argc, const char * argv[]) /////////////////////////////////////// // Declare your class in a header(.h) file: - -@interface UserObject : NSObject +// Class Declaration Syntax: +// @interface : +// { +// Member variable declarations; +// } +// -/+ (type) Method declarations; +// @end +@interface MyClass : NSObject { - // instance variables + int count; + id data; + NSString *name; } +// Create the public getter/setter for the variable count +@property(assign) int count; + +// Methods ++/- (return type)methodSignature:(Parameter Type *)parameterName; -// Class method +// + for class method + (NSString *)classMethod; -// Instance method +// - for instance method - (NSString *)instanceMethodWithParmeter:(NSString *)string; - +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; +- @end // Implement the methods in an implementation (.m) file: @implementation UserObject +// Constructors are a way of creating classes +// This is a default constructor +- (id)init +{ + if ((self = [super init])) + { + self.count = 1; + + // Create an object instance by allocating memory and initializing it. + // An object is not fully functional until both steps have been completed. + UserObject *someObject = [[UserObject alloc] init]; + } + return self; +} + + (NSString *)classMethod { - return @"SomeString"; + return [[self alloc] init]; } - (NSString *)instanceMethodWithParmeter:(NSString *)string @@ -236,9 +265,6 @@ int main (int argc, const char * argv[]) @end -// Create an object instance by allocating memory and initializing it. An object is not fully functional until both steps have been completed. -UserObject *someObject = [[UserObject alloc] init]; - ##Calling Methods // The Objective-C model of object-oriented programming is based on message passing to object instances. -- cgit v1.2.3 From 3fe1c3c8a562427533439f385df952a00b36a998 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:49:23 +0200 Subject: minor update --- objective-c.html.markdown | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index f43081cf..22791659 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -189,6 +189,18 @@ int main (int argc, const char * argv[]) } // => prints "Exception: File Not Found on System" // "Finally" + /////////////////////////////////////// + // Objects + /////////////////////////////////////// + + // Create an object instance by allocating memory and initializing it. + // An object is not fully functional until both steps have been completed. + MyClass *myObject = [[MyClass alloc] init]; + + // The Objective-C model of object-oriented programming is based on message passing to object instances. + // In Objective-C one does not simply call a method; one sends a message. + [myObject instanceMethodWithParmeter:@"Steve Jobs"]; + // Clean up the memory you used into your program [pool drain]; @@ -240,10 +252,6 @@ int main (int argc, const char * argv[]) if ((self = [super init])) { self.count = 1; - - // Create an object instance by allocating memory and initializing it. - // An object is not fully functional until both steps have been completed. - UserObject *someObject = [[UserObject alloc] init]; } return self; } @@ -265,18 +273,6 @@ int main (int argc, const char * argv[]) @end -##Calling Methods - -// The Objective-C model of object-oriented programming is based on message passing to object instances. -// In Objective-C one does not simply call a method; one sends a message. - -[someObject instanceMethodWithParmeter:@"Steve Jobs"]; - -##Nested Messages -// nested messages look like this: - -[someObject instanceMethodWithParmeter:[someObject otherMethodWithString:@"Jony Ive"]]; - ``` ## Further Reading -- cgit v1.2.3 From 664d592bc79c7dcc6c429bcee79965ef3df464f5 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:50:22 +0200 Subject: minor change --- objective-c.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 22791659..9d1178e1 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -20,6 +20,7 @@ Multi-line comments look like this. // Imports the Foundation headers with #import #import +#import "MyClass.h" // Your program's entry point is a function called // main with an integer return type. @@ -212,7 +213,7 @@ int main (int argc, const char * argv[]) // Classes And Functions /////////////////////////////////////// -// Declare your class in a header(.h) file: +// Declare your class in a header(MyClass.h) file: // Class Declaration Syntax: // @interface : // { @@ -241,7 +242,7 @@ int main (int argc, const char * argv[]) - @end -// Implement the methods in an implementation (.m) file: +// Implement the methods in an implementation (MyClass.m) file: @implementation UserObject -- cgit v1.2.3 From 0932765947b14407aa41fbe7ded00ca37a25f5c6 Mon Sep 17 00:00:00 2001 From: Yannick Loriot Date: Tue, 13 Aug 2013 16:58:13 +0200 Subject: [NEW] Protocol Implementation --- objective-c.html.markdown | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 9d1178e1..187ea30a 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -215,20 +215,22 @@ int main (int argc, const char * argv[]) // Declare your class in a header(MyClass.h) file: // Class Declaration Syntax: -// @interface : +// @interface ClassName : ParentClassName // { // Member variable declarations; // } // -/+ (type) Method declarations; // @end -@interface MyClass : NSObject +@interface MyClass : NSObject { int count; id data; NSString *name; } -// Create the public getter/setter for the variable count -@property(assign) int count; +// Convenience notation to auto generate public getter and setter +@property int count; +@property (copy) NSString *name; // Copy the object during assignment. +@property (readonly) id data; // Declare only a getter method. // Methods +/- (return type)methodSignature:(Parameter Type *)parameterName; @@ -246,8 +248,13 @@ int main (int argc, const char * argv[]) @implementation UserObject +// Call when the object is releasing +- (void)dealloc +{ +} + // Constructors are a way of creating classes -// This is a default constructor +// This is a default constructor which is call when the object is creating - (id)init { if ((self = [super init])) @@ -272,8 +279,25 @@ int main (int argc, const char * argv[]) return @42; } +// Methods declared into MyProtocol +- (void)myProtocolMethod +{ + // statements +} + @end +/* + * A protocol declares methods that can be implemented by any class. + * Protocols are not classes themselves. They simply define an interface + * that other objects are responsible for implementing. + * / +@protocol MyProtocol + - (void)myProtocolMethod; +@end + + + ``` ## Further Reading -- cgit v1.2.3 From 50e49eced17ee758d47ca06141ee2b0e27ea2ee8 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 13 Aug 2013 10:30:44 -0700 Subject: Line length edits to objective c --- objective-c.html.markdown | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 187ea30a..2b1b3c67 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -11,7 +11,7 @@ filename: LearnObjectiveC.m Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch. It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. -```Objective-C +```cpp // Single-line comments start with // /* @@ -180,7 +180,8 @@ int main (int argc, const char * argv[]) @try { // Your statements here - @throw [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil]; + @throw [NSException exceptionWithName:@"FileNotFoundException" + reason:@"File Not Found on System" userInfo:nil]; } @catch (NSException * e) { NSLog(@"Exception: %@", e); @@ -198,9 +199,10 @@ int main (int argc, const char * argv[]) // An object is not fully functional until both steps have been completed. MyClass *myObject = [[MyClass alloc] init]; - // The Objective-C model of object-oriented programming is based on message passing to object instances. + // The Objective-C model of object-oriented programming is based on message + // passing to object instances. // In Objective-C one does not simply call a method; one sends a message. - [myObject instanceMethodWithParmeter:@"Steve Jobs"]; + [myObject instanceMethodWithParameter:@"Steve Jobs"]; // Clean up the memory you used into your program [pool drain]; @@ -241,7 +243,7 @@ int main (int argc, const char * argv[]) // - for instance method - (NSString *)instanceMethodWithParmeter:(NSString *)string; - (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; -- + @end // Implement the methods in an implementation (MyClass.m) file: @@ -291,7 +293,7 @@ int main (int argc, const char * argv[]) * A protocol declares methods that can be implemented by any class. * Protocols are not classes themselves. They simply define an interface * that other objects are responsible for implementing. - * / + */ @protocol MyProtocol - (void)myProtocolMethod; @end -- cgit v1.2.3 From 50ac50f05f82c3b3069329634f373c1ba55263c0 Mon Sep 17 00:00:00 2001 From: Seva Baskin Date: Thu, 15 Aug 2013 23:25:51 +0100 Subject: Update objective-c.html.markdown Fixed a few typos --- objective-c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index 2b1b3c67..b92e3218 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -160,7 +160,7 @@ int main (int argc, const char * argv[]) int jj; for (jj=0; jj < 4; jj++) { - NSLog(@"%d,", ii++); + NSLog(@"%d,", jj++); } // => prints "0," // "1," // "2," @@ -256,7 +256,7 @@ int main (int argc, const char * argv[]) } // Constructors are a way of creating classes -// This is a default constructor which is call when the object is creating +// This is a default constructor which is called when the object is creating - (id)init { if ((self = [super init])) -- cgit v1.2.3 From dcdfd9114f3d606ede2a3d4967e7699579427e46 Mon Sep 17 00:00:00 2001 From: JakeHurlbut Date: Sat, 17 Aug 2013 18:06:23 -0400 Subject: Corrected Array Object NSLog Call --- objective-c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'objective-c.html.markdown') diff --git a/objective-c.html.markdown b/objective-c.html.markdown index b92e3218..9e9f43e7 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -90,7 +90,7 @@ int main (int argc, const char * argv[]) // Array object NSArray *anArray = @[@1, @2, @3, @4]; NSNumber *thirdNumber = anArray[2]; - NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3" + NSLog(@"Third number = %@", thirdNumber); // Print "Third number = 3" // Dictionary object NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; -- cgit v1.2.3