1

I'm looking at Apple's MVC Networking sample project and I have found that in the class PhotoGallery the author has created instance and class versions of the method abandonGalleryCacheAtPath::

On line 139:

+ (void)abandonGalleryCacheAtPath:(NSString *)galleryCachePath

On line 457:

- (void)abandonGalleryCacheAtPath:(NSString *)galleryCachePath

All the instance version of the method seems to do is a bit of logging before calling the class method as follows:

[[QLog log] logWithFormat:@"gallery %zu abandon '%@'", (size_t) self.sequenceNumber, [galleryCachePath lastPathComponent]];
[[self class] abandonGalleryCacheAtPath:galleryCachePath];

The log message includes self.sequenceNumber, which is an instance variable which would not be available to the class method.

A couple of questions:

  1. Will the system automatically direct calls to the right method e.g. if another class method calls self abandonGalleryCacheAtPath:abc then the class version of the method will be executed, and if another instance method calls it then the instance version of the method will be executed?
  2. Do you think the author has implemented the instance method purely so that the value of sequenceNumber can be logged? Are there any other design/technical benefits of doing this kind of double implementation?
David Jones - iPushPull
  • 2,819
  • 1
  • 22
  • 24

1 Answers1

2

Since Objective C uses dynamic method binding, the system will automatically direct calls to the Class or Instance method depending on the context in which the call was made. Refer Objective-C uses dynamic binding, but how?

Community
  • 1
  • 1
Shanti K
  • 2,873
  • 1
  • 16
  • 31