1

Am reading the book "Objective-C Programming" by Big Nerd Ranch, and am not sure about the differentiation.

Context: NSLog() is an Objective-C function (not a method!) that works a lot like printf(). In NSLog(), however, the format string is actually an instance of NSString.

pdenlinger
  • 3,897
  • 10
  • 60
  • 92

4 Answers4

7

A method is just a function that is defined as part of a class. A plain function, such as NSLog or printf, does not belong to a class.

Paul R
  • 208,748
  • 37
  • 389
  • 560
2

There is no such thing as an "Objective-C function." NSLog is a function, same as any other standard function in the library, it just happens to take an Objective-C object, but those are just pointers so they can interact with C code just fine. There is nothing 'special' about NSLog.

A method on the other hand is part of an Objective-C class.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
1

Methods belong to classes & objects, and are invoked via message passing. Functions are not attached to a specific class or object, and work exactly as they do in C.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

A function in Obj-C is not scoped to a specific class, whereas a method is scoped to a class.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65