Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-C: Day 1

Objective-C: Day 1

Jayson Basañes

July 16, 2012
Tweet

More Decks by Jayson Basañes

Other Decks in Programming

Transcript

  1. Headers  and  Implementa&ons   #import <Foundation/Foundation.h> @interface PPlace : NSObject

    { } @property (nonatomic) NSUInteger placeId; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *address; - (void) findPlace:(NSString *)searchText; @end #import "PPlace.h" @implementation PPlace @synthesize placeId; @synthesize name; @synthesize address; - (void) findPlace:(NSString *)searchText { // do something } @end PPlace.h   PPlace.m  
  2. You  don’t  need  implementa&ons  for…   #define B_RELEASE_SAFELY(__POINTER) { [__POINTER

    release]; __POINTER = nil; } #define B_UI_IS_IPAD() UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad #define B_UI_ORIENTATION_IS_PORTRAIT() UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) #define B_UI_ORIENTATION_IS_LANDSCAPE() UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) MyMacros.h  
  3. You  don’t  need  implementa&ons  for…   MyControllerDelegate.h   #import <Foundation/Foundation.h>!

    #import <UIKit/UIKit.h>! ! @protocol FControllerDelegate ! ! @optional! ! - (void) controllerDidFinish:(UIViewController *)controller;! ! @end!
  4. You  don’t  need  implementa&ons  for…   AFNetworking.h   #import <Foundation/Foundation.h>!

    #import <Availability.h>! ! #ifndef _AFNETWORKING_! #define _AFNETWORKING_! ! #import "AFURLConnectionOperation.h"! ! #import "AFHTTPRequestOperation.h"! #import "AFJSONRequestOperation.h"! #import "AFXMLRequestOperation.h"! #import "AFPropertyListRequestOperation.h"! #import "AFHTTPClient.h"! ! #import "AFImageRequestOperation.h"! ! #if __IPHONE_OS_VERSION_MIN_REQUIRED! #import "AFNetworkActivityIndicatorManager.h"! #import "UIImageView+AFNetworking.h"! #endif! ! #endif /* _AFNETWORKING_ */  
  5. Prefix  headers   MyApp-­‐Prefix.pch   import <Availability.h>! ! #include <sys/attr.h>!

    ! #ifndef __IPHONE_3_0! #warning "This project uses features only available in iPhone SDK 3.0 and later."! #endif! ! ! #ifdef __OBJC__! #import <Foundation/Foundation.h>! #import <UIKit/UIKit.h>! ! #import "PGlobals.h"! #import "PMacros.h"! #import "PCGeometry.h"! #import "NSDate+RFC2822.h"! #import "NSDictionary+PAdditions.h"! #import "NSDateFormatter+PAdditions.h"! #import "GTMNSString+HTML.h"! #import "UINavigationBar+CustomImage.h"! #endif  
  6. Classes   #import <Foundation/ Foundation.h>! ! @interface Photo : NSObject

    {! ! }! ! @end   #import "Photo.h"! ! @implementation Photo! ! @end   class Photo { }   Photo.php   Photo.h   Photo.m  
  7. Class  Instance  (Object)   Photo *photo = [[Photo alloc] init];!

    ! [photo release];   $photo = new Photo();  
  8. Methods   @interface Photo : NSObject {! ! }! !

    - (void)printCaption;! ! @end   @implementation Photo! ! - (void)printCaption! {! NSLog(@"I have no caption!");! }! ! @end   class Photo { public function printCaption() { echo 'I have no caption!'; } }
  9. Methods   Photo *photo = [[Photo alloc] init];! [photo printCaption];

    // prints "I have no caption!"! ! [photo release];   $photo = new Photo(); $photo->printCaption();  
  10. Instance  Variables  (ivars)   @interface Photo : NSObject {! //

    can only be accessed inside Photo! NSString *caption; ! }! ! - (void)printCaption;! ! @end   @implementation Photo! ! - (void)printCaption! { ! NSLog(@"My caption is %@", caption);! }! ! @end   class Photo { protected $caption; public function printCaption() { echo 'My caption is ' . $this->caption; } }  
  11. Constructors   @interface Photo : NSObject {! NSString *caption; !

    }! ! - (id)initWithCaption:(NSString *)caption;! - (void)printCaption;! ! @end   @implementation Photo! ! - (id)initWithCaption:(NSString *)theCaption! {! self = [super init];! if (self) {! caption = [theCaption retain]; ! }! ! return self;! }! ! - (void)printCaption! { ! NSLog(@"My caption is %@", caption);! }! ! @end  
  12. Constructors   class Photo { protected $caption; public function __construct($caption

    = null) { $this->caption = $caption; } public function printCaption() { echo 'My caption is ' . $this->caption; } }
  13. Constructors   Photo *photo = [[Photo alloc] initWithCaption:@"Fart!"];! [photo printCaption];!

    ! [photo release];   $photo = new Photo('Fart!'); $photo->printCaption();   My  cap&on  is  Fart!  
  14. Methods  that  return  stuffs   @interface Photo : NSObject {!

    NSString *caption; ! }! ! - (id)initWithCaption:(NSString *)caption;! - (NSString *)caption;! ! @end   @implementation Photo! ! ...! ! - (NSString *)caption! {! return caption;! }! ! @end   class Photo { protected $caption; ... public function getCaption() { return $this->caption; } }  
  15. Methods  that  return  stuffs   Photo *photo = [[Photo alloc]

    initWithCaption:@"Fart!"];! ! NSString *caption = [photo caption];! NSLog(@"The caption is: %@", caption);! ! [photo release];   $photo = new Photo('Fart!'); echo 'The caption is: ' . $photo->getCaption();   The  cap&on  is:  Fart!  
  16. Tip   You  can  call  methods  on  nil  objects.  The

     method  call  will  just  do   nothing.  For  methods  that  return  something,  nil  will  be   returned.   Photo *photo = nil;! ! [photo printCaption]; // does nothing! ! NSString *caption = [photo caption]; // nil!
  17. Inheritance   #import "Photo.h"! ! @interface EpicPhoto : Photo! !

    @end   #import "EpicPhoto.h"! ! @implementation EpicPhoto! ! - (void)printCaption! {! [super printCaption];! ! NSLog(@"And %@ is epic.", caption);! }! ! @end   class EpicPhoto extends Photo { public function printCaption() { parent::printCaption(); echo 'And ' . $this->caption . ' is epic.'; } }  
  18. Inheritance   EpicPhoto *photo = [[EpicPhoto alloc] ! initWithCaption:@"Fart!"];! [photo

    printCaption];! ! [photo release];   $photo = new EpicPhoto('Fart!'); $photo->printCaption();   My  cap&on  is  Fart!  And  Fart!  Is  epic.  
  19. Exercise   Create  a  base  class  named  FUser.  The  class

     should  have  these  methods:     •  canCreateUsers  -­‐  returns  a  boolean  (BOOL)  with  value  NO.   •  fullName  -­‐  returns  a  concatenated  first  name  and  last  name.  Returns  an   NSString.     The  class  should  have  an  init  method  where  the  first  name  and  last  name  can  be   passed.     Create  a  subclass  of  FUser  and  name  it  FAdminUser.  The  method  -­‐canCreateUsers   of  FAdminUser  should  return  YES.  The  -­‐fullName  method  should  append  the   string  "  (Admin)"  when  returning   the  result.     Make  a  sample  usage  (NSLog)  in  your  app  delegate.