SlideShare uma empresa Scribd logo
1 de 149
Baixar para ler offline
CS193P - Lecture 3
           iPhone Application Development

           Custom Classes
           Object Lifecycle
           Autorelease
           Properties




Tuesday, January 12, 2010                   1
Announcements
        • Assignments 1A and 1B due Wednesday 1/13 at 11:59 PM
            ■ Enrolled Stanford students can email cs193p@cs.stanford.edu
              with any questions
            ■ Submit early! Instructions on the website...

                 ■   Delete the “build” directory manually, Xcode won’t do it




Tuesday, January 12, 2010                                                       2
Announcements
        • Assignments 2A and 2B due Wednesday 1/20 at 11:59 PM
            ■   2A: Continuation of Foundation tool
                 ■ Add custom class
                 ■ Basic memory management

            ■   2B: Beginning of first iPhone application
                 ■ Topics to be covered on Thursday, 1/14
                 ■ Assignment contains extensive walkthrough




Tuesday, January 12, 2010                                        3
Enrolled students & iTunes U
        • Lectures have begun showing up on iTunes U
        • Lead time is longer than last year
        • Come to class!!
            ■   Lectures may not post in time for assignments




Tuesday, January 12, 2010                                       4
Office Hours
        • Paul’s office hours: Thursday 2-4, Gates B26B
        • David’s office hours: Mondays 4-6pm: Gates 360




Tuesday, January 12, 2010                                  5
Today’s Topics
        • Questions from Assignment 1A or 1B?
        • Creating Custom Classes
        • Object Lifecycle
        • Autorelease
        • Objective-C Properties




Tuesday, January 12, 2010                       6
Custom Classes




Tuesday, January 12, 2010   7
Design Phase




Tuesday, January 12, 2010   8
Design Phase
        • Create a class
            ■   Person




Tuesday, January 12, 2010   8
Design Phase
        • Create a class
            ■   Person
        • Determine the superclass
            ■   NSObject (in this case)




Tuesday, January 12, 2010                 8
Design Phase
        • Create a class
            ■   Person
        • Determine the superclass
            ■   NSObject (in this case)
        • What properties should it have?
            ■   Name, age, whether they can vote




Tuesday, January 12, 2010                          8
Design Phase
        • Create a class
            ■   Person
        • Determine the superclass
            ■   NSObject (in this case)
        • What properties should it have?
            ■   Name, age, whether they can vote
        • What actions can it perform?
            ■   Cast a ballot




Tuesday, January 12, 2010                          8
Defining a class
                 A public header and a private implementation




                            Header File   Implementation File




Tuesday, January 12, 2010                                       9
Defining a class
                 A public header and a private implementation




                            Header File   Implementation File




Tuesday, January 12, 2010                                       9
Class interface declared in header file




Tuesday, January 12, 2010                        10
Class interface declared in header file

         @interface Person




         @end

Tuesday, January 12, 2010                        10
Class interface declared in header file

         @interface Person   : NSObject




         @end

Tuesday, January 12, 2010                        10
Class interface declared in header file
         #import <Foundation/Foundation.h>

         @interface Person   : NSObject




         @end

Tuesday, January 12, 2010                        10
Class interface declared in header file
         #import <Foundation/Foundation.h>

         @interface Person   : NSObject
         {




         }




         @end

Tuesday, January 12, 2010                        10
Class interface declared in header file
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }




         @end

Tuesday, January 12, 2010                        10
Class interface declared in header file
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }

         // method declarations
         - (NSString *)name;
         - (void)setName:(NSString *)value;

         - (int)age;
         - (void)setAge:(int)age;

         - (BOOL)canLegallyVote;
         - (void)castBallot;
         @end

Tuesday, January 12, 2010                        10
Defining a class
                 A public header and a private implementation




                            Header File   Implementation File




Tuesday, January 12, 2010                                       11
Implementing custom class
        • Implement setter/getter methods
        • Implement action methods




Tuesday, January 12, 2010                   12
Class Implementation




Tuesday, January 12, 2010     13
Class Implementation
         #import "Person.h"




Tuesday, January 12, 2010     13
Class Implementation
         #import "Person.h"

         @implementation Person




         @end




Tuesday, January 12, 2010         13
Class Implementation
         #import "Person.h"

         @implementation Person

         - (int)age {
            return age;
         }
         - (void)setAge:(int)value {
            age = value;
         }

         //... and other methods

         @end




Tuesday, January 12, 2010              13
Calling your own methods




Tuesday, January 12, 2010         14
Calling your own methods
         #import "Person.h"

         @implementation Person

         - (BOOL)canLegallyVote {

         }

         - (void)castBallot {




         }

         @end



Tuesday, January 12, 2010           14
Calling your own methods
         #import "Person.h"

         @implementation Person

         - (BOOL)canLegallyVote {
            return ([self age] >= 18);
         }

         - (void)castBallot {




         }

         @end



Tuesday, January 12, 2010                14
Calling your own methods
         #import "Person.h"

         @implementation Person

         - (BOOL)canLegallyVote {
            return ([self age] >= 18);
         }

         - (void)castBallot {
               if ([self canLegallyVote]) {
               	 	 // do voting stuff
               } else {
               	 	 NSLog (@“I’m not allowed to vote!”);
               }
         }

         @end



Tuesday, January 12, 2010                                 14
Superclass methods
        • As we just saw, objects have an implicit variable named “self”
            ■   Like “this” in Java and C++
        • Can also invoke superclass methods using “super”




Tuesday, January 12, 2010                                                  15
Superclass methods
        • As we just saw, objects have an implicit variable named “self”
            ■   Like “this” in Java and C++
        • Can also invoke superclass methods using “super”
                - (void)doSomething {
                  // Call superclass implementation first
                  [super doSomething];

                    // Then do our custom behavior
                    int foo = bar;
                    // ...
                }




Tuesday, January 12, 2010                                                  15
Object Lifecycle




Tuesday, January 12, 2010   16
Object Lifecycle
        • Creating objects
        • Memory management
        • Destroying objects




Tuesday, January 12, 2010      17
Object Creation




Tuesday, January 12, 2010   18
Object Creation
       • Two step process
            ■ allocate memory to store the object
            ■ initialize object state




Tuesday, January 12, 2010                           18
Object Creation
       • Two step process
            ■ allocate memory to store the object
            ■ initialize object state

         + alloc
            ■   Class method that knows how much memory is needed




Tuesday, January 12, 2010                                           18
Object Creation
       • Two step process
            ■ allocate memory to store the object
            ■ initialize object state

         + alloc
            ■   Class method that knows how much memory is needed
         - init
            ■   Instance method to set initial values, perform other setup




Tuesday, January 12, 2010                                                    18
Create = Allocate + Initialize




Tuesday, January 12, 2010               19
Create = Allocate + Initialize
         Person *person = nil;




Tuesday, January 12, 2010               19
Create = Allocate + Initialize
         Person *person = nil;

         person = [[Person alloc] init];




Tuesday, January 12, 2010                  19
Implementing your own -init method
         #import "Person.h"

         @implementation Person




         @end



Tuesday, January 12, 2010                   20
Implementing your own -init method
         #import "Person.h"

         @implementation Person

         - (id)init {




         }

         @end



Tuesday, January 12, 2010                   20
Implementing your own -init method
         #import "Person.h"

         @implementation Person

         - (id)init {
           // allow superclass to initialize its state first
           if (self = [super init]) {




             }

             return self;
         }

         @end



Tuesday, January 12, 2010                                      20
Implementing your own -init method
         #import "Person.h"

         @implementation Person

         - (id)init {
           // allow superclass to initialize its state first
           if (self = [super init]) {
                     age = 0;
                     name = @“Bob”;

                     // do other initialization...
             }

             return self;
         }

         @end



Tuesday, January 12, 2010                                      20
Multiple init methods
        • Classes may define multiple init methods
            - (id)init;
            - (id)initWithName:(NSString *)name;
            - (id)initWithName:(NSString *)name age:(int)age;




Tuesday, January 12, 2010                                       21
Multiple init methods
        • Classes may define multiple init methods
            - (id)init;
            - (id)initWithName:(NSString *)name;
            - (id)initWithName:(NSString *)name age:(int)age;


        • Less specific ones typically call more specific with default values
            - (id)init {
                return [self initWithName:@“No Name”];
            }

            - (id)initWithName:(NSString *)name {
                return [self initWithName:name age:0];
            }




Tuesday, January 12, 2010                                                       21
Finishing Up With an Object
         Person *person = nil;

         person = [[Person alloc] init];




Tuesday, January 12, 2010                  22
Finishing Up With an Object
         Person *person = nil;

         person = [[Person alloc] init];

         [person setName:@“Jimmy Jones”];
         [person setAge:32];

         [person castBallot];
         [person doSomethingElse];




Tuesday, January 12, 2010                   22
Finishing Up With an Object
         Person *person = nil;

         person = [[Person alloc] init];

         [person setName:@“Jimmy Jones”];
         [person setAge:32];

         [person castBallot];
         [person doSomethingElse];

         // What do we do with person when we’re done?




Tuesday, January 12, 2010                                22
Memory Management

                                          Allocation   Destruction
                                C          malloc         free

                            Objective-C    alloc        dealloc




Tuesday, January 12, 2010                                            23
Memory Management

                                          Allocation   Destruction
                                C          malloc         free

                            Objective-C    alloc        dealloc




Tuesday, January 12, 2010                                            23
Memory Management

                                          Allocation   Destruction
                                C          malloc         free

                            Objective-C    alloc        dealloc




Tuesday, January 12, 2010                                            23
Memory Management

                                          Allocation   Destruction
                                C          malloc         free

                            Objective-C    alloc        dealloc


       • Calls must be balanced
            ■   Otherwise your program may leak or crash




Tuesday, January 12, 2010                                            23
Memory Management

                                          Allocation   Destruction
                                C          malloc         free

                            Objective-C     alloc       dealloc


       • Calls must be balanced
            ■   Otherwise your program may leak or crash
       • However, you’ll never call -dealloc directly
            ■   One exception, we’ll see in a bit...




Tuesday, January 12, 2010                                            23
Reference Counting




Tuesday, January 12, 2010   24
Reference Counting
        • Every object has a retain count
            ■ Defined on NSObject
            ■ As long as retain count is > 0, object is alive and valid




Tuesday, January 12, 2010                                                 24
Reference Counting
        • Every object has a retain count
            ■ Defined on NSObject
            ■ As long as retain count is > 0, object is alive and valid


        • +alloc and -copy create objects with retain count == 1




Tuesday, January 12, 2010                                                 24
Reference Counting
        • Every object has a retain count
            ■ Defined on NSObject
            ■ As long as retain count is > 0, object is alive and valid


        • +alloc and -copy create objects with retain count == 1
        • -retain increments retain count




Tuesday, January 12, 2010                                                 24
Reference Counting
        • Every object has a retain count
            ■ Defined on NSObject
            ■ As long as retain count is > 0, object is alive and valid


        • +alloc and -copy create objects with retain count == 1
        • -retain increments retain count
        • -release decrements retain count




Tuesday, January 12, 2010                                                 24
Reference Counting
        • Every object has a retain count
            ■ Defined on NSObject
            ■ As long as retain count is > 0, object is alive and valid


        • +alloc and -copy create objects with retain count == 1
        • -retain increments retain count
        • -release decrements retain count
        • When retain count reaches 0, object is destroyed
            • -dealloc method invoked automatically
            ■   One-way street, once you’re in -dealloc there’s no turning back




Tuesday, January 12, 2010                                                         24
Balanced Calls
         Person *person = nil;

         person = [[Person alloc] init];




Tuesday, January 12, 2010                  25
Balanced Calls
         Person *person = nil;

         person = [[Person alloc] init];

         [person setName:@“Jimmy Jones”];
         [person setAge:32];

         [person castBallot];
         [person doSomethingElse];




Tuesday, January 12, 2010                   25
Balanced Calls
         Person *person = nil;

         person = [[Person alloc] init];

         [person setName:@“Jimmy Jones”];
         [person setAge:32];

         [person castBallot];
         [person doSomethingElse];

         // When we’re done with person, release it
         [person release];   // person will be destroyed here




Tuesday, January 12, 2010                                       25
Reference counting in action




Tuesday, January 12, 2010             26
Reference counting in action
         Person *person = [[Person alloc] init];




Tuesday, January 12, 2010                          26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc




Tuesday, January 12, 2010                             26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc
         [person retain];




Tuesday, January 12, 2010                             26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc
         [person retain];

               Retain count increases to 2 with -retain




Tuesday, January 12, 2010                                 26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc
         [person retain];

               Retain count increases to 2 with -retain
         [person release];




Tuesday, January 12, 2010                                 26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc
         [person retain];

               Retain count increases to 2 with -retain
         [person release];

               Retain count decreases to 1 with -release




Tuesday, January 12, 2010                                  26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc
         [person retain];

               Retain count increases to 2 with -retain
         [person release];

               Retain count decreases to 1 with -release
         [person release];




Tuesday, January 12, 2010                                  26
Reference counting in action
         Person *person = [[Person alloc] init];

               Retain count begins at 1 with +alloc
         [person retain];

               Retain count increases to 2 with -retain
         [person release];

               Retain count decreases to 1 with -release
         [person release];

               Retain count decreases to 0, -dealloc automatically called




Tuesday, January 12, 2010                                                   26
Messaging deallocated objects




Tuesday, January 12, 2010              27
Messaging deallocated objects
           Person *person = [[Person alloc] init];
           // ...
           [person release]; // Object is deallocated




Tuesday, January 12, 2010                               27
Messaging deallocated objects
           Person *person = [[Person alloc] init];
           // ...
           [person release]; // Object is deallocated



           [person doSomething]; // Crash!




Tuesday, January 12, 2010                               27
Messaging deallocated objects
           Person *person = [[Person alloc] init];
           // ...
           [person release]; // Object is deallocated




Tuesday, January 12, 2010                               27
Messaging deallocated objects
           Person *person = [[Person alloc] init];
           // ...
           [person release]; // Object is deallocated
           person = nil;




Tuesday, January 12, 2010                               27
Messaging deallocated objects
           Person *person = [[Person alloc] init];
           // ...
           [person release]; // Object is deallocated
           person = nil;

           [person doSomething]; // No effect




Tuesday, January 12, 2010                               27
Implementing a -dealloc method
         #import "Person.h"

         @implementation Person




         @end




Tuesday, January 12, 2010               28
Implementing a -dealloc method
         #import "Person.h"

         @implementation Person

         - (void)dealloc {




         }

         @end




Tuesday, January 12, 2010               28
Implementing a -dealloc method
         #import "Person.h"

         @implementation Person

         - (void)dealloc {
              // Do any cleanup that’s necessary
              // ...




         }

         @end




Tuesday, January 12, 2010                          28
Implementing a -dealloc method
         #import "Person.h"

         @implementation Person

         - (void)dealloc {
              // Do any cleanup that’s necessary
              // ...

                 // when we’re done, call super to clean us up
                 [super dealloc];
         }

         @end




Tuesday, January 12, 2010                                        28
Object Lifecycle Recap
       • Objects begin with a retain count of 1
       • Increase and decrease with -retain and -release
       • When retain count reaches 0, object deallocated automatically
       • You never call dealloc explicitly in your code
            ■ Exception is calling -[super dealloc]
            ■ You only deal with alloc, copy, retain, release




Tuesday, January 12, 2010                                                29
Object Ownership
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name; // Person class “owns” the name
            int age;
         }

         // method declarations
         - (NSString *)name;
         - (void)setName:(NSString *)value;

         - (int)age;
         - (void)setAge:(int)age;

         - (BOOL)canLegallyVote;
         - (void)castBallot;
         @end

Tuesday, January 12, 2010                                     30
Object Ownership
         #import "Person.h"

         @implementation Person




         @end




Tuesday, January 12, 2010         31
Object Ownership
         #import "Person.h"

         @implementation Person

         - (NSString *)name {
            return name;
         }
         - (void)setName:(NSString *)newName {




         }

         @end




Tuesday, January 12, 2010                        31
Object Ownership
         #import "Person.h"

         @implementation Person

         - (NSString *)name {
            return name;
         }
         - (void)setName:(NSString *)newName {
               if (name != newName) {
                   [name release];
                   name = [newName retain];
                    // name’s retain count has been bumped up by 1
               }
         }

         @end




Tuesday, January 12, 2010                                            31
Object Ownership
         #import "Person.h"

         @implementation Person

         - (NSString *)name {
            return name;
         }
         - (void)setName:(NSString *)newName {




         }

         @end




Tuesday, January 12, 2010                        31
Object Ownership
         #import "Person.h"

         @implementation Person

         - (NSString *)name {
            return name;
         }
         - (void)setName:(NSString *)newName {
               if (name != newName) {
                   [name release];
                   name = [newName copy];
                    // name has retain count of 1, we own it
               }
         }

         @end




Tuesday, January 12, 2010                                      31
Releasing Instance Variables
         #import "Person.h"

         @implementation Person




         @end




Tuesday, January 12, 2010             32
Releasing Instance Variables
         #import "Person.h"

         @implementation Person

         - (void)dealloc {




         }

         @end




Tuesday, January 12, 2010             32
Releasing Instance Variables
         #import "Person.h"

         @implementation Person

         - (void)dealloc {
             // Do any cleanup that’s necessary
              [name release];




         }

         @end




Tuesday, January 12, 2010                         32
Releasing Instance Variables
         #import "Person.h"

         @implementation Person

         - (void)dealloc {
             // Do any cleanup that’s necessary
              [name release];

                 // when we’re done, call super to clean us up
                 [super dealloc];
         }

         @end




Tuesday, January 12, 2010                                        32
Autorelease




Tuesday, January 12, 2010   33
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];


                   return result;
             }




Tuesday, January 12, 2010                                                  34
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];


                   return result;
             }


                                Wrong: result is leaked!




Tuesday, January 12, 2010                                                  34
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];
                   [result release];

                   return result;
             }




Tuesday, January 12, 2010                                                  34
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];
                   [result release];

                   return result;
             }


                            Wrong: result is released too early!
                            Method returns bogus value




Tuesday, January 12, 2010                                                  34
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];


                   return result;
             }




Tuesday, January 12, 2010                                                  34
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];
                   [result autorelease];

                   return result;
             }




Tuesday, January 12, 2010                                                  34
Returning a newly created object
             - (NSString *)fullName {
                NSString *result;

                   result = [[NSString alloc] initWithFormat:@“%@ %@”,
                         	 	    	    	 	 	 	 	 	 	 firstName, lastName];
                   [result autorelease];

                   return result;
             }


                     Just right: result is released, but not right away
                     Caller gets valid object and could retain if needed




Tuesday, January 12, 2010                                                  34
Autoreleasing Objects
        • Calling -autorelease flags an object to be sent release at some
          point in the future
        • Let’s you fulfill your retain/release obligations while allowing an
          object some additional time to live
        • Makes it much more convenient to manage memory
        • Very useful in methods which return a newly created object




Tuesday, January 12, 2010                                                       35
Method Names & Autorelease




Tuesday, January 12, 2010           36
Method Names & Autorelease
        • Methods whose names includes alloc, copy, or new
          return a retained object that the caller needs to release




Tuesday, January 12, 2010                                             36
Method Names & Autorelease
        • Methods whose names includes alloc, copy, or new
          return a retained object that the caller needs to release
          NSMutableString *string = [[NSMutableString alloc] init];
          // We are responsible for calling -release or -autorelease
          [string autorelease];




Tuesday, January 12, 2010                                              36
Method Names & Autorelease
        • Methods whose names includes alloc, copy, or new
          return a retained object that the caller needs to release
          NSMutableString *string = [[NSMutableString alloc] init];
          // We are responsible for calling -release or -autorelease
          [string autorelease];


        • All other methods return autoreleased objects




Tuesday, January 12, 2010                                              36
Method Names & Autorelease
        • Methods whose names includes alloc, copy, or new
          return a retained object that the caller needs to release
          NSMutableString *string = [[NSMutableString alloc] init];
          // We are responsible for calling -release or -autorelease
          [string autorelease];


        • All other methods return autoreleased objects
          NSMutableString *string = [NSMutableString string];
          // The method name doesn’t indicate that we need to release it
          // So don’t- we’re cool!




Tuesday, January 12, 2010                                                  36
Method Names & Autorelease
        • Methods whose names includes alloc, copy, or new
          return a retained object that the caller needs to release
          NSMutableString *string = [[NSMutableString alloc] init];
          // We are responsible for calling -release or -autorelease
          [string autorelease];


        • All other methods return autoreleased objects
          NSMutableString *string = [NSMutableString string];
          // The method name doesn’t indicate that we need to release it
          // So don’t- we’re cool!


        • This is a convention- follow it in methods you define!



Tuesday, January 12, 2010                                                  36
How does -autorelease work?




Tuesday, January 12, 2010            37
How does -autorelease work?
        • Object is added to current autorelease pool




Tuesday, January 12, 2010                               37
How does -autorelease work?
        • Object is added to current autorelease pool
        • Autorelease pools track objects scheduled to be released
            ■   When the pool itself is released, it sends -release to all its objects




Tuesday, January 12, 2010                                                                37
How does -autorelease work?
        • Object is added to current autorelease pool
        • Autorelease pools track objects scheduled to be released
            ■   When the pool itself is released, it sends -release to all its objects
        • UIKit automatically wraps a pool around every event dispatch




Tuesday, January 12, 2010                                                                37
Autorelease Pools (in pictures)




                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t          pp
         nc                  ia                     i   n                 ve                 v        it a
       au                 nit                      a                   ore                lee       Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                       38
Autorelease Pools (in pictures)

              Pool




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                                                                                                         [object
                                                                                                         autorelease];




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                              Pool released




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)
                                                [object release];


              Pool                              [object release];
                                                                                                         Objects autoreleased
                                                                                                          here go into pool



   [object release];
                 Pool released




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Autorelease Pools (in pictures)

              Pool                                                                                       Objects autoreleased
                                                                                                          here go into pool




                              Pool released




                                                                                                    Pool created
                 pp                  d
              ha                lize                      nib                nt               en
                                                                                                t                       pp
         nc                  ia                     i   n                 ve                 v                     it a
       au                 nit                      a                   ore                lee                 Ex
     L                  pi                  a   dm                 it f                nd
                      Ap                 Lo                     Wa                Ha

Tuesday, January 12, 2010                                                                                                    38
Hanging Onto an Autoreleased Object
        • Many methods return autoreleased objects
            ■ Remember the naming conventions...
            ■ They’re hanging out in the pool and will get released later


        • If you need to hold onto those objects you need to retain them
            ■   Bumps up the retain count before the release happens




Tuesday, January 12, 2010                                                   39
Hanging Onto an Autoreleased Object
        • Many methods return autoreleased objects
            ■ Remember the naming conventions...
            ■ They’re hanging out in the pool and will get released later


        • If you need to hold onto those objects you need to retain them
            ■   Bumps up the retain count before the release happens

                name = [NSMutableString string];

                // We want to name to remain valid!
                [name retain];

                // ...
                // Eventually, we’ll release it (maybe in our -dealloc?)
                [name release];




Tuesday, January 12, 2010                                                   39
Side Note: Garbage Collection
        • Autorelease is not garbage collection
        • Objective-C on iPhone OS does not have garbage collection




Tuesday, January 12, 2010                                             40
Objective-C Properties




Tuesday, January 12, 2010        41
Properties
        • Provide access to object attributes
        • Shortcut to implementing getter/setter methods
        • Also allow you to specify:
            ■ read-only versus read-write access
            ■ memory management policy




Tuesday, January 12, 2010                                  42
Defining Properties
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }
         // method declarations
         - (NSString *) name;
         - (void)setName:(NSString *)value;
         - (int) age;
         - (void)setAge:(int)age;
         - (BOOL) canLegallyVote;


         - (void)castBallot;
         @end


Tuesday, January 12, 2010                     43
Defining Properties
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }
         // method declarations
         - (NSString *) name;
         - (void)setName:(NSString *)value;
         - (int) age;
         - (void)setAge:(int)age;
         - (BOOL) canLegallyVote;


         - (void)castBallot;
         @end


Tuesday, January 12, 2010                     43
Defining Properties
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }
         // method declarations
         - (NSString *) name;
         - (void)setName:(NSString *)value;
         - (int) age;
         - (void)setAge:(int)age;
         - (BOOL) canLegallyVote;


         - (void)castBallot;
         @end


Tuesday, January 12, 2010                     43
Defining Properties
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }


         // property declarations
         @property int age ;
         @property (copy) NSString * name;
         @property (readonly) BOOL canLegallyVote ;



         - (void)castBallot;
         @end


Tuesday, January 12, 2010                             43
Defining Properties
         #import <Foundation/Foundation.h>

         @interface Person : NSObject
         {
            // instance variables
            NSString *name;
            int age;
         }


         // property declarations
         @property int age;
         @property (copy) NSString *name;
         @property (readonly) BOOL canLegallyVote;



         - (void)castBallot;
         @end


Tuesday, January 12, 2010                            44
Synthesizing Properties
         @implementation Person

         - (int)age {
            return age;
         }
         - (void)setAge:(int)value {
            age = value;
         }
         - (NSString *)name {
             return name;
         }
         - (void)setName:(NSString *)value {
             if (value != name) {
                 [name release];
                 name = [value copy];
             }
         }
         - (void)canLegallyVote { ...


Tuesday, January 12, 2010                      45
Synthesizing Properties
         @implementation Person

         - (int)age {
            return age;
         }
         - (void)setAge:(int)value {
            age = value;
         }
         - (NSString *)name {
             return name;
         }
         - (void)setName:(NSString *)value {
             if (value != name) {
                 [name release];
                 name = [value copy];
             }
         }
         - (void)canLegallyVote { ...


Tuesday, January 12, 2010                      45
Synthesizing Properties
         @implementation Person

         - (int)age {
                age
            return age;
         }
         - (void)setAge:(int)value {
            age = value;
         }
         - (NSString *)name {
                       name
             return name;
         }
         - (void)setName:(NSString *)value {
             if (value != name) {
                 [name release];
                 name = [value copy];
             }
         }
         - (void)canLegallyVote { ...


Tuesday, January 12, 2010                      45
Synthesizing Properties
         @implementation Person

         @synthesize age;
         @synthesize name;
          - (BOOL)canLegallyVote {
             return (age > 17);
          }

          @end




Tuesday, January 12, 2010            46
Property Attributes
        • Read-only versus read-write
          ! @property int age; // read-write by default
          	 @property (readonly) BOOL canLegallyVote;


        • Memory management policies (only for object properties)
            @property (assign) NSString *name; // pointer assignment
          	 @property (retain) NSString *name; // retain called
          	 @property (copy) NSString *name;   // copy called




Tuesday, January 12, 2010                                              47
Property Names vs. Instance Variables
        • Property name can be different than instance variable
            @interface Person : NSObject {
          	 	 	 int numberOfYearsOld;
          	}

          	 @property int age;


          	 @end




Tuesday, January 12, 2010                                         48
Property Names vs. Instance Variables
        • Property name can be different than instance variable
             @interface Person : NSObject {
           	 	 	 int numberOfYearsOld;
           	}

           	 @property int age;


           	 @end


       	     @implementation Person

       	     @synthesize age = numberOfYearsOld;

       	     @end




Tuesday, January 12, 2010                                         48
Properties
        • Mix and match synthesized and implemented properties
              @implementation Person
              @synthesize age;
              @synthesize name;

              - (void)setAge:(int)value {
                  age = value;

                     // now do something with the new age value...
              }

              @end


        • Setter method explicitly implemented
        • Getter method still synthesized

Tuesday, January 12, 2010                                            49
Properties In Practice
        • Newer APIs use @property
        • Older APIs use getter/setter methods
        • Properties used heavily throughout UIKit APIs
            ■   Not so much with Foundation APIs
        • You can use either approach
            ■   Properties mean writing less code, but “magic” can sometimes
                be non-obvious




Tuesday, January 12, 2010                                                      50
Dot Syntax and self
        • When used in custom methods, be careful with dot syntax for
          properties defined in your class
        • References to properties and ivars behave very differently
             @interface Person : NSObject
             {
                NSString *name;
             }
             @property (copy) NSString *name;
             @end




Tuesday, January 12, 2010                                               51
Dot Syntax and self
        • When used in custom methods, be careful with dot syntax for
          properties defined in your class
        • References to properties and ivars behave very differently
             @interface Person : NSObject
             {
                NSString *name;
             }
             @property (copy) NSString *name;
             @end

             @implementation Person
             - (void)doSomething {
             	 	 name = @“Fred”;         // accesses ivar directly!
             	 	 self.name = @“Fred”;    // calls accessor method
             }




Tuesday, January 12, 2010                                               51
Common Pitfall with Dot Syntax
        What will happen when this code executes?
                    @implementation Person
                    - (void)setAge:(int)newAge {
                    	 	 self.age = newAge;
                    }
                    @end




Tuesday, January 12, 2010                           52
Common Pitfall with Dot Syntax
        What will happen when this code executes?
                    @implementation Person
                    - (void)setAge:(int)newAge {
                    	 	 self.age = newAge;
                    }
                    @end

        This is equivalent to:
                    @implementation Person
                    - (void)setAge:(int)newAge {
                    	 	 [self setAge:newAge]; // Infinite loop!
                    }
                    @end




Tuesday, January 12, 2010                                         52
Further Reading
        • Objective-C 2.0 Programming Language
            ■ “Defining a Class”
            ■ “Declared Properties”


        • Memory Management Programming Guide for Cocoa




Tuesday, January 12, 2010                                 53
Questions?




Tuesday, January 12, 2010   54

Mais conteúdo relacionado

Mais procurados

Frontend Engineer Toolbox
Frontend Engineer ToolboxFrontend Engineer Toolbox
Frontend Engineer ToolboxYnon Perek
 
NoSQL - Post-Relational Databases - BarCamp Ruhr3
NoSQL - Post-Relational Databases - BarCamp Ruhr3NoSQL - Post-Relational Databases - BarCamp Ruhr3
NoSQL - Post-Relational Databases - BarCamp Ruhr3Jonathan Weiss
 
The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...
The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...
The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...Charleston Conference
 
No SQL - BarCamp Nürnberg 2010
No SQL - BarCamp Nürnberg 2010No SQL - BarCamp Nürnberg 2010
No SQL - BarCamp Nürnberg 2010Jonathan Weiss
 
Sqlpo Presentation
Sqlpo PresentationSqlpo Presentation
Sqlpo Presentationsandook
 
Pinterest的数据库分片架构
Pinterest的数据库分片架构Pinterest的数据库分片架构
Pinterest的数据库分片架构Tommy Chiu
 

Mais procurados (6)

Frontend Engineer Toolbox
Frontend Engineer ToolboxFrontend Engineer Toolbox
Frontend Engineer Toolbox
 
NoSQL - Post-Relational Databases - BarCamp Ruhr3
NoSQL - Post-Relational Databases - BarCamp Ruhr3NoSQL - Post-Relational Databases - BarCamp Ruhr3
NoSQL - Post-Relational Databases - BarCamp Ruhr3
 
The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...
The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...
The Semantic Web for Librarians and Publishers, by Michael Keller, Stanford U...
 
No SQL - BarCamp Nürnberg 2010
No SQL - BarCamp Nürnberg 2010No SQL - BarCamp Nürnberg 2010
No SQL - BarCamp Nürnberg 2010
 
Sqlpo Presentation
Sqlpo PresentationSqlpo Presentation
Sqlpo Presentation
 
Pinterest的数据库分片架构
Pinterest的数据库分片架构Pinterest的数据库分片架构
Pinterest的数据库分片架构
 

Destaque

06 View Controllers
06 View Controllers06 View Controllers
06 View ControllersMahmoud
 
02 Objective C
02 Objective C02 Objective C
02 Objective CMahmoud
 
04 Model View Controller
04 Model View Controller04 Model View Controller
04 Model View ControllerMahmoud
 
01 Introduction
01 Introduction01 Introduction
01 IntroductionMahmoud
 
CS193P Lecture 5 View Animation
CS193P Lecture 5 View AnimationCS193P Lecture 5 View Animation
CS193P Lecture 5 View Animationonoaonoa
 

Destaque (8)

06 View Controllers
06 View Controllers06 View Controllers
06 View Controllers
 
02 Objective C
02 Objective C02 Objective C
02 Objective C
 
iOS: View Controllers
iOS: View ControllersiOS: View Controllers
iOS: View Controllers
 
04 Model View Controller
04 Model View Controller04 Model View Controller
04 Model View Controller
 
01 Introduction
01 Introduction01 Introduction
01 Introduction
 
05 Views
05 Views05 Views
05 Views
 
CS193P Lecture 5 View Animation
CS193P Lecture 5 View AnimationCS193P Lecture 5 View Animation
CS193P Lecture 5 View Animation
 
Animation in iOS
Animation in iOSAnimation in iOS
Animation in iOS
 

Semelhante a 03 Custom Classes

A Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemA Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemLeonard Axelsson
 
Plone Conference 2010 – Where we go from here
Plone Conference 2010 – Where we go from herePlone Conference 2010 – Where we go from here
Plone Conference 2010 – Where we go from hereEric Steele
 
Data and Information Extraction on the Web
Data and Information Extraction on the WebData and Information Extraction on the Web
Data and Information Extraction on the WebTommaso Teofili
 
Anatomy of a UI Control - Extension Library Case Study
Anatomy of a UI Control - Extension Library Case StudyAnatomy of a UI Control - Extension Library Case Study
Anatomy of a UI Control - Extension Library Case Studygregorbyte
 
Behat dpc12
Behat dpc12Behat dpc12
Behat dpc12benwaine
 
Intro to unity for as3
Intro to unity for as3Intro to unity for as3
Intro to unity for as3mrondina
 
Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDDSteven Mak
 
iInnovate Institute Welcome 2010
iInnovate Institute Welcome 2010iInnovate Institute Welcome 2010
iInnovate Institute Welcome 2010artugua
 
Fcc open-developer-day
Fcc open-developer-dayFcc open-developer-day
Fcc open-developer-dayTed Drake
 

Semelhante a 03 Custom Classes (9)

A Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemA Tour Through the Groovy Ecosystem
A Tour Through the Groovy Ecosystem
 
Plone Conference 2010 – Where we go from here
Plone Conference 2010 – Where we go from herePlone Conference 2010 – Where we go from here
Plone Conference 2010 – Where we go from here
 
Data and Information Extraction on the Web
Data and Information Extraction on the WebData and Information Extraction on the Web
Data and Information Extraction on the Web
 
Anatomy of a UI Control - Extension Library Case Study
Anatomy of a UI Control - Extension Library Case StudyAnatomy of a UI Control - Extension Library Case Study
Anatomy of a UI Control - Extension Library Case Study
 
Behat dpc12
Behat dpc12Behat dpc12
Behat dpc12
 
Intro to unity for as3
Intro to unity for as3Intro to unity for as3
Intro to unity for as3
 
Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDD
 
iInnovate Institute Welcome 2010
iInnovate Institute Welcome 2010iInnovate Institute Welcome 2010
iInnovate Institute Welcome 2010
 
Fcc open-developer-day
Fcc open-developer-dayFcc open-developer-day
Fcc open-developer-day
 

Mais de Mahmoud

مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟Mahmoud
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتكMahmoud
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغيرMahmoud
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمMahmoud
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائقMahmoud
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقريةMahmoud
 
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيهمهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيهMahmoud
 
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟Mahmoud
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغيرMahmoud
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائقMahmoud
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتكMahmoud
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمMahmoud
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقريةMahmoud
 
Accident Investigation
Accident InvestigationAccident Investigation
Accident InvestigationMahmoud
 
Investigation Skills
Investigation SkillsInvestigation Skills
Investigation SkillsMahmoud
 
Building Papers
Building PapersBuilding Papers
Building PapersMahmoud
 
Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02Mahmoud
 
Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01Mahmoud
 
A Basic Modern Russian Grammar
A Basic Modern Russian Grammar A Basic Modern Russian Grammar
A Basic Modern Russian Grammar Mahmoud
 

Mais de Mahmoud (20)

مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتك
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغير
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقرية
 
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيهمهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
 
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغير
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتك
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقرية
 
Accident Investigation
Accident InvestigationAccident Investigation
Accident Investigation
 
Investigation Skills
Investigation SkillsInvestigation Skills
Investigation Skills
 
Building Papers
Building PapersBuilding Papers
Building Papers
 
Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02
 
Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01
 
A Basic Modern Russian Grammar
A Basic Modern Russian Grammar A Basic Modern Russian Grammar
A Basic Modern Russian Grammar
 
Teams Ar
Teams ArTeams Ar
Teams Ar
 

Último

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

03 Custom Classes

  • 1. CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1
  • 2. Announcements • Assignments 1A and 1B due Wednesday 1/13 at 11:59 PM ■ Enrolled Stanford students can email cs193p@cs.stanford.edu with any questions ■ Submit early! Instructions on the website... ■ Delete the “build” directory manually, Xcode won’t do it Tuesday, January 12, 2010 2
  • 3. Announcements • Assignments 2A and 2B due Wednesday 1/20 at 11:59 PM ■ 2A: Continuation of Foundation tool ■ Add custom class ■ Basic memory management ■ 2B: Beginning of first iPhone application ■ Topics to be covered on Thursday, 1/14 ■ Assignment contains extensive walkthrough Tuesday, January 12, 2010 3
  • 4. Enrolled students & iTunes U • Lectures have begun showing up on iTunes U • Lead time is longer than last year • Come to class!! ■ Lectures may not post in time for assignments Tuesday, January 12, 2010 4
  • 5. Office Hours • Paul’s office hours: Thursday 2-4, Gates B26B • David’s office hours: Mondays 4-6pm: Gates 360 Tuesday, January 12, 2010 5
  • 6. Today’s Topics • Questions from Assignment 1A or 1B? • Creating Custom Classes • Object Lifecycle • Autorelease • Objective-C Properties Tuesday, January 12, 2010 6
  • 9. Design Phase • Create a class ■ Person Tuesday, January 12, 2010 8
  • 10. Design Phase • Create a class ■ Person • Determine the superclass ■ NSObject (in this case) Tuesday, January 12, 2010 8
  • 11. Design Phase • Create a class ■ Person • Determine the superclass ■ NSObject (in this case) • What properties should it have? ■ Name, age, whether they can vote Tuesday, January 12, 2010 8
  • 12. Design Phase • Create a class ■ Person • Determine the superclass ■ NSObject (in this case) • What properties should it have? ■ Name, age, whether they can vote • What actions can it perform? ■ Cast a ballot Tuesday, January 12, 2010 8
  • 13. Defining a class A public header and a private implementation Header File Implementation File Tuesday, January 12, 2010 9
  • 14. Defining a class A public header and a private implementation Header File Implementation File Tuesday, January 12, 2010 9
  • 15. Class interface declared in header file Tuesday, January 12, 2010 10
  • 16. Class interface declared in header file @interface Person @end Tuesday, January 12, 2010 10
  • 17. Class interface declared in header file @interface Person : NSObject @end Tuesday, January 12, 2010 10
  • 18. Class interface declared in header file #import <Foundation/Foundation.h> @interface Person : NSObject @end Tuesday, January 12, 2010 10
  • 19. Class interface declared in header file #import <Foundation/Foundation.h> @interface Person : NSObject { } @end Tuesday, January 12, 2010 10
  • 20. Class interface declared in header file #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } @end Tuesday, January 12, 2010 10
  • 21. Class interface declared in header file #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; - (void)castBallot; @end Tuesday, January 12, 2010 10
  • 22. Defining a class A public header and a private implementation Header File Implementation File Tuesday, January 12, 2010 11
  • 23. Implementing custom class • Implement setter/getter methods • Implement action methods Tuesday, January 12, 2010 12
  • 25. Class Implementation #import "Person.h" Tuesday, January 12, 2010 13
  • 26. Class Implementation #import "Person.h" @implementation Person @end Tuesday, January 12, 2010 13
  • 27. Class Implementation #import "Person.h" @implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } //... and other methods @end Tuesday, January 12, 2010 13
  • 28. Calling your own methods Tuesday, January 12, 2010 14
  • 29. Calling your own methods #import "Person.h" @implementation Person - (BOOL)canLegallyVote { } - (void)castBallot { } @end Tuesday, January 12, 2010 14
  • 30. Calling your own methods #import "Person.h" @implementation Person - (BOOL)canLegallyVote { return ([self age] >= 18); } - (void)castBallot { } @end Tuesday, January 12, 2010 14
  • 31. Calling your own methods #import "Person.h" @implementation Person - (BOOL)canLegallyVote { return ([self age] >= 18); } - (void)castBallot { if ([self canLegallyVote]) { // do voting stuff } else { NSLog (@“I’m not allowed to vote!”); } } @end Tuesday, January 12, 2010 14
  • 32. Superclass methods • As we just saw, objects have an implicit variable named “self” ■ Like “this” in Java and C++ • Can also invoke superclass methods using “super” Tuesday, January 12, 2010 15
  • 33. Superclass methods • As we just saw, objects have an implicit variable named “self” ■ Like “this” in Java and C++ • Can also invoke superclass methods using “super” - (void)doSomething { // Call superclass implementation first [super doSomething]; // Then do our custom behavior int foo = bar; // ... } Tuesday, January 12, 2010 15
  • 35. Object Lifecycle • Creating objects • Memory management • Destroying objects Tuesday, January 12, 2010 17
  • 37. Object Creation • Two step process ■ allocate memory to store the object ■ initialize object state Tuesday, January 12, 2010 18
  • 38. Object Creation • Two step process ■ allocate memory to store the object ■ initialize object state + alloc ■ Class method that knows how much memory is needed Tuesday, January 12, 2010 18
  • 39. Object Creation • Two step process ■ allocate memory to store the object ■ initialize object state + alloc ■ Class method that knows how much memory is needed - init ■ Instance method to set initial values, perform other setup Tuesday, January 12, 2010 18
  • 40. Create = Allocate + Initialize Tuesday, January 12, 2010 19
  • 41. Create = Allocate + Initialize Person *person = nil; Tuesday, January 12, 2010 19
  • 42. Create = Allocate + Initialize Person *person = nil; person = [[Person alloc] init]; Tuesday, January 12, 2010 19
  • 43. Implementing your own -init method #import "Person.h" @implementation Person @end Tuesday, January 12, 2010 20
  • 44. Implementing your own -init method #import "Person.h" @implementation Person - (id)init { } @end Tuesday, January 12, 2010 20
  • 45. Implementing your own -init method #import "Person.h" @implementation Person - (id)init { // allow superclass to initialize its state first if (self = [super init]) { } return self; } @end Tuesday, January 12, 2010 20
  • 46. Implementing your own -init method #import "Person.h" @implementation Person - (id)init { // allow superclass to initialize its state first if (self = [super init]) { age = 0; name = @“Bob”; // do other initialization... } return self; } @end Tuesday, January 12, 2010 20
  • 47. Multiple init methods • Classes may define multiple init methods - (id)init; - (id)initWithName:(NSString *)name; - (id)initWithName:(NSString *)name age:(int)age; Tuesday, January 12, 2010 21
  • 48. Multiple init methods • Classes may define multiple init methods - (id)init; - (id)initWithName:(NSString *)name; - (id)initWithName:(NSString *)name age:(int)age; • Less specific ones typically call more specific with default values - (id)init { return [self initWithName:@“No Name”]; } - (id)initWithName:(NSString *)name { return [self initWithName:name age:0]; } Tuesday, January 12, 2010 21
  • 49. Finishing Up With an Object Person *person = nil; person = [[Person alloc] init]; Tuesday, January 12, 2010 22
  • 50. Finishing Up With an Object Person *person = nil; person = [[Person alloc] init]; [person setName:@“Jimmy Jones”]; [person setAge:32]; [person castBallot]; [person doSomethingElse]; Tuesday, January 12, 2010 22
  • 51. Finishing Up With an Object Person *person = nil; person = [[Person alloc] init]; [person setName:@“Jimmy Jones”]; [person setAge:32]; [person castBallot]; [person doSomethingElse]; // What do we do with person when we’re done? Tuesday, January 12, 2010 22
  • 52. Memory Management Allocation Destruction C malloc free Objective-C alloc dealloc Tuesday, January 12, 2010 23
  • 53. Memory Management Allocation Destruction C malloc free Objective-C alloc dealloc Tuesday, January 12, 2010 23
  • 54. Memory Management Allocation Destruction C malloc free Objective-C alloc dealloc Tuesday, January 12, 2010 23
  • 55. Memory Management Allocation Destruction C malloc free Objective-C alloc dealloc • Calls must be balanced ■ Otherwise your program may leak or crash Tuesday, January 12, 2010 23
  • 56. Memory Management Allocation Destruction C malloc free Objective-C alloc dealloc • Calls must be balanced ■ Otherwise your program may leak or crash • However, you’ll never call -dealloc directly ■ One exception, we’ll see in a bit... Tuesday, January 12, 2010 23
  • 58. Reference Counting • Every object has a retain count ■ Defined on NSObject ■ As long as retain count is > 0, object is alive and valid Tuesday, January 12, 2010 24
  • 59. Reference Counting • Every object has a retain count ■ Defined on NSObject ■ As long as retain count is > 0, object is alive and valid • +alloc and -copy create objects with retain count == 1 Tuesday, January 12, 2010 24
  • 60. Reference Counting • Every object has a retain count ■ Defined on NSObject ■ As long as retain count is > 0, object is alive and valid • +alloc and -copy create objects with retain count == 1 • -retain increments retain count Tuesday, January 12, 2010 24
  • 61. Reference Counting • Every object has a retain count ■ Defined on NSObject ■ As long as retain count is > 0, object is alive and valid • +alloc and -copy create objects with retain count == 1 • -retain increments retain count • -release decrements retain count Tuesday, January 12, 2010 24
  • 62. Reference Counting • Every object has a retain count ■ Defined on NSObject ■ As long as retain count is > 0, object is alive and valid • +alloc and -copy create objects with retain count == 1 • -retain increments retain count • -release decrements retain count • When retain count reaches 0, object is destroyed • -dealloc method invoked automatically ■ One-way street, once you’re in -dealloc there’s no turning back Tuesday, January 12, 2010 24
  • 63. Balanced Calls Person *person = nil; person = [[Person alloc] init]; Tuesday, January 12, 2010 25
  • 64. Balanced Calls Person *person = nil; person = [[Person alloc] init]; [person setName:@“Jimmy Jones”]; [person setAge:32]; [person castBallot]; [person doSomethingElse]; Tuesday, January 12, 2010 25
  • 65. Balanced Calls Person *person = nil; person = [[Person alloc] init]; [person setName:@“Jimmy Jones”]; [person setAge:32]; [person castBallot]; [person doSomethingElse]; // When we’re done with person, release it [person release]; // person will be destroyed here Tuesday, January 12, 2010 25
  • 66. Reference counting in action Tuesday, January 12, 2010 26
  • 67. Reference counting in action Person *person = [[Person alloc] init]; Tuesday, January 12, 2010 26
  • 68. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc Tuesday, January 12, 2010 26
  • 69. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Tuesday, January 12, 2010 26
  • 70. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Retain count increases to 2 with -retain Tuesday, January 12, 2010 26
  • 71. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Retain count increases to 2 with -retain [person release]; Tuesday, January 12, 2010 26
  • 72. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Retain count increases to 2 with -retain [person release]; Retain count decreases to 1 with -release Tuesday, January 12, 2010 26
  • 73. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Retain count increases to 2 with -retain [person release]; Retain count decreases to 1 with -release [person release]; Tuesday, January 12, 2010 26
  • 74. Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Retain count increases to 2 with -retain [person release]; Retain count decreases to 1 with -release [person release]; Retain count decreases to 0, -dealloc automatically called Tuesday, January 12, 2010 26
  • 76. Messaging deallocated objects Person *person = [[Person alloc] init]; // ... [person release]; // Object is deallocated Tuesday, January 12, 2010 27
  • 77. Messaging deallocated objects Person *person = [[Person alloc] init]; // ... [person release]; // Object is deallocated [person doSomething]; // Crash! Tuesday, January 12, 2010 27
  • 78. Messaging deallocated objects Person *person = [[Person alloc] init]; // ... [person release]; // Object is deallocated Tuesday, January 12, 2010 27
  • 79. Messaging deallocated objects Person *person = [[Person alloc] init]; // ... [person release]; // Object is deallocated person = nil; Tuesday, January 12, 2010 27
  • 80. Messaging deallocated objects Person *person = [[Person alloc] init]; // ... [person release]; // Object is deallocated person = nil; [person doSomething]; // No effect Tuesday, January 12, 2010 27
  • 81. Implementing a -dealloc method #import "Person.h" @implementation Person @end Tuesday, January 12, 2010 28
  • 82. Implementing a -dealloc method #import "Person.h" @implementation Person - (void)dealloc { } @end Tuesday, January 12, 2010 28
  • 83. Implementing a -dealloc method #import "Person.h" @implementation Person - (void)dealloc { // Do any cleanup that’s necessary // ... } @end Tuesday, January 12, 2010 28
  • 84. Implementing a -dealloc method #import "Person.h" @implementation Person - (void)dealloc { // Do any cleanup that’s necessary // ... // when we’re done, call super to clean us up [super dealloc]; } @end Tuesday, January 12, 2010 28
  • 85. Object Lifecycle Recap • Objects begin with a retain count of 1 • Increase and decrease with -retain and -release • When retain count reaches 0, object deallocated automatically • You never call dealloc explicitly in your code ■ Exception is calling -[super dealloc] ■ You only deal with alloc, copy, retain, release Tuesday, January 12, 2010 29
  • 86. Object Ownership #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; // Person class “owns” the name int age; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; - (void)castBallot; @end Tuesday, January 12, 2010 30
  • 87. Object Ownership #import "Person.h" @implementation Person @end Tuesday, January 12, 2010 31
  • 88. Object Ownership #import "Person.h" @implementation Person - (NSString *)name { return name; } - (void)setName:(NSString *)newName { } @end Tuesday, January 12, 2010 31
  • 89. Object Ownership #import "Person.h" @implementation Person - (NSString *)name { return name; } - (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName retain]; // name’s retain count has been bumped up by 1 } } @end Tuesday, January 12, 2010 31
  • 90. Object Ownership #import "Person.h" @implementation Person - (NSString *)name { return name; } - (void)setName:(NSString *)newName { } @end Tuesday, January 12, 2010 31
  • 91. Object Ownership #import "Person.h" @implementation Person - (NSString *)name { return name; } - (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName copy]; // name has retain count of 1, we own it } } @end Tuesday, January 12, 2010 31
  • 92. Releasing Instance Variables #import "Person.h" @implementation Person @end Tuesday, January 12, 2010 32
  • 93. Releasing Instance Variables #import "Person.h" @implementation Person - (void)dealloc { } @end Tuesday, January 12, 2010 32
  • 94. Releasing Instance Variables #import "Person.h" @implementation Person - (void)dealloc { // Do any cleanup that’s necessary [name release]; } @end Tuesday, January 12, 2010 32
  • 95. Releasing Instance Variables #import "Person.h" @implementation Person - (void)dealloc { // Do any cleanup that’s necessary [name release]; // when we’re done, call super to clean us up [super dealloc]; } @end Tuesday, January 12, 2010 32
  • 97. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; return result; } Tuesday, January 12, 2010 34
  • 98. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; return result; } Wrong: result is leaked! Tuesday, January 12, 2010 34
  • 99. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; [result release]; return result; } Tuesday, January 12, 2010 34
  • 100. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; [result release]; return result; } Wrong: result is released too early! Method returns bogus value Tuesday, January 12, 2010 34
  • 101. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; return result; } Tuesday, January 12, 2010 34
  • 102. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; [result autorelease]; return result; } Tuesday, January 12, 2010 34
  • 103. Returning a newly created object - (NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; [result autorelease]; return result; } Just right: result is released, but not right away Caller gets valid object and could retain if needed Tuesday, January 12, 2010 34
  • 104. Autoreleasing Objects • Calling -autorelease flags an object to be sent release at some point in the future • Let’s you fulfill your retain/release obligations while allowing an object some additional time to live • Makes it much more convenient to manage memory • Very useful in methods which return a newly created object Tuesday, January 12, 2010 35
  • 105. Method Names & Autorelease Tuesday, January 12, 2010 36
  • 106. Method Names & Autorelease • Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release Tuesday, January 12, 2010 36
  • 107. Method Names & Autorelease • Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release NSMutableString *string = [[NSMutableString alloc] init]; // We are responsible for calling -release or -autorelease [string autorelease]; Tuesday, January 12, 2010 36
  • 108. Method Names & Autorelease • Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release NSMutableString *string = [[NSMutableString alloc] init]; // We are responsible for calling -release or -autorelease [string autorelease]; • All other methods return autoreleased objects Tuesday, January 12, 2010 36
  • 109. Method Names & Autorelease • Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release NSMutableString *string = [[NSMutableString alloc] init]; // We are responsible for calling -release or -autorelease [string autorelease]; • All other methods return autoreleased objects NSMutableString *string = [NSMutableString string]; // The method name doesn’t indicate that we need to release it // So don’t- we’re cool! Tuesday, January 12, 2010 36
  • 110. Method Names & Autorelease • Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release NSMutableString *string = [[NSMutableString alloc] init]; // We are responsible for calling -release or -autorelease [string autorelease]; • All other methods return autoreleased objects NSMutableString *string = [NSMutableString string]; // The method name doesn’t indicate that we need to release it // So don’t- we’re cool! • This is a convention- follow it in methods you define! Tuesday, January 12, 2010 36
  • 111. How does -autorelease work? Tuesday, January 12, 2010 37
  • 112. How does -autorelease work? • Object is added to current autorelease pool Tuesday, January 12, 2010 37
  • 113. How does -autorelease work? • Object is added to current autorelease pool • Autorelease pools track objects scheduled to be released ■ When the pool itself is released, it sends -release to all its objects Tuesday, January 12, 2010 37
  • 114. How does -autorelease work? • Object is added to current autorelease pool • Autorelease pools track objects scheduled to be released ■ When the pool itself is released, it sends -release to all its objects • UIKit automatically wraps a pool around every event dispatch Tuesday, January 12, 2010 37
  • 115. Autorelease Pools (in pictures) pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 116. Autorelease Pools (in pictures) Pool Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 117. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 118. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 119. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool [object autorelease]; Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 120. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 121. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 122. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool Pool released Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 123. Autorelease Pools (in pictures) [object release]; Pool [object release]; Objects autoreleased here go into pool [object release]; Pool released Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 124. Autorelease Pools (in pictures) Pool Objects autoreleased here go into pool Pool released Pool created pp d ha lize nib nt en t pp nc ia i n ve v it a au nit a ore lee Ex L pi a dm it f nd Ap Lo Wa Ha Tuesday, January 12, 2010 38
  • 125. Hanging Onto an Autoreleased Object • Many methods return autoreleased objects ■ Remember the naming conventions... ■ They’re hanging out in the pool and will get released later • If you need to hold onto those objects you need to retain them ■ Bumps up the retain count before the release happens Tuesday, January 12, 2010 39
  • 126. Hanging Onto an Autoreleased Object • Many methods return autoreleased objects ■ Remember the naming conventions... ■ They’re hanging out in the pool and will get released later • If you need to hold onto those objects you need to retain them ■ Bumps up the retain count before the release happens name = [NSMutableString string]; // We want to name to remain valid! [name retain]; // ... // Eventually, we’ll release it (maybe in our -dealloc?) [name release]; Tuesday, January 12, 2010 39
  • 127. Side Note: Garbage Collection • Autorelease is not garbage collection • Objective-C on iPhone OS does not have garbage collection Tuesday, January 12, 2010 40
  • 129. Properties • Provide access to object attributes • Shortcut to implementing getter/setter methods • Also allow you to specify: ■ read-only versus read-write access ■ memory management policy Tuesday, January 12, 2010 42
  • 130. Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *) name; - (void)setName:(NSString *)value; - (int) age; - (void)setAge:(int)age; - (BOOL) canLegallyVote; - (void)castBallot; @end Tuesday, January 12, 2010 43
  • 131. Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *) name; - (void)setName:(NSString *)value; - (int) age; - (void)setAge:(int)age; - (BOOL) canLegallyVote; - (void)castBallot; @end Tuesday, January 12, 2010 43
  • 132. Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *) name; - (void)setName:(NSString *)value; - (int) age; - (void)setAge:(int)age; - (BOOL) canLegallyVote; - (void)castBallot; @end Tuesday, January 12, 2010 43
  • 133. Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // property declarations @property int age ; @property (copy) NSString * name; @property (readonly) BOOL canLegallyVote ; - (void)castBallot; @end Tuesday, January 12, 2010 43
  • 134. Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // property declarations @property int age; @property (copy) NSString *name; @property (readonly) BOOL canLegallyVote; - (void)castBallot; @end Tuesday, January 12, 2010 44
  • 135. Synthesizing Properties @implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { return name; } - (void)setName:(NSString *)value { if (value != name) { [name release]; name = [value copy]; } } - (void)canLegallyVote { ... Tuesday, January 12, 2010 45
  • 136. Synthesizing Properties @implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { return name; } - (void)setName:(NSString *)value { if (value != name) { [name release]; name = [value copy]; } } - (void)canLegallyVote { ... Tuesday, January 12, 2010 45
  • 137. Synthesizing Properties @implementation Person - (int)age { age return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { name return name; } - (void)setName:(NSString *)value { if (value != name) { [name release]; name = [value copy]; } } - (void)canLegallyVote { ... Tuesday, January 12, 2010 45
  • 138. Synthesizing Properties @implementation Person @synthesize age; @synthesize name; - (BOOL)canLegallyVote { return (age > 17); } @end Tuesday, January 12, 2010 46
  • 139. Property Attributes • Read-only versus read-write ! @property int age; // read-write by default @property (readonly) BOOL canLegallyVote; • Memory management policies (only for object properties) @property (assign) NSString *name; // pointer assignment @property (retain) NSString *name; // retain called @property (copy) NSString *name; // copy called Tuesday, January 12, 2010 47
  • 140. Property Names vs. Instance Variables • Property name can be different than instance variable @interface Person : NSObject { int numberOfYearsOld; } @property int age; @end Tuesday, January 12, 2010 48
  • 141. Property Names vs. Instance Variables • Property name can be different than instance variable @interface Person : NSObject { int numberOfYearsOld; } @property int age; @end @implementation Person @synthesize age = numberOfYearsOld; @end Tuesday, January 12, 2010 48
  • 142. Properties • Mix and match synthesized and implemented properties @implementation Person @synthesize age; @synthesize name; - (void)setAge:(int)value { age = value; // now do something with the new age value... } @end • Setter method explicitly implemented • Getter method still synthesized Tuesday, January 12, 2010 49
  • 143. Properties In Practice • Newer APIs use @property • Older APIs use getter/setter methods • Properties used heavily throughout UIKit APIs ■ Not so much with Foundation APIs • You can use either approach ■ Properties mean writing less code, but “magic” can sometimes be non-obvious Tuesday, January 12, 2010 50
  • 144. Dot Syntax and self • When used in custom methods, be careful with dot syntax for properties defined in your class • References to properties and ivars behave very differently @interface Person : NSObject { NSString *name; } @property (copy) NSString *name; @end Tuesday, January 12, 2010 51
  • 145. Dot Syntax and self • When used in custom methods, be careful with dot syntax for properties defined in your class • References to properties and ivars behave very differently @interface Person : NSObject { NSString *name; } @property (copy) NSString *name; @end @implementation Person - (void)doSomething { name = @“Fred”; // accesses ivar directly! self.name = @“Fred”; // calls accessor method } Tuesday, January 12, 2010 51
  • 146. Common Pitfall with Dot Syntax What will happen when this code executes? @implementation Person - (void)setAge:(int)newAge { self.age = newAge; } @end Tuesday, January 12, 2010 52
  • 147. Common Pitfall with Dot Syntax What will happen when this code executes? @implementation Person - (void)setAge:(int)newAge { self.age = newAge; } @end This is equivalent to: @implementation Person - (void)setAge:(int)newAge { [self setAge:newAge]; // Infinite loop! } @end Tuesday, January 12, 2010 52
  • 148. Further Reading • Objective-C 2.0 Programming Language ■ “Defining a Class” ■ “Declared Properties” • Memory Management Programming Guide for Cocoa Tuesday, January 12, 2010 53