1.All objects in an array are held onto strongly in the heap.So as long as that array itself is in the heap,as long as someone has a strong pointer to the array itself,all the objects the are in the array will stay in the heap as well.Because it has strong pointers to all of them.(32:00)
2.NSNumber is a class that is used to wrap primitive types,like integers,floats,doubles,Bools,things like that.And why do you want to wrap them?Usually because you want to put them in Array or a dictionary.You can create them with class methods like numberWithInt,or you can use @() or even just @number if you want to create a number.(35:00)
3.Really all object pointers(e.g.NSString *) are treated like id at runtime.But at compile time,if you type something NSString * instead of id,the compiler can help you.It can find bugs and suggest what methods would be appropriate to send to it,etc.If you type something using id,the compiler can’t help very much because it doesn’t know much.Figuring out the code to execute when a message is sent at runtime is called"dynamic binding”.
4.Treating all object pointers as "pointer to unknown type” at runtime seems dangerous,right?What stops you from sending a message to an object that it doesn’t understand?Nothing.And your program crashes if you do so.Oh my,Objective-C programs must crash a lot!Not really.Because we mostly use static typing(e.g.NSString *)and compiler is really smart.
5.So when would we ever intentionally use this dangerous thing?
When we want to mix objects of different class in a collection(e.g.in an NSArray).
When we want to support the "blind,structured” communication in MVC(i.e.delegation).
And there are other generic or blind communication needs
But to make these things safer,we’re going to use two things:Introspection and Protocols.
6.Introspection:Asking at runtime what class an object is or what message can be sent to it.
All objects that inherit from NSObject know these method...
isKindOfClass:returns whether an object is that kind of class(inheritance included)
isMemberOfClass:returns whether an object is that kind of class(no inheritance)
respondsToSelector:returns whether an object responds to an given method.
It calculates the answer to these questions at runtime(i.e. at the instant you send them)
Protocols:A syntax that is “in between” id and static typing.
Dose not specify the class of an object pointed to ,but does specify what methods it implements(e.g.id<UIScrollViewDelegate>scrollViewDelegate).
7.Special @selector() directive turns the name of a method into a selector...
if([obj respondsToSelector:@selector(shoot)])
{[obj shoot]}
else if ([obj respondsToSelector:@silector(shootAt:)])
{[obj shootAt:target]}
SEL is the Objective-C “type” for a selector
SEL shootSelector = @selector(shoot);
SEL shootAtSelector = @selector(shootAt:);
SEL moveToSelector = @selector(moveTo:withPenColor:);
If you have a SEL,you can also ask an object to perform it...
Using the performSelector: or t in NSObject:
[obj performSelector:shootSelector];
[obj performSelector:shootAtSelector withObject:coordinate];
Using makeObjectPerformSelector: methods in NSArray
[array makeObjectsPerformSelector:shootSelector];
[array makeObjectsPerformSelector:shootAtSelector withObject:target];//target is an id
In UIButton,-(void)addTarget:(id)anObject action:(SEL)action…;
[button addTarget:self action:@selector(digitPressed)…];
8.-(NSString *)description is a useful method to override(it’s %@ in NSLog).
e.g.NSLog(@“array contents are %@“,myArray);
The %@ is replaced with the results of invoking[myArray description].
9.NSObject:Base class for pretty much every object in the IOS SDK.
Implements introspection methods.
It’s not uncommon to have an array or dictionary and make a mutableCopy and modify that.Or to have a mutable array or dictionary and copy it to “freeze it” and make it immutable.Making copies of collection classes is very efficient,so don’t sweat doing so.
10.NSArray:Immutable.That’s right,once you create the array,you cannot add or remove objects.
All objects in the array are held onto strongly.
Usually created by manipulating other arrays or with @[];
You already know these key methods...
-(NSUInteger)count;
-(id) objectAtIndex:(NSUInteger)index;//crashes if index is out of bounds;returns id!
-(id)lastObjects;//returns nil(doesn’t crash)if there are no objects in the array
-(id)firstObjects;//returns nil(doesn’t crash)if there are no objects in the array
But there are a lot of very interesting methods in this class.Examples..
-(NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
-(void)makeObjectsPerfornSelector:(SEL)aSelector withObject:(id)selectorArgument;
-(NSString *)componentsJoinedByString:(NSString *)separator;
11.NSMutableArray:Mutable Version of NSArray.
Create with alloc/init or...
+(id)arrayWithCapacity:(NSUInteger)numItems;//numItems is a performance hint only
+(id)array;[NSMutableArray array]is just like [[NSMutableArray alloc ]init]
And you know that it implements these key methods as well...
-(void)addObject:(id)object;//to the end of the array
-(void)insertObject:(id)object atIndex(NSUInteger)index;
-(void)removeObjectAtIndex:(NSUInteger)index;
12.Looping through members of an array in an efficient manner.
Example:NSArray of NSString objects
NSArray *myArray = …;
for(NSString in myArray){
//no way for compiler to know what myArray contains
double value = [string doubleValue];//crash here if string is not an NSString
}
Example:NSArray of id
NSArray *myArray = …;
for(id obj in myArray){
//do something with obj,but make sure you don’t send it a message it does not respond to
if([obj isKindOfClass:[NSString class]]){
//send NSString messages to obj with no worries
}
}
13.NSNumber:Object wrapper around primitive type like int,float,double,BOOL,enums,etc.It;s useful when you want to put these primitive types in a collection(e.g.NSArray or NSDictionary)
New syntax for creating an NSNumber in IOS 6:@()
NSNumber *three = @3;
NSNumber *underline = @(NSUnderlineStyleSingle;)//enum
NSNumber *match = @([card match:@[otherCard]]);//expression that returns a primitive type.
14.NSValue:Generic object wrapper for some non-object,non-primitive data types(i.e.C structs).
e.g. NSVaule *edgeInsetsObject = [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(1,1,1,1)]
15.NSData:”Bag of bits.”Used to save/restore/transmit raw data throughout the IOS SDK.
16.NSDate:Used to find out the time right now or to store past or future times.dates.
17.NSSet/NSMutableSet:Like an array,but no ordering(no objectAtIndex:method).
member:is an important method(returns an object if there is one in the set isEqual:to it)
18.NSOrderedSet/NSMutableOrderedSet:Sort of a cross between NSArray and NSSet.
Objects in an ordered set are distinct.You can’t out the same object in multiple times like array.
19.NSDictionary:Immutable collection of objects looked up by a key(simple hash table).
All keys and values are held onto strongly by an NSDictionary.
Can create with this syntax@{key1:value1,key2:value2,key3:value3}
NSDictionary *colors = @[
@“green”:[UIColor greenColor];
@“blue”:[UIColor blueColor];
@“red”:[UIColor redColor];
]
Lookup using “array like”notation
NSString *colorString = …;
UIColor *colorObject = colors[colorString];//works the same as objectForKey: below
-(NSUInteger)count;
-(id)objectForKey:(id)key;//key must be copyable and implement isEqual:properly NSStrings make good keys because of this.
20.NSMutableDictionary:Create using alloc/init or one of the +(id)dictionary… class methods.
In addition to all the methods inherited from NSDictionary,here are some important methods...
-(void)setObject:(id)anObject forKey:(id)key;
-(void)removeObjectForKey:(id)key;
-(void)removeAllObjects;
-(void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
Looping through the keys or values of a dictionary
Example:NSDictionary *myDictionary = …;
for(id key in myDictionary){
//do something with key here
id value = [myDictionary objectForKey:key];
}
21.The term “Property List”just means a collection of collections.It’s just a phrase(not a language thing).It means any graph of objects containing only:NSArray,NSDictionary,NSNumber,NSString,NSDate,NSData(or mutable subclasses thereof).
22.NSUserDefaults:Lightweight storage of Property Lists.It’s basically an NSDictionary that persists between launched of your application.Not a full-on database,so only store small things like user preferences.
23.NSRange:C struct(not a class).Used to specify subranges inside strings and arrays.
typedef struct{
NSUInteger location
NSUInteger length;
}NSRange