# import
@ Interface wenViewController: UIViewController {
NSString * key;
}
I am. M files can be accessed and modified pieces key
The second method is. H file write
# import
@ Interface wenViewController: UIViewController {
NSString * key;
}
@ Property (nonatomic, retain) NSString * key;
Such two kinds of writing can be read and distinguish it
Reply:
proprety belongs to abstraction, physical variables are stored in the structure of the tank, under normal circumstances, other objects can not access them directly. The property is the object's attributes, can be accessed. In addition, property can be returned or send an instance variable, see the following example
@ interface Person: NSObject {
NSString * name;
}
@ Property (copy) NSString * name;
@ Property (copy) NSString * firstName;
@ Property (copy) NSString * lastName;
@ End
@ Implementation Person
@ Synthesize name;
- (NSString *) firstName {
[[Name componentsSeparatedByString: @ ""] objectAtIndex: 0];
}
- (NSString *) lastName {
[[Name componentsSeparatedByString: @ ""] lastObject];
}
- (NSString *) setFirstName: (NSString *) newName {
NSArray * nameArray = [name componentsSeparatedByString: @ ""];
NSArray * newNameArray [[NSArray arrayWithObjects: newName, nil] arrayByAddingObjectsFromArray: [nameArray subarrayWithRange: NSMakeRange (1, [nameArray size] -1)]];
self.name = [newNameArray componentsJoinedByString: @ ""];
}
- (NSString *) setLastName: (NSString *) newName {
NSArray * nameArray = [name componentsSeparatedByString: @ ""];
NSArray * newNameArray [[nameArray subarrayWithRange: NSMakeRange (0, [nameArray size] -2)] arrayByAddingObjectsFromArray: [NSArray arrayWithObjects: newName, nil]];
self.name = [newNameArray componentsJoinedByString: @ ""];
}
@ End
Assuming the code name already exists and has two parts (eg such as Bill Gates instead Gates this)
Reply:
JAVA in member variables and attributes.
Reply:
In Object-c add attributes, programmers typically define a number of methods used to set and retrieve the class instance variables have the value of these methods is called access methods and modify the method can also be called getter and setter methods (java in called the set and get methods). But the @ property declaration lets you say goodbye to boring access methods and modification method. We typed in the interface file @ property declaration and implementation file (@ synthesize) the combination of another statement, you can tell the compiler at compile time to create getter and setter methods.
No comments:
Post a Comment