[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf

上传人:飞****9 文档编号:134189479 上传时间:2020-06-03 格式:PDF 页数:46 大小:9.41MB
返回 下载 相关 举报
[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf_第1页
第1页 / 共46页
[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf_第2页
第2页 / 共46页
[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf_第3页
第3页 / 共46页
[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf_第4页
第4页 / 共46页
[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf》由会员分享,可在线阅读,更多相关《[斯坦福大学iOS开发教程2010年秋].Lecture.4.pdf(46页珍藏版)》请在金锄头文库上搜索。

1、Stanford CS193p Developing Applications for iPhone 4 iPod Touch id objectAtIndex int index void makeObjectsPerformSelector SEL aSelector NSArray sortedArrayUsingSelector SEL aSelector id lastObject returns nil if there are no objects in the array convenient void addObject id anObject void insertObje

2、ct id anObject atIndex int index void removeObjectAtIndex int index Foundation Framework NSDictionary NSMutableDictionary Mutable version of NSDictionary Stanford CS193p Fall 2010 Hash table Look up objects using a key to get a value Immutable That s right you cannot add or remove objects to it once

3、 it s created Keys are objects which must implement NSUInteger hash id objectForKey id key NSArray allKeys NSArray allValues void setObject id anObject forKey id key void removeObjectForKey id key void addEntriesFromDictionary NSDictionary otherDictionary Foundation Framework NSSet NSMutableSet Muta

4、ble version of NSSet Stanford CS193p Fall 2010 Unordered collection of objects Immutable That s right you cannot add or remove objects to it once it s created Important methods int count BOOL containsObject id anObject id anyObject void makeObjectsPerformSelector SEL aSelector id member id anObject

5、uses isEqual and returns a matching object if any void addObject id anObject void removeObject id anObject void unionSet NSSet otherSet void minusSet NSSet otherSet void intersectSet NSSet otherSet Enumeration Looping through members of a collection in an effi cient manner Language support using for

6、 in similar to Java Stanford CS193p Fall 2010 Example NSArray of NSString objects NSArray myArray for NSString string in myArray double value string doubleValue crash here if string is not an NSString Example NSSet of id could just as easily be an NSArray of id NSSet mySet for id obj in mySet do som

7、ething 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 impunity Enumeration Looping through the keys or values of a dictionary Stanford CS193p Fall 2010 Example NSDictionary myDictionary for id key in my

8、Dictionary do something with key here id value myDictionary objectForKey key do something with value here The term Property List just means a collection of collections Specifi cally it is any graph of objects containing only the following classes NSArray NSDictionary NSNumber NSString NSDate NSData

9、An NSArray is a Property List if all its members are too So an NSArray of NSString is a Property List So is an NSArray of NSArray as long as those NSArray s members are Property Lists An NSDictionary is one only if all keys and values are too An NSArray of NSDictionarys whose keys are NSStrings and

10、values are NSNumbers is one Why defi ne this term Because the SDK has a number of methods which operate on Property Lists Usually to read them from somewhere or write them out to somewhere plist writeToFile NSString path atomically BOOL plist is NSArray or NSDictionary Stanford CS193p Fall 2010 Prop

11、erty List Other Foundation NSUserDefaults Stanford CS193p Fall 2010 Lightweight storage of Property Lists It s basically an NSDictionary that persists between launches of your application Not a full on database so only store small things like user preferences Read and write via a shared instance obt

12、ained via class method standardUserDefaults NSUserDefaults standardUserDefaults setArray rvArray forKey RecentlyViewed Sample methods void setDouble double aDouble forKey NSString key NSInteger integerForKey NSString key NSInteger is a typedef to 32 or 64 bit int void setObject id obj forKey NSStrin

13、g key obj must be a Property List NSArray arrayForKey NSString key will return nil if value for key is not NSArray Always remember to write the defaults out after each batch of changes NSUserDefaults standardUserDefaults synchronize It is a two step operation to create an object in Objective C Alloc

14、ating almost always done with the NSObject class method alloc Initializing done with a method that starts with the four letters init alloc makes space in heap for the class s instance variables Also sets them all to zero so instance variables which are object pointers start out nil Each class has a

15、designated initializer method NSObject s is init by convention all init methods start with the four letters init When you subclass an object you must call your superclass s designated initializer from your designated initializer and make sure that it did not fail and return nil Your designated initi

16、alizer should only take arguments that are required for the proper initialization of your class i e if you can have sensible defaults for things do it You can have other initializers convenience initializers which take other arguments but those should always call your designated initializer so that subclasses of yours will work properly All init methods should be typed in their declaration to return id not statically typed Callers should statically type though e g MyObject obj MyObject alloc ini

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号