简体   繁体   中英

Where to Use Class Method ,Instance Method in Objective-C

"Instance" mean in Objective-C? Kindly tell me where to use Class Method And where to use Instance Method,also tell me where we use (Instacetype) method? why/where we use multi Parameters?

A class method is a method whose self parameter is a reference to the class's class object.

An instance method is a method whose self parameter is a reference to a specific instance of the class.

Those are the technical differences.

A more practical answer is that an instance method operates on a single instance of the class, while a class method operates at a more global, non-specific level. A class method can act as a factory method, such as NSString 's stringWithFormat: method. It can also be used to configure behavior that will affect all instances of the class. It can also be used to operate on a collection of instances of the class, such as sorting or filtering.

instancetype is a keyword that can be used as a placeholder for the current class's type. It says to the compiler: pretend that I wrote <my class name> here, so if you see the result of this method assigned somewhere, you know what type it's supposed to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM