Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between class methods and instance methods in Swift?

protocol NoteProtocol {
    var body: NSString? { get set }
    var createdAt: NSDate? { get set }
    var entityId: NSString? { get set }
    var modifiedAt: NSDate? { get set }
    var title: NSString? { get set }

    // class methods
    class func insertNewNoteInManagedObjectContext(managedObjectContext: NSManagedObjectContext!) -> NoteProtocol
    class func noteFromNoteEntity(noteEntity: NSManagedObject) -> NoteProtocol

    // instance methods
    func update(#title: String, body: String)
    func deleteInManagedObjectContext(managedObjectContext: NSManagedObjectContext!)
}

Hi This is a piece of code I found on GitHub. In this protocol, what is the main difference between class methods and instance methods? How they are defined? Can anyone help me?

like image 860
Dennis Avatar asked Feb 17 '15 19:02

Dennis


2 Answers

Some text from the documentation:

Instance Methods

Instance methods are functions that belong to instances of a particular class, structure, or enumeration. They support the functionality of those instances, either by providing ways to access and modify instance properties, or by providing functionality related to the instance’s purpose.

ie. An Instance of the class has to call this method. Example :

var a:classAdoptingNoteProtocol=classAdoptingNoteProtocol()
a.update()

Class Methods

Instance methods, as described above, are methods that are called on an instance of a particular type. You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods for classes by writing the keyword class before the method’s func keyword, and type methods for structures and enumerations by writing the keyword static before the method’s func keyword.

They are what are called as Static methods in other languages.To use them, this is what I would do:

var b=classAdoptingNoteProtocol.noteFromNoteEntity(...)

This will return a instance of a class which adopts NoteProtocol. ie. you don't have to create a instance of the class to use them.

like image 78
Dhruv Ramani Avatar answered Oct 07 '22 23:10

Dhruv Ramani


Below the definition of instance methods and class methods (called type methods in Swift).

For more details you can browse the method section of the Swift documentation

Instance methods:

Instance methods are functions that belong to instances of a particular class, structure, or enumeration. They support the functionality of those instances, either by providing ways to access and modify instance properties, or by providing functionality related to the instance’s purpose. Instance methods have exactly the same syntax as functions

Type methods:

Instance methods, as described above, are methods that are called on an instance of a particular type. You can also define methods that are called on the type itself. These kinds of methods are called type methods. You indicate type methods for classes by writing the keyword class before the method’s func keyword, and type methods for structures and enumerations by writing the keyword static before the method’s func keyword.

Basically you can call type method (class method) without instance:

var myNoteProtocol = NoteProtocolAdoptImplClass.noteFromNoteEntity(...);

While you need to instantiate for instance methods:

var myNoteProtocol  = NoteProtocolAdoptImplClass()
myNoteProtocol.update(...)
like image 28
Jérôme Avatar answered Oct 07 '22 23:10

Jérôme