iOS training (basic)

837 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
837
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
57
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Reference:http://disanji.net/iOS_Doc/#documentation/DeveloperTools/Conceptual/A_Tour_of_Xcode/010-Xcode_Features_Overview/FeaturesTake2.html#//apple_ref/doc/uid/TP30000890-CH220-SW3http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/RM_YourFirstApp_iOS/Articles/01_CreatingProject.html#//apple_ref/doc/uid/TP40011343-TP40012323-CH3-SW3
  • Reference:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
  • Reference:https://developer.apple.com/library/mac/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/http://www.icodeblog.com/2009/06/18/objective-c-20-an-intro-part-1/
  • iOS training (basic)

    1. 1. iOS Training (Basics) Gurpreet Singh Sriram Viswanathan Yahoo! Confidential 1
    2. 2. Topics  Getting Started  Introduction to Xcode and Application settings  Understanding App execution flow  Introduction to Objective-C  Writing your First iPhone Application  Introduction to Interface Builder  Outlets and Actions  Storyboards  Using the iPhone/iPad Simulator Yahoo! Confidential 2
    3. 3. Some Interesting Facts  What does ‘i’ stands for in iPhone?  iPhone 5 was world’s best selling smartphone in Q4 2012, iPhone 5 and iPhone 4S put together accounted for 1 in every 5 smartphones shipped in Q4.  Total iPhones sold till date – 250 million  Total iPads sold till date – 100 million  People spend $1 million / day in Apple App store.  App store has 8 lacs active apps as of March 2013  40 billion app downloads as of March 2013 Yahoo! Confidential 3
    4. 4. Topics  Getting Started  Introduction to Xcode and Application settings  Understanding App execution flow  Introduction to Objective-C  Writing your First iPhone Application  Introduction to Interface Builder  Outlets and Actions  Storyboards  Using the iPhone/iPad Simulator Yahoo! Confidential 4
    5. 5. Introduction to Xcode Xcode Yahoo! Confidential 5
    6. 6. Topics  Getting Started  Introduction to Xcode and Application settings  Understanding App execution flow  Introduction to Objective-C  Writing your First iPhone Application  Introduction to Interface Builder  Outlets and Actions  Storyboards  Using the iPhone/iPad Simulator Yahoo! Confidential 6
    7. 7. Understanding App Execution Flow App execution flow Yahoo! Confidential 7
    8. 8. Understanding App Execution Flow Not running Foreground Inactive Active Background Suspended Yahoo! Confidential 8
    9. 9. App Delegate Methods  application:willFinishLaunchingWithOptions  application:didFinishLaunchingWithOptions  applicationDidBecomeActive  applicationWillResignAcitve  applicationDidEnterBackground  applicationWillEnterForeground  applicationWillTerminate Yahoo! Confidential 9
    10. 10. App Launch Cycle User taps icon main() UIApplicationMain() Load main UI File First Initialization application:willFinishLaunchingWithOptions Restore UI state Various methods Final Initialization application:didFinishLaunchingWithOptions Activate the App application:didBecomeActive Event Loop Yahoo! Confidential Handle Events 10
    11. 11. Topics  Getting Started  Introduction to Xcode and Application settings  Understanding App execution flow  Introduction to Objective-C  Writing your First iPhone Application  Introduction to Interface Builder  Outlets and Actions  Storyboards  Using the iPhone/iPad Simulator Yahoo! Confidential 11
    12. 12. Objective C Basics  Objective C is layered on top of C language  Is a superset of C  Provides object-oriented capabilities  NeXT Software licensed Objective C in 1988  Apple acquired NeXT in 1996  Today it is the native language for developing applications for Mac OS X and iOS  All of the syntax for non-object-oriented operations (including primitive variables, expressions, function declarations) are identical to that of C  While the syntax for object-oriented features is an implementation of messaging.  You might find its syntax a bit complex in starting but will get used to as you progress. Yahoo! Confidential 12
    13. 13. Objective C Basics Creating objects In other languages, you create objects like this: object = new Class(); The same in Objective C will look like object = [[Class alloc] init]; There might be some cases when you may want to pass some input while creating objects. In other languages you pass the input to the constructor like this: object = new Class(2); The same in Objective C you will do the same like this object = [[Class alloc] initWithInt:2]; Yahoo! Confidential 13
    14. 14. Objective C Basics Some points to note about objects in Objective C  All object variable are pointers  Object must be alloc'ed and init'ed  When you declare a variable to store an object you need to mention type of object it will hold. Class *object; object = [[Class alloc] init];  Keyword id is just a way of saying any object id object; object = [[Class alloc] init];  id doesn't use pointer notation  Keyword nil just means no object Class *object = nil; Yahoo! Confidential 14
    15. 15. Objective C Basics Declaring methods A simple method declaration in other languages will look like setX(n) or setX (int n); or void setX (int n); The same in Objective C will look like - (void) setX: (int) n; Yahoo! Confidential 15
    16. 16. Objective C Basics Method declaration explained - (void) setX: (int) n; Method type: + = class method - = instance method Yahoo! Confidential Return type Method name Argument type Argument name 16
    17. 17. Objective C Basics Calling methods A simple method call in other languages will look like output = object.method(); output = object.method(inputParameter); The same in Objective C will look like output = [object method]; output = [object method:inputParameter]; Yahoo! Confidential 17
    18. 18. Objective C Basics Nested method calls In many languages, nested method or method calls look like this: object.function1 ( object.function2() ); The same in Objective C will look like [object function1:[object function2]]; e.g. [[Class alloc] init] Multi-input methods A simple multi-input method call in other languages will look like object.setXAndY(3, 2); The same in Objective C will look like [object setX:3 andY:2]; Yahoo! Confidential 18
    19. 19. Objective C Basics Multi-input methods explained [object setX:3 andY:2] Beginning Method Call Syntax Object of the Class containing the method First parameter value First named parameter Yahoo! Confidential Ending Method Call Syntax Second parameter value Second named parameter 19
    20. 20. Objective C Basics Creating classes  The specification of a class in Objective-C requires two distinct pieces: the interface and the implementation.  The interface portion contains the class declaration and defines the instance variables and methods associated with the class.  The interface is usually in a .h file.  The implementation portion contains the actual code for the methods of the class.  The implementation is usually in a .m file.  When you want to include header files in your source code, you typically use a #import directive.  This is like #include, except that it makes sure that the same file is never included more than once. Yahoo! Confidential 20
    21. 21. Objective C Basics Example: @interface Movie: NSObject { NSString *name; } - (id) initWithString: (NSString *) movieName; + (Movie *) createMovieWithName: (NSString *) movieName; @end Yahoo! Confidential 21
    22. 22. Objective C Basics #import Movie.h; @implementation Movie - (id) initWithString: (NSString *) movieName { self = [super init]; if (self) { name = movieName; } return self; } + (Movie *) createMovieWithName: (NSString *) movieName { return [[self alloc] initWithString: movieName]; } @end Yahoo! Confidential Same as this (keyword) in other languages 22
    23. 23. Objective C Basics Some points to note about #import  Movie.m file includes Movie.h file.  Wherever we want to use Movie object we import Movie.h file (we never import Movie.m file)  Use @class to avoid circular reference (class A needs to import class B and class B needs to import class A). @class B; @interface A: NSObject - (B*) calculateMyBNess; @end @class A; @interface B: NSObject - (A*) calculateMyANess; @end  This concept is called forward declaration, tells compiler trust me there is class called class B Yahoo! Confidential 23
    24. 24. Objective C Basics There are some issues in previous example (Movie Class)  By default all methods are public in objective C  By default all instance variables are private in objective C  What if I directly call initWithString instead of createMovieWithName?  We need to make initWithString private.  Secondly, what if I don’t know the movie name upfront and I want to create the Movie object and then assign the name of movie later?  We have to provide public methods (getter and setter) to get and set the movie name. Yahoo! Confidential 24
    25. 25. Objective C Basics Using Private Methods @interface Movie: NSObject { NSString *name; } - (id) initWithString: (NSString *) movieName; + (Movie *) createMovieWithName: (NSString *) movieName; @end Yahoo! Confidential 25
    26. 26. Objective C Basics #import Movie.h; @interface Movie (private) - (id) initWithString: (NSString *) movieName; @end @implementation Movie - (id) initWithString: (NSString *) movieName { self = [super init]; if (self) { name = movieName; } return self; } + (Movie *) createMovieWithName: (NSString *) movieName { return [[self alloc] initWithString: movieName]; } @end Yahoo! Confidential 26
    27. 27. Objective C Basics Adding getter and setter methods  Note, in objective C, as per convention the getter method for a variable named ‘age’ is not ‘getAge’, in fact it is called as ‘age’ only.  But, the setter method for variable ‘age’ will be called as ‘setAge’.  So in our example, getter method will be movie.name and setter method will be movie.setName Yahoo! Confidential 27
    28. 28. Objective C Basics Adding getter and setter methods @interface Movie: NSObject { NSString *name; } - (NSString *) name; - (void) setName: (NSString *) movieName; + (Movie *) createMovieWithName: (NSString *) movieName; @end Yahoo! Confidential 28
    29. 29. Objective C Basics … @implementation Movie - (NSString *) name { return name; } - (void) setName: (NSString *) movieName { if (![name isEqualToString: movieName]) { name = movieName; } } … @end Usage Movie *myMovie = [[Movie alloc] init]; [myMovie setName:@”Dhoom”]; NSString *movieName = [myMovie name]; Yahoo! Confidential 29
    30. 30. Objective C Basics Using @property and @synthesize directive  Adding getter / setter methods for all the instance variables can become a tedious task.  Apple provide a simple way for this, you can use @property directive  Benefits  You do not have to write getter and setter methods yourself.  You can define the "assigning behavior" (namely copy, strong, weak, nonatomic) Keywords: • copy: The object is copied to the ivar when set • strong: The object is retained on set • weak: The object's pointer is assigned to the ivar when set and will be set to nil automatically when the instance is deallocated • nonatomic: The accessor is not @synchronized (threadsafe), and therefore faster • atomic: The accessor is @synchronized (threadsafe), and therefore slower Yahoo! Confidential 30
    31. 31. Objective C Basics Using @property and @synthesize directive @interface Movie: NSObject { NSString *name; } @property (nonatomic, strong) NSString *name; - (NSString *) name; - (void) setName: (NSString *) movieName; + (Movie *) createMovieWithName: (NSString *) movieName; @end Yahoo! Confidential 31
    32. 32. Objective C Basics … @implementation Movie @synthesize name; - (NSString *) name { return name; } - (void) setName: (NSString *) movieName { if (![name isEqualToString: movieName]) { name = movieName; } } … @end Yahoo! Confidential 32
    33. 33. Objective C Basics To sum up: NSString *name; - declares an instance variable 'name' @property (nonatomic, strong) NSString *name; - declares the accessor methods for 'name' @synthesize name; - implements the accessor methods for 'name' Yahoo! Confidential 33
    34. 34. Objective C Basics Using strings (NSString class) NSString *movieName = @”Dhoom”; The @ symbol Ok, why does this funny @ sign show up all the time? Well, Objective-C is an extension of the C-language, which has its own ways to deal with strings. To differentiate the new type of strings, which are fully-fledged objects, Objective-C uses an @ sign. A new kind of string How does Objective-C improve on strings of the C language? Well, Objective-C strings are Unicode strings instead of ASCII strings. Unicode-strings can display characters of just about any language, such as Chinese, as well as the Roman alphabet. A C string is simply a series of characters (a one-dimensional character array) that is nullterminated, whereas an NSString object is a complete object with class methods, instance methods, etc. Note: It is possible (but not recommended) to use C language strings in Objective C. Yahoo! Confidential 34
    35. 35. Objective C Basics Using ‘stringWithFormat’ NSString *movieName = [NSString stringWithFormat:@”Dhoom %d”, 2]; // Dhoom 2 Specifiers: %d %f %@ Signed 32-bit integer (int) 64-bit floating-point number (double) Objective-C object Complete list of format specifiers is available here. Introduction to NSLog NSLog(@”Movie name is %@”, movieName);     Format specifiers same as ‘stringWithFormat’. Used for debugging Same as error_log in PHP or console.log in JavaScript Note the use of round brackets unlike other method calls in objective C Yahoo! Confidential 35
    36. 36. Objective C Basics Using arrays (NSArray class)  Provide random access  The objects contained in an array do not all have to be of the same type. Factory methods (static methods that build array from given parameters): + (id)array Creates and returns an empty array + (id)arrayWithObjects Creates and returns an array containing a given object Lot of such factory methods available Accessing the NSArray - (BOOL)containsObject:(id)anObject - (NSUInteger)count - (id)lastObject - (id)objectAtIndex:(NSUInteger)index Yahoo! Confidential Returns true if a given object is found in the array Returns the size of the array Returns the last object in the array Returns the object at a given index. 36
    37. 37. Objective C Basics Introduction to NSMutableArray  NSArray is immutable (content of array cannot be modified without recreating it)  You can create mutable arrays (NSMutableArray) if you want to add or remove elements after creating. Additional functions to manipulate the array insertObject:atIndex: removeObjectAtIndex: addObject: removeLastObject replaceObjectAtIndex:withObject: Yahoo! Confidential 37
    38. 38. Objective C Basics Introduction to NSDictionary  NSDictionary are like Maps and Hashes in other languages  Key-value pairs  It is an unordered collection of objects Factory methods (static methods that build array from given parameters): + (id)dictionary Creates and returns an empty dictionary + (id)dictionaryWithObjects: forKeys: Creates and returns a dictionary containing entries constructed from the contents of an array of keys and an array of values Lot of such factory methods available Accessing the NSDictionary – allKeys – allValues – objectForKey: Yahoo! Confidential Returns a new array containing the dictionary’s keys. Returns a new array containing the dictionary’s values. Returns the value associated with a given key. 38
    39. 39. Objective C Basics Introduction to NSMutableDictionary  Similar to NSArray, NSDictionary is also immutable  You can create mutable dictionary (NSMutableDictionary) if you want to add or remove objects after creating. Additional functions to manipulate the dictionary setObject:forKey: removeObjectForKey: removeAllObjects: removeObjectsForKeys: Points to note:  NSArray and NSDictionary only store objects  So if you want to store numbers then you have to convert it to NSNumber  Use NSNull for empty values Yahoo! Confidential 39
    40. 40. Topics  Getting Started  Introduction to Xcode and Application settings  Understanding App execution flow  Introduction to Objective-C  Writing your First iPhone Application  Introduction to Interface Builder  Outlets and Actions  Storyboards  Using the iPhone/iPad Simulator Yahoo! Confidential 40

    ×