When you develop iOS applications, you will find many occasions when you need to write your own custom classes. It is useful when you need to package custom classes with data. In a custom class, you can define your own behavior to store, manipulate, and display data.
For example, consider the global clock label of the iOS clock application. In this table view, these cells need to display more content than standard table View cells. This is to implement a subclass. It extends the UITableViewCell behavior so that you can display additional custom data for given table View cells. If you are designing this custom class, you can add a dot as a label to display future information and Image view time to display the right side of the cell on the custom clock.
This chapter will teach you what the Objective-C syntax and class structure you need to understand to implement the behavior of your todolist application. It discusses XYZToDoItem, which represents the design of custom classes for a single project in your to-do list. In the third tutorial, you will actually implement this class and add it to your application.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/examples/PBvbj2sr + 31qO6vdO/examples/a1ri2qL6/examples + examples/templates/e1xMO/templates/yda00NC0 + examples + stjG5MTasr/Ktc/samples + samples/examples/ release/2qGj0 + release/qsn5w/e008TjtcS0 + sLrtcTKtc/release + vt/release + us3Ktc/WzsS8/release ++ transfer + cq + o7o8YnI + release = "brush: java; "> @ interface XYZToDoItem: NSObject @ end
This example declares a class named XYZToDoItem, which inherits from NSObject.
It is defined in the @ interface declaration of Public attributes and behaviors. In this example, no superclass is exceeded, so it is expected that the unique behavior of XYZToDoItem instances is inherited from NSObject. All objects are expected to have a minimum behavior, so by default, they must inherit from NSObject (or its subclass ).
Implementation is used to declare a class to implement the Objective-C syntax as follows:
#import "XYZToDoItem.h" @implementation XYZToDoItem @end
If any method of the declared class interface, you need to implement them in this file.
Store the data of an object as a property
Consider the information to be held for the to-do list. You may need to know its name, creation time, and whether it has been completed. In your custom XYZToDoItem class, you will store this information in performance.
Declare the property in the interface file (XYZToDoItem. h. The following is what they look like:
@interface XYZToDoItem : NSObject @property NSString *itemName;@property BOOL completed;@property NSDate *creationDate; @end
In this example, the XYZToDoItem class declares three public attributes. These attributes can be used for comprehensive public access. With public access, other objects can read and change attribute values.
You may decide that an attribute should not be changed (that is, it should be read-only ). To indicate whether the attribute is intended to be read-only, including-Objective-C. The attribute Declaration includes the property attribute. For example, if you do not want to change the creation date of a XYZToDoItem, you may update the interface of the XYZToDoItem class to look like this:
@interface XYZToDoItem : NSObject @property NSString *itemName;@property BOOL completed;@property (readonly) NSDate *creationDate; @end
Attributes can be private or public. Sometimes it makes sense to make a private property so that other classes cannot view or access it. For example, if you want to keep a project marked as not giving access to this information to other categories, make private property by extending it in a class at the top of your implementation file (finish date from property track XYZToDoItem. m ).
#import "XYZToDoItem.h" @interface XYZToDoItem ()@property NSDate *completionDate;@end @implementation XYZToDoItem @end
You can access the getter and setter attributes. The suction agent returns an attribute value and changes it with setter. A common question mark ?? What are the methods for accessing getter and setter in shorthand syntax ?? It is a point symbol. For access to property reading and writing, you can use the dot symbol to obtain and set attribute values. If you have TodoItem of the XYZToDoItem object, you can perform the following operations:
toDoItem.itemName = @"Buy milk"; //Sets the value of itemNameNSString *selectedItemName = toDoItem.itemName; //Gets the value of itemName