1. Protocol proxy
A.h
#import
@class ViewControllerA;@protocol ViewControllerAdelegate-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;@end@interface ViewControllerA : UIViewController@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;-(IBAction)changeColorAction:(id)sender;@end
A.M
# Import "ViewControllerA. h "# import" ViewControllerB. h "@ interface ViewControllerA () @ end @ implementation ViewControllerA @ synthesize delegate; // use protocol proxy Delegate-(IBAction) changecolequaltion :( id) sender {[delegate identifier: self withColor: [UIColor grayColor]; [[self navigationController] popViewControllerAnimated: YES];} @ end
Finally, B
B. h
#import
#import "ViewControllerA.h"@interface ViewControllerB : UIViewController
@end
B. m
# Import "ViewControllerB. h "@ implementation ViewControllerB // response protocol proxy Delegate-(void) changeBgColorFromCtrlA :( ViewControllerA *) aView withColor :( UIColor *) color {[self. view setBackgroundColor: color] ;}@ end
2. notification center (Other examples above)
A.M
// Notification center nsicationicationcenter-(IBAction) changecolexception2 :( id) sender {UIColor * color = [UIColor greenColor]; [[nsicationicationcenter defacenter center] postNotificationName: @ "ChangeColorKey" object: color]; [[self navigationController] popViewControllerAnimated: YES];}
B. m
# Import "ViewControllerB. h "@ implementation ViewControllerB-(void) viewDidLoad {[super viewDidLoad]; // register [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (changeColor :) name: @ "ChangeColorKey" object: nil];} // response notification center nsicationicationcenter-(void) changeColor :( NSNotification *) notification {if ([notification object]! = Nil) {UIColor * color = [notification object]; [self. view setBackgroundColor: color] ;}}@ end
3. NSString
NSString * string = @ "abc ";
Comparison: [a isEqualToString: B]
Matching start and end: [a hasPrefix: @ "AB"]; [a hasSuffix: @ ". txt"];
Case-insensitive comparison: [a caseInsensitiveCompare: B] = NSOrderedSame;
Splicing: NSString * newS = [NSString stringWithFormat: @ "% @", a, B]; // if it already exists, use [a appendFormat: [NSString stringWithFormaat: @ "hhhhhhh"];
Segmentation: (string segmentation)
NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];NSArray *array = [string componentsSeparatedByString:@","];
(Arrays form strings)
// Merge elements from the array to the string-componentsJoinedByString: NSArray * array = [[NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three ", @ "Four", nil]; NSString * string = [array componentsJoinedByString: @ ","];
Uppercase: [a uppercaseString];
Lowercase: [a lowercaseString]
Interception:
Start from scratch: [a substringToIndex: 3];
Where to: [a substringFromIndex: 3];
Section: [a substringWithRange: NSMakeRange (0, 4)];
File Extension: [path pathExtension]
/* -------------- Read the string from the file: initWithContentsOfFile method -------------- */NSString * path = @ "astring. text "; NSString * astring = [[NSString alloc] initWithContentsOfFile: path]; NSLog (@" astring: % @ ", astring); [astring release]; /* ---------------- write a String to the file: writeToFile method ---------------- */NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "]; NSLog (@" astring: % @ ", astring); NSString * path = @" astring. text "; [astring writeToFile: path atomically: YES]; [astring release];
4. NSSArray
NSArray * array = [[NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three", @ "Four", nil]; // end with nil
Add element: addObject
Delete: removeObjectAtIndex: 1
Copy an array:
Direct Replication
NSMutableArray *MutableArray = [[NSMutableArray alloc] init];NSArray *array = [NSArray arrayWithObjects: @"a",@"b",@"c",nil];MutableArray = [NSMutableArray arrayWithArray:array];
Cycle:
NSMutableArray *newArray = [[NSMutableArray alloc] init];NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];for(int i = 0; i < [oldArray count]; i++){ obj = [[oldArray objectAtIndex:i] copy]; [newArray addObject: obj];}
Quick enumeration:
for(id obj in oldArray){ [newArray addObject: obj];}
Or:
NSEnumerator * enumerator; enumerator = [oldArray objectEnumerator]; // upload id obj from the back to the front; while (obj = [enumerator nextObject]) {[newArray addObject: obj];}
Sort:
[newArray sortUsingSelector:@selector(compare:)];
5. NSDictionary
NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @ "One", @ "1", @ "Two", @ "2", @ "Three ", @ "3", nil]; NSString * string = [dictionary objectForKey: @ "One"]; [dictionary setObject: @ "One" forKey: @ "1"]; // Delete the specified dictionary [dictionary removeObjectForKey: @ "3"];
6. Folder operations:
/*************************************** **************************************** ******************** **************************************** * **************************** // NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * home; home = @".. /Users/"; NSDirectoryEnumerator * direnum; direnum = [fileManager enumeratorAtPath: home]; NSMutableArray * files = [[NSMutableArray alloc] init]; // enumerate NSString * filename; while (filename = [direnum nextObject]) {if ([[filename pathExtension] hasSuffix: @ "jpg"]) {[files addObject: filename];} // quick enumeration // for (NSString * filename in direnum) // {// if ([[filename pathExtension] isw.tostring: @ "jpg"]) {// [files addObject: filename]; //}