Definition
Category (Category) YesObjective-CNew features of the language to add new methods for existing classes. Limitations: 1. You cannot add new instance variables. 2. Conflict with the method name of the class. When a name conflict occurs, the category has a higher priority. Purpose: 1. Implement it Using Scattered categories. 2. Use category to create a forward reference. 3. Informal agreements and delegation types.
Here we add a category substring. h file to nsstring:
@ Interface nsstring (substring)-(nsstring *) getnewmethod; // the newly added method @ end
. M file
@ Implementation nsstring (substring)-(nsstring *) getnewmethod {return @ "newmethed" ;}@ end
Import header files
# Import "nsstring + substring. H"
Call Method
Nsstring * teststr = @ "categorytest-lyhuzhu"; nslog (@ "getnewmethod: % @", [teststr getnewmethod]);
Returned results
Getnewmethod: newmethed
-