Saturday, September 21, 2013

Objective-C > NSArray

1. Declare a new NSMutableArray object named "username":
 NSMutableArray *username = [NSMutableArray arrayWithObjects: @"Peter",@"Mary", @"John", @"David" ,@"Susan", nil];


2. Common class method [ + ] and instance methods [ - ]:

  + array
  + arrayWithObjects
  - (NSUInteger) count
  - objectAtIndex: (NSSInteger) index
  - (void) addObject: (id) anObject
  - (void) insertObject: (id) anObject atIndex: (NSUInteger) index
  - (void) removeLastObject
  -  (void) removeObjectAtIndex: (NSUInteger) index
  - (void) removeAllObjects
  -
  -
+: Class Method: 無須實體化就可以使用的方法 (生仔用only)
-: Instance Method: 需先實體化才可以使用的方法,假設str1 已經是NSString 文字實體 (生完仔, 對仔落指令)


[+] arrayWithObjects: (NSString *) format

[-] (NSUInteger) count



[-] (NSUInteger) objectAtIndex: (NSSInteger) index



[-] (void) addObject: (id) anObject



[-] (void) insertObject: (id) anObject atIndex: (NSUInteger) index


[-] (void) removeLastObject




[-] (void) removeObjectAtIndex: (NSUInteger) index



[-] (void) removeAllObjects
<Exercises>
NSMutableArray
1a. Verify that the Foundation array classes are bounds checked:
  • Create an NSMutableArray and populate it with three NSString objects.
  • Using the method objectAtIndex:, try to retrieve an object from index -1.
1b. Try again with index 3. What happens?
  • Using the method insertObject:atIndex:, try to insert another NSString at index -1. What happens?
  • Now try to insert the new string at index 3. Explain your results.
-----------------------------------------------------------------------------------------------------------------------------
Sort an NSArray
1. Create an NSArray that holds the following strings: @"Raspberry", @"Peach", @"Banana", @"Blackberry", @"Blueberry", and @"Apple".
2. Obtain a sorted version of the array using the NSArray method sortedArrayUsingSelector:. Use the selector for the NSString method caseInsensitiveCompare: as the argument. You should read the documentation for sortedArrayUsingSelector: and caseInsensitiveCompare:
before proceeding.
3.Log the contents of the newly created sorted array.

No comments:

Post a Comment