[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture4

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

时间: 2024-12-15 01:54:57

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture4的相关文章

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture7

1.One is CGFloat.It’s a floating point number.All floating point numbers that have to do with drawing on the screen or getting touch events or whatever are CGFloats.This might be a double.It might be just a regular floating point number.Not only usin

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6

1.Abstract means that this class cannot be instantiated and used,it’s only useful as a superclass for other classes that are concrete. (04:00) 2.And I also like to document,even on my implementation any methods that basically are abstract.Any method

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture5

1.There is a class called NSNotificationCenter .It has a class method called defaultCenter.That returns a shared instance kind of like NSUserDefault,standard UserDefault did — a shared instance.That’s the object you use to tune into radio stations. A

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture1

1.The difference is card.h is the public API.That’s what your dot h is.It’s your public API.It’s what methods in your class you want to make public so that other people can call them.Card.m is your private API and all your implementation. 2.It’s impo

机器人学-笔记-斯坦福大学公开课-class 2

kinematics 1. manipulator is defined by a set of links connected trough joints. 2. joint type: 移动,转动 3. configuration parameters 4. constraints, freedom. 5. planning motions in configuraiton coordinates 6. degress of redundancy. 7. rotation matrix.

机器人学-笔记-斯坦福大学公开课-class 1

This course is going to really cover the foundations of robotics(mathematical models). 模型的运动学特性, 驱动系统actuate the system, motors, 正确的转矩让robort move, 1. if you want to control the robot, you need to know its position -- sensor(GPS(x,y), encoders(one fr

斯坦福大学公开课:iOS 7应用开发 笔记

2015-07-06 第一讲   课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留下笔记

《斯坦福大学公开课:编程方法学》随笔

这是观看网易公开课 Mehran Sahami教授的<斯坦福大学公开课:编程方法学>后的随笔. 目前只看到第三次课,<Karel与Java>,我的收获有以下要点: 1.软件工程跟普通的写代码是不同的.软件应该考虑很多东西,比如:可移植性.便于升级维护等等,而不仅仅是写出代码实现功能那么简单. 2.代码是写给机器执行的,但更重要的是人要让人能看懂.(代码后期维护等等的工作量或者时间是写代码的10倍及更多,所以让代码更规范更易被人读懂很重要) 3.准确定义一个函数.一个类.一个包的功能

斯坦福大学公开课:iPad和iPhone应用开发(iOS5) 学习笔记 2

继续学习公开课 第二节课做了一个简单的计算器作为例子.大概Touch了如下知识点: 讲解了XCode4,我看了一下最新下载的是XCode8了. XCode创建工程, singleViewApplication还是有的,界面对比起XCode4来,更简洁些了,操作跟视频讲解里的差不多. 体会了下第一节课讲的MVC View的代码看不到这个感觉不太爽,特别是前面操作是将number的button拷贝到了 operation的 button,结构导致operation button也都连接到了digit