text
stringlengths 0
13.4k
|
---|
@interface XYZToDoItem : NSObject |
@end |
This example declares a class named XYZToDoItem, which inherits from NSObject. |
The public properties and behavior are defined inside the @interface declaration. In this example, nothing |
is specified beyond the superclass, so the only behavior expected to be available on instances of XYZToDoItem |
is the behavior inherited from NSObject. All objects are expected to have a minimum behavior, so by default, |
they must inherit from NSObject (or one of its subclasses). |
Implementation |
The Objective-C syntax used to declare a class implementation looks like this: |
#import "XYZToDoItem.h" |
@implementation XYZToDoItem |
@end |
If you declare any methods in the class interface, you鈥檒l need to implement them inside this file. |
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved. |
87 |
Writing a Custom Class |
Properties Store an Object鈥檚 Data |
Properties Store an Object鈥檚 Data |
Consider what information the to-do item needs to hold. You probably need to know its name, when it was |
created, and whether it鈥檚 been completed. In your custom XYZToDoItem class, you鈥檒l store this information in |
properties. |
Declarations for these properties reside inside the interface file (XYZToDoItem.h). Here鈥檚 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 properties. These properties are available for |
full public access. With public access, other objects can both read and change the values of the properties. |
You may decide to declare that a property shouldn鈥檛 be changed (that is, that it should be read-only). To indicate |
whether a property is intended to be read-only鈥攁mong other things鈥擮bjective-C property declarations |
include property attributes. For example, if you don鈥檛 want the creation date of an XYZToDoItem to be |
changeable, you might update the XYZToDoItem class interface to look like this: |
@interface XYZToDoItem : NSObject |
@property NSString *itemName; |
@property BOOL completed; |
@property (readonly) NSDate *creationDate; |
@end |
Properties can be private or public. Sometimes it makes sense to make a property private so that other classes |
can鈥檛 see or access it. For example, if you want to keep track of a property that represents the date an item was |
marked as completed without giving other classes access to this information, make the property private by |
putting it in a class extension at the top of your implementation file (XYZToDoItem.m). |
#import "XYZToDoItem.h" |
2013-10-22 | Copyright 漏 2013 Apple Inc. All Rights Reserved. |
88 |
Writing a Custom Class |
Methods Define an Object鈥檚 Behavior |
@interface XYZToDoItem () |
@property NSDate *completionDate; |
@end |
@implementation XYZToDoItem |
@end |
You access properties using getters and setters. A getter returns the value of a property, and a setter changes |
it. A common syntactical shorthand for accessing getters and setters is dot notation. For properties with read |
and write access, you can use dot notation for both getting and setting a property鈥檚 value. If you have an object |
toDoItem of class XYZToDoItem, you can do the following: |
toDoItem.itemName = @"Buy milk"; |
//Sets the value of itemName |
NSString *selectedItemName = toDoItem.itemName; //Gets the value of itemName |