程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> object-c常用類

object-c常用類

編輯:關於C語言

1.協議代理

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;


//使用協議代理Delegate
-(IBAction)changeColorAction:(id)sender{
    [delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];
    [[self navigationController] popViewControllerAnimated:YES];
}

@end

最終實現在B

B.h

#import 
#import "ViewControllerA.h"
@interface ViewControllerB : UIViewController

@end

B.m

#import "ViewControllerB.h"

@implementation ViewControllerB

//響應協議代理Delegate
-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{
    [self.view setBackgroundColor:color];
}
@end

2.通知中心(其他同上面的例子)

a.m

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

b.m

#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"] ];

分割:(字符串分割)

NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
NSArray *array = [string componentsSeparatedByString:@","];

(數組組成字符串)

//從數組合並元素到字符串- 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]



/*----------------從文件讀取字符串: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

NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];  //注意用nil結尾

增加元素: addObject

刪除:removeObjectAtIndex:1



復制數組:

直接復制

NSMutableArray *MutableArray = [[NSMutableArray alloc] init];
NSArray *array = [NSArray arrayWithObjects:
                  @"a",@"b",@"c",nil];
MutableArray = [NSMutableArray arrayWithArray:array];
用循環:

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];
}
快速枚舉:

for(id obj in oldArray)
{
    [newArray addObject: obj];
}

或者:

NSEnumerator *enumerator;
enumerator = [oldArray objectEnumerator];   //reverseObjectEnumerator 從後往前
id obj;
while(obj = [enumerator nextObject])
{
    [newArray addObject: obj];
}


排序:

[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"];

//刪除指定的字典[dictionary removeObjectForKey:@"3"];



6.文件夾操作:

/*******************************************************************************************
 從目錄搜索擴展名為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];
//    }
//}





  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved