- In Objective C, setter and getter methods are automatically generated by the compiler if @property (in the interface file, .h) and @synthesize (in the implementation file, .m) keywords are used.
- Every instance variable is protected by default.
- Objective C has the garbage collection mechanism built into it, but not all platforms support it, e.g.,
iPhone . - A variable defined outside a method is not only a global variable but it is an external variable also. To make a variable global within the file, but not externally accessible, the keyword, "static" should be used.
- #ifdef DEBUG along with #endif is a neat way to contextually switch on or off all the debugging statements embedded inside the source.
- The default return type of a function is "int" whereas that for a method is "id". Functions are external by default, in order to restrict access to it, the keyword "static" should be used.
- Foundation framework has NSString class to work with strings. There are immutable and mutable strings. NSNumber is the class for working with numbers. Similarly NSArray is the class to be used when arrays are to be used, and there are mutable and immutable arrays. NSMutableArray is a subclass of NSArray (immutable).
- Dictionary is a collection of key/value pairs. It can be mutable or immutable. For a mutable dictionary, entries can be dynamically added and removed. Dictionaries can be searched on a particular key, and their contents may be enumerated. The keys must be unique and they can be of any object type, typically strings though. The value associated with a key can be of any object type except for nil.
- A Set is a collection of unique objects, it can be mutable or immutable. Operations on a Set includes: searching, adding, and removing members (valid for mutable sets); comparing two sets; and finding union and intersection of two sets.
- NSFileManager lets one to work with files. The available operations are: create a new file, read from an existing file, write data to a file, rename a file, remove (delete) a file, test for the existence of a file, determine the size of the file along with other attributes, make a copy of a file, and test two files for the equality of their contents. Operations such as create, read, or delete can be performed on directories. It is possible to link files, i.e., the same files can exist under different names or even in different directories. To open a file and perform multiple reads and writes on it, NSFileHandle's methods are needed. NSFileHandle enables one to open a file for reading, writing, or updating; seek a specified position within a file; and read or write a specified number of bytes from or to a file. The methods from NSFileHandle can also be applied to devices or sockets.
- About program 16.1 (Programming in Objective C 3rd Edition): The current working directory while running the program in XCode is build/debug, so the testfile must be there. Methods such as: copyPath: toPath: handler:, movePath: toPath: handler:, fileAttributesAtPath: traverseLink:, moveFileAtPath: handler:, removeFileAtPath: handler:, and stringWithContentsOfFile: have been deprecated; these methods must be replaced with: copyItemAtPath: toPath: error:, moveItemAtPath: toPath: error:, attributesOfItemAtPath: error:, removeItemAtPath: error:, and stringWithContentsOfFile: ecoding: (e.g., NSUTF8StringEncoding) error: methods.
- For program 16.3: Use createDirectoryAtPath: withIntermediateDirectories: attributes: error: method to create the new directory.
- Autorelease pools are distinct from built-in garbage collection, therefore,
iPhone still has access to autorelease pools even though it doesn't support garbage collection. An object can be attached to the NSAutoReleasePool by sending "autoRelease" message to it, but doing that does not change its reference count. Reference count of an object is automatically handled while adding and removing to or from a collection (e.g., an array); in other cases the programmer has to keep track of the count explicitly in order to ensure that the system will actually free up the memory. Every "retain" message to an object increases the reference count by 1 and every "release" decrements it by 1. When an object is created its reference count is set to 1. If the reference count of an object comes down to 0, the system frees up (calls "deallocate") the memory. All in all, creation of an object must be paired up with a "release", and every intermediate "retain" must be paired up with an intermediate "release". - Constant strings have no mechanism for counting references, they can never be released. The value of the "retainCount" of a constant string is the largest possible unsigned integer value, or UINT_MAX (for some systems the largest possible signed int value, INT_MAX), in the standard header file limits.h
. - Assigning a copy of an object using copy or mutableCopy method to another object reference is called "shallow" copying; it is equivalent to assigning a reference of the first object to the reference of the second one. If the object being copied is a collection of objects, then subsequent changes to the original object or its copy affect both of them, because they both point to the same memory locations. To break the tie between them, "deep" copying is required.
- In order to have a copy method for your class, copyWithZone: (NSZone*) and/or mutableCopyWithZone: (NSZone*) must be implemented. The "Zone" argument enables you to work with different zones of the total memory for your program which might be useful for programs that allocate a lot of memory. The idea is to optimize allocation by grouping them into zones.
- Saving an object to a file and reading it back can be performed in two ways: property lists and key-valued coding. NSString, NSDictionary, NSArray, NSDate, NSData, or NSNumber objects can use writeToFile:atomically: method to write contents to a file. In case of writing out a dictionary or an array, this method writes the data in the format of an XML property list (plist). When you create a plist from a dictionary, the keys in the dictionary must all be NSString type objects. The elements of an array or the values in a dictionary can be NSString, NSArray, NSDictionary, NSData, NSDate, and NSNumber type objects. For reading back data from a file, you can use one of these methods as it fits: dictionaryWithContentsOfFile:, arrayWithContentsOfFile:, dataWithContentsOfFile:, and stringWithContentsOfFile: .
- NSKeyedArchiver, on the other hand, provides more flexibility: one can save and read any type of object with it. Here an archived object acquires a key, and for retrieving the object from the archive this key is used.
iPhone does allow it. - In order to save and read objects of one's own classes, one has to provide encoder and decoder in those classes. "Deep" copying involves archiving. The idea is to archive from the source object to a NSData or file and then unarchive to the new object.
Tuesday, September 14, 2010
Programming in Objective C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment