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?
Some text from the documentation:
Instance Methods
ie. An Instance of the class has to call this method. Example :
Class Methods
They are what are called as Static methods in other languages.To use them, this is what I would do:
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.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:
Type methods:
Basically you can call type method (class method) without instance:
While you need to instantiate for instance methods: