object-c常用类
1.协议代理
A.h
[html] view plaincopy

- #import <UIKit/UIKit.h>
- @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
[html] view plaincopy

- #import “ViewControllerA.h”
- #import “ViewControllerB.h”
- @interface ViewControllerA ()
- @end
- @implementation ViewControllerA
- @synthesize delegate;
- //使用协议代理Delegate
- -(IBAction)changeColorAction:(id)sender{
- [delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];
- [[self navigationController] popViewControllerAnimated:YES];
- }
- @end
最终实现在B
B.h
[html] view plaincopy

- #import <UIKit/UIKit.h>
- #import “ViewControllerA.h”
- @interface ViewControllerB : UIViewController<ViewControllerAdelegate>
- @end
B.m
[html] view plaincopy

- #import “ViewControllerB.h”
- @implementation ViewControllerB
- //响应协议代理Delegate
- -(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{
- [self.view setBackgroundColor:color];
- }
- @end
2.通知中心(其他同上面的例子)
a.m
[html] view plaincopy

- //通知中心NSNotificationCenter
- -(IBAction)changeColorAction2:(id)sender{
- UIColor *color = [UIColor greenColor];
- [[NSNotificationCenter defaultCenter] postNotificationName:@”ChangeColorKey” object:color];
- [[self navigationController] popViewControllerAnimated:YES];
- }
b.m
[html] view plaincopy

- #import “ViewControllerB.h”
- @implementation ViewControllerB
- – (void)viewDidLoad
- {
- [super viewDidLoad];
- //注册
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(changeColor:)
- name:@”ChangeColorKey”
- object:nil];
- }
- //响应通知中心NSNotificationCenter
- – (void)changeColor:(NSNotification *)notification{
- if([notification object] != nil)
- {
- UIColor *color = [notification object];
- [self.view setBackgroundColor:color];
- }
- }
- @end
3.NSString
NSString *string = @”abc”;
比较:[a isEqualToString:b]
匹配开头结尾: [a hasPrefix:@”ab”] ; [a hasSuffix:@”.txt”];
忽略大小写比较: [a caseInsensitiveCompare:b] == NSOrderedSame;
拼接: NSString *newS = [NSString stringWithFormat:@”%@%@”,a,b ]; //已有的话用[a appendFormat :[NSString stringWithFormaat:@”hhhhhhh”] ];
分割:(字符串分割)
[html] view plaincopy

- NSString *string = [[NSString alloc] initWithString:@”One,Two,Three,Four”];
- NSArray *array = [string componentsSeparatedByString:@”,”];
(数组组成字符串)
[html] view plaincopy

- //从数组合并元素到字符串- componentsJoinedByString:
- NSArray *array = [[NSArray alloc] initWithObjects:@”One”,@”Two”,@”Three”,@”Four”,nil];
- NSString *string = [array componentsJoinedByString:@”,”];
大写: [a uppercaseString];
小写: [a lowercaseString]
截取:
从头开始:[a substringToIndex:3];
到哪里: [a substringFromIndex:3];
一段: [a substringWithRange:NSMakeRange(0,4)];
文件扩展名: [path pathExtension]
[html] view plaincopy

- /*—————-从文件读取字符串:initWithContentsOfFile方法—————-*/
- NSString *path = @”astring.text”;
- NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
- NSLog(@”astring:%@”,astring);
- [astring release];
- /*—————-写字符串到文件:writeToFile方法—————-*/
- 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
[html] view plaincopy

- NSArray *array = [[NSArray alloc] initWithObjects:@”One”,@”Two”,@”Three”,@”Four”,nil]; //注意用nil结尾
增加元素: addObject
删除:removeObjectAtIndex:1
复制数组:
直接复制
[html] view plaincopy

- NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
- NSArray *array = [NSArray arrayWithObjects:
- @”a”,@”b”,@”c”,nil];
- MutableArray = [NSMutableArray arrayWithArray:array];
用循环:
[html] view plaincopy

- 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];
- }
快速枚举:
[html] view plaincopy

- for(id obj in oldArray)
- {
- [newArray addObject: obj];
- }
或者:
[html] view plaincopy

- NSEnumerator *enumerator;
- enumerator = [oldArray objectEnumerator]; //<span style=”font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace; line-height: 13.183333396911621px; white-space: pre; “>reverseObjectEnumerator 从后往前</span>
- id obj;
- while(obj = [enumerator nextObject])
- {
- [newArray addObject: obj];
- }
排序:
[html] view plaincopy

- [newArray sortUsingSelector:@selector(compare:)];
5.NSDictionary
[html] view plaincopy

- NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@”One”,@”1″,@”Two”,@”2″,@”Three”,@”3″,nil];
- NSString *string = [dictionary objectForKey:@”One”];
[html] view plaincopy

- <code class=”objc plain” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; min-height: inherit !important; white-space: pre; “>[dictionary setObject:@</code><code class=”objc string” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; min-height: inherit !important; color: blue !important; white-space: pre; “>”One”</code><span style=”color: rgb(0, 128, 0); font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace; line-height: 13.183333396911621px; white-space: pre; “> </span><code class=”objc plain” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; min-height: inherit !important; white-space: pre; “>forKey:@</code><code class=”objc string” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; min-height: inherit !important; color: blue !important; white-space: pre; “>”1″</code><code class=”objc plain” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; min-height: inherit !important; white-space: pre; “>];</code>
[html] view plaincopy

- <code class=”objc plain” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; min-height: inherit !important; white-space: pre; “></code><div class=”line number476 index475 alt1” style=”outline: 0px !important; margin: 0px !important; padding: 0px 1em !important; word-break: break-all; word-wrap: break-word; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; background-color: white !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace; min-height: inherit !important; white-space: pre !important; color: rgb(0, 128, 0); “><code class=”objc comments” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; font-size: 1em !important; min-height: inherit !important; color: rgb(0, 130, 0) !important; “>//删除指定的字典</code></div><div class=”line number477 index476 alt2” style=”outline: 0px !important; margin: 0px !important; padding: 0px 1em !important; word-break: break-all; word-wrap: break-word; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; background-color: white !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 13.183333396911621px; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace; min-height: inherit !important; white-space: pre !important; color: rgb(0, 128, 0); “><code class=”objc plain” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; font-size: 1em !important; min-height: inherit !important; color: black !important; “>[dictionary removeObjectForKey:@</code><code class=”objc string” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; font-size: 1em !important; min-height: inherit !important; color: blue !important; “>”3″</code><code class=”objc plain” style=”outline: 0px !important; margin: 0px !important; padding: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.1em !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, ‘Bitstream Vera Sans Mono’, ‘Courier New’, Courier, monospace !important; font-size: 1em !important; min-height: inherit !important; color: black !important; “>];</code></div>
6.文件夹操作:
[html] view plaincopy

- /*******************************************************************************************
- 从目录搜索扩展名为jpg的文件
- *******************************************************************************************/
- //NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *home;
- home = @”../Users/”;
- NSDirectoryEnumerator *direnum;
- direnum = [fileManager enumeratorAtPath: home];
- NSMutableArray *files = [[NSMutableArray alloc] init];
- //枚举
- NSString *filename;
- while (filename = [direnum nextObject]) {
- if([[filename pathExtension] hasSuffix:@”jpg”]){
- [files addObject:filename];
- }
- }
- //快速枚举
- //for(NSString *filename in direnum)
- //{
- // if([[filename pathExtension] isEqualToString:@”jpg”]){
- // [files addObject:filename];
- // }
- //}