the messy lecture
TRANSCRIPT
РАЗХВЪРЛЯНАТА ПРЕЗЕНТАЦИЯ
Около първи етаж след основите
Thursday, December 22, 11
DOT SYNTAX(.) alternative to ([]) to invoke accessor methods
setget
// Each member of the path is an object.
x = person.address.street.name;x = [[[person address] street] name];
self.age = 10;
[self setAge:10];
// The path contains a C struct.// This will crash if window is nil or -contentView returns nil.
y = window.contentView.bounds.origin.y;y = [[window contentView] bounds].origin.y;
// An example of using a setter.
person.address.street.name = @"Oxford Road";[[[person address] street] setName: @"Oxford Road"];
Thursday, December 22, 11
INSTANCE VARIABLES SCOPE
• default : @protected
Figure 2-‐1 illustrates the levels of scoping.
Figure 2-1 The scope of instance variables (@package scope not shown)
Unrelated code
The class that declares the
instance variable
A class thatinherits the
instance variable
@private
@protected
@public
A scoping directive applies to all the instance variables listed after it, up to the next directive or the end ofthe list. In the following example, the age and evaluation instance variables are private; name, job, andwage are protected; and boss is public.
@interface Worker : NSObject{ char *name;@private int age; char *evaluation;@protected id job; float wage;@public id boss;}
By default, all unmarked instance variables (like name above) are @protected.
All instance variables that a class declares, no matter how they’re marked, are within the scope of the classdefinition. For example, a class that declares a job instance variable, such as the Worker class shown above,can refer to it in a method definition:
- promoteTo:newPosition{ id old = job; job = newPosition; return old;}
Class Implementation 372011-10-12 | © 2011 Apple Inc. All Rights Reserved.
CHAPTER 2
Defining a Class
Thursday, December 22, 11
INSTANCE VARIABLES SCOPE
@interface Worker : NSObject{ NSString *name;@private NSInteger age; NSString *evaluation;@protected id job; CGFloat wage;@public id boss; }
Thursday, December 22, 11
@property
@property float value;@property(copy, readwrite) NSString *value;
readwrite - defaultreadonly - only the getter method
@synthesize = напищи ми getter & setter
(ARC)strong (owning)weak (non-owning)
(MRC)
copyThe previous value is sent a release message. NSCopying protocol.
assigndefault. NSInteger, CGRect....
retainupon assignment. The previous value is release-d
Multithreaded environmentAccess Memory
nonatomic returns the value directly
By default, accessors are atomic.
Thursday, December 22, 11
MEMORY MANAGEMENT
Защо да създаваме обект:
• запазване на instance variable
• използване в метод
Thursday, December 22, 11
MEMORY MANAGEMENT
- (void)setLastUpdatedDate:(NSDate *)newDate {! if (newDate) {! ! [lastUpdatedDate release];! ! lastUpdatedDate = [newDate retain]; }}
Setter method
//Recommended code:-(void) setName: (NSString *) theName{ [name release] name = [[NSString alloc] initWthString: theName];}
//Incorrect code according to Kochan:-(void) setName: (NSString *) theName{ [name release] name = [NSString stringWthString: theName];}
Thursday, December 22, 11
MEMORY MANAGEMENT
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75];[self setTotal:value1];
NSNumber* value2 = [NSNumber numberWithFloat:14.78];[self setTotal:value2]; // only release value1[value1 release];
Thursday, December 22, 11
MESSAGES TO NIL
[myArray release];myArray = nil;id anObjectMaybeNil = nil;
// this is valid
if ([anObjectMaybeNil methodThatReturnsADouble] == 0.0)
{
// implementation continues...
}
Thursday, December 22, 11
TTIMAGEVIEW
UIImageView
Name Link Title Note
TTImageView
Thursday, December 22, 11
XCODE
•NSString
• .plist
• [NSUserDefaults standardUserDefaults]
• Custom UIImageView class
Thursday, December 22, 11