环境:macos10.1,xcode3.2
创建类对象,初始化属性
create a project 创建一个macOS的Foundation框架的项目,在新版本表示为command line tool项目
in projectname.m主函数直接使用song类,手动分配内存释放内存,涉及内存管理引用计数。新版xcode有自动引用计数,但有时还是要手动操作:
#import <Foundation/Foundation.h> #import "song.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // insert code here... NSLog(@"Hello, World!"); song *s1=[[song alloc] initwithname:@"two tiggers" get_artist:@"libai" get_price:20]; song *s2=[[song alloc] initwithartist:@"wangwei" get_name:@"three pigs" get_price:30]; song *s3=[[song alloc] initwithprice:40]; NSLog(@"s1:%@",s1); NSLog(@"s2:%@",s2); NSLog(@"s3:%@",s3); [s1 release]; [s2 release]; [s3 release]; [pool drain]; return 0; }在工程里创建新cocoa类文件,h文件里声明,m文件里实现 create a song class
statement in song.h:
#import <Cocoa/Cocoa.h> @interface song : NSObject { NSString *name; NSString *artist; int price; } -(NSString *)get_name; -(NSString *)get_artist; -(int)get_price; -(void)set_name:(NSString *)newname; -(void)set_artist:(NSString *)newartist; -(void)set_price:(int)newprice; -(id)initwithname:(NSString *)newname get_artist:(NSString *)newartist get_price:(int)newprice; -(id)initwithartist:(NSString *)newartist get_name:(NSString *)newname get_price:(int)newprice; -(id)initwithprice:(int)newprice;实现访问器get…,设置器set…,初始化方法initwith… implementation in song.m:
#import "song.h" @implementation song //song get and set property's api -(NSString*)get_name { return name; } -(NSString *)get_artist { return artist; } -(int)get_price { return price; } -(void )set_name:(NSString*)newname { [newname retain]; [name release]; name=newname; } -(void )set_artist:(NSString*)newartist { [newartist retain]; [artist release]; artist=newartist; } -(void)set_price:(int)newprice { price=newprice; } //rewrite father api -(id)init { return [self initwithname:nil get_artist:nil get_price:0]; } -(NSString *)description { return [[self get_name] stringByAppendingString:[self get_artist]]; } -(void)dealloc { NSLog(@"dealloc %@",self); [name release]; [artist release]; [super dealloc]; } //write my api自动义初始化方法 -(id)initwithname:(NSString*)newname get_artist:(NSString *)newartist get_price:(int)newprice { [super init]; [self set_name:newname]; [self set_artist:newartist]; [self set_price:newprice]; return self; } -(id)initwithartist:(NSString *)newartist get_name:(NSString *)newname get_price:(int)newprice { [super init]; [self set_name:newname]; [self set_artist:newartist]; [self set_price:newprice]; return self; } -(id)initwithprice:(int)newprice { [super init]; [self set_price:newprice]; return self; } @end字符串使用
NSString * string1=@"one two three,oh oh oh yes"; NSRange range=[string1 rangeOfString:@","]; printf("comma location:%i\n", range.location); printf("location length:%i\n", range.length); NSString * head=[string1 substringToIndex:range.location]; NSString * tail=[string1 substringFromIndex:range.location + range.length]; printf("head:%s\n",[head UTF8String]); printf("tail:%s\n",[tail UTF8String]); NSMutableString*string2=[[NSMutableString alloc] init]; [string2 appendString:@"add string2"]; printf("append string2:%s\n",[string2 UTF8String]); [string2 replaceCharactersInRange:[string2 rangeOfString:@"add"] withString:@"replace"]; printf("replace string2:%s\n",[string2 UTF8String]); NSRange range2=[string2 rangeOfString:@"ing"]; [string2 deleteCharactersInRange:(NSRange)range2]; printf("delete string2:%s\n",[string2 UTF8String]); [string2 insertString:@"ong" atIndex:range2.location]; printf("insert string2:%s\n",[string2 UTF8String]); [string2 release];(3)get filename 获取文件名
NSString * filename =@"~/Documents/se0864/10-25/get_filename/get_file/get_file.m"; //NSString * filename =@"/Users/se0030a/Documents/se0864/10-25/get_filename/get_file/get_file.m"; filename=[filename stringByStandardizingPath]; printf("standard path:%s\n",[filename UTF8String]); NSString * source=[NSString stringWithContentsOfFile:filename]; printf("file content:%s\n",[source UTF8String]);(4)load a string into a array 字符串放进数组里
NSString * string = @"I am find Thank you"; NSArray * array = [string componentsSeparatedByString:@" "]; int count = [array count]; int i; for(i=0;i<count;i++) { printf("%i:%s\n",i,[[array objectAtIndex:i] UTF8String]); } [string writeToFile:@"/Users/se0030a/Documents/se0864/10-25/1.txt" atomically:1]; printf("%i\n",[array containsObject:@"find"]); printf("%i\n",[array containsObject:@"fond"]);mutable array: 可变数组
NSMutableArray * array = [[NSMutableArray alloc] init]; [array addObject:@"b"]; //b [array addObject:@"black"]; //black [array addObject:@"crow and kid"]; //"crow and kid" [array addObject:@"play waterball"]; //"play waterball" printf("%s\n",[[array description] UTF8String]); [array insertObject:@"very" atIndex:[array indexOfObject:@"black"]]; printf("%s\n",[[array description] UTF8String]); [array removeObjectAtIndex:1]; printf("%s\n",[[array description] UTF8String]); [array removeObject:@"black"]; printf("%s\n",[[array description] UTF8String]); [array removeObject:@"blue"]; printf("%s\n",[[array description] UTF8String]); [array release];(5)address book 地址簿的使用
#import <Foundation/Foundation.h> #import <AddressBook/AddressBook.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; ABAddressBook * book = [ABAddressBook sharedAddressBook];//get a addressbook for user NSArray * people = [book people];//get a array which contains all people in book int count = [people count]; int i; for(i=0;i<count;i++) { ABPerson * person = [people objectAtIndex:i];//get a object on book array NSString * firstname = [person valueForProperty:@"First"]; NSString * lastname = [person valueForProperty:@"Last"]; printf("%s %s\n",[lastname UTF8String],[firstname UTF8String]); } [pool drain]; return 0; }(6)dictionaries 字典使用
//using dictionaries NSArray * keys = [@"one two three four five" componentsSeparatedByString:@" "]; NSArray * values = [@"aaa,bbb,ccc,ddd,eee" componentsSeparatedByString:@","]; NSDictionary * dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; printf("%s\n",[[dict description] UTF8String]); //using mutable dictionaries NSArray * keys2 = [@"one two three four five" componentsSeparatedByString:@" "]; NSArray * values2 = [@"a,b,c,d,e" componentsSeparatedByString:@","]; NSMutableDictionary * dict2 = [[NSMutableDictionary alloc] initWithObjects:values2 forKeys:keys2]; printf("%s\n",[[dict2 description] UTF8String]); [dict2 setObject:@"e" forKey:@"four"]; [dict2 setObject:@"f" forKey:@"six"]; [dict2 removeObjectForKey:@"one"]; printf("%s\n",[[dict2 description] UTF8String]);