objecive-c関数
9104 ワード
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//
NSString *height;
/** :
+(id) stringWithFormat: (NSString *) format,...
NSString
(。。。): 。
(+), , , , 。
-------------------
objective-c , 。 , , 。 long , ( )
。
(-)
*/
height=[NSString stringWithFormat:@"you heigh is %d feet,%d inches",5,11];
NSLog(height);
//length 。-(unsigned int) length;
if([height length]>5){
NSLog(@"height length ------");
}
//
/**
isEqualToString : , yes no
*/
NSString *thing1=@"hello";
NSString *thing2=[[NSString alloc] initWithString:@"hello"];
if([thing1 isEqualToString:thing2]){
NSLog(@"they are same");
}
/**
==: ,
*/
if(thing1==thing2){
NSLog(@"== same");
}
/*
compare: 。
compare , NSComparisonResult( ) 。
typedef enum _NSComparisonResult{
NSOrderedAscending=-1;
NSOrderedsame;
NSOrderedDescending;
} NSComparisonResult;
*/
[thing1 compare:thing2];
if(NSOrderedSame==[thing1 compare:thing2]){
NSLog(@"compare same");
}
//compare:options:
/***
-(NSComparisonResult) compare:(NSString *) string
options:(unsinged) mask;
options , |
:
NSCaseInsensitiveSearch:
NSLiteralSearch: ,
NSNumbericSearch: ,
*/
if([thing1 compare:thing2 options:NSCaseInsensitiveSearch|
NSNumericSearch]==NSOrderedSame){
NSLog(@"they match");
}
/**
-(BOOL) hasPrefix:(NSString *) aString;
-(BOOL) hasSuffix:(NSString *) aString;
*/
NSString *fileName=@"aabbbcc";
if([fileName hasPrefix:@"aa"]){
NSLog(@"begin with aa");
}
if([fileName hasSuffix:@"cc"]){
NSLog(@"end with cc");
}
//NSMutableString
//SString , NSString , 。
//+(id) stringWithCapacity:(unsinged) capacity; capacity: NSMutableString , , 。
NSMutableString *str=[NSMutableString stringWithCapacity:40];
[str appendFormat:@"sdfsdf%d",5];
[str appendString:@"ssssssss"];
NSLog(str);
//
//-(void) deleteCharactersInRange:(NSRange) range;
NSMutableString *ms;
ms=[NSMutableString stringWithCapacity:50];
[ms appendString:@"aabbccdd"];
NSRange range;
range=[ms rangeOfString:@"cc"];
[ms deleteCharactersInRange:range];
NSLog(ms);
// ,
//------------------ --------------
//NSArray ,NSDictionary
/**
NSArray cocoa , 。
NSArray :1, objective-c , c int,float,enum,struct, nsarray 。2, nsarray nil
:
arrayWithObjects: nsarray。 , nil ,( nsarray nil )
*/
NSArray *array=[NSArray arrayWithObjects:@"aa",@"bb",@"cc",nil];
//-(unsigned) count;
//-(id) objectAtIndex:(unsigned int) index;
int i;
for (i=0; i<[array count]; i++) {
NSLog(@"index %d has %@",i,[array objectAtIndex:i]);
}
//------------
//-componentsSeparatedByString:
NSString *ns=@"sdf,dsfs,dfd,fdf,df,dd";
NSArray *comArr=[ns componentsSeparatedByString:@","];
for(int i=0;i<[comArr count];i++){
NSLog(@"componentsSeparatedByString===%@",[comArr objectAtIndex:i]);
}
//componentJoinedByString: nsarray
NSString *joinedStr=[comArr componentsJoinedByString:@"-->"];
NSLog(@"joined---= %@",joinedStr);
//
NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:40];
[mutableArr addObject:@"aa"];
[mutableArr addObject:@"bb"];
[mutableArr addObject:@"cc"];
[mutableArr addObject:@"dd"];
for(int i=0;i<[mutableArr count];i++){
NSLog(@"mutableArr==%@",[mutableArr objectAtIndex:i]);
}
//----- -(void) removeObjectAtIndex:(unsinged) index; ,
// , ,
[mutableArr removeObjectAtIndex:2];
for(int i=0;i<[mutableArr count];i++){
NSLog(@"removeObjectAtIndex == %@",[mutableArr objectAtIndex:i]);
}
//
//NSEnumerator , cocoa
//-(NSEnumerator *) objectEnumerator;
NSEnumerator *enumerator=[mutableArr objectEnumerator];
id thingie;
while(thingie=[enumerator nextObject]){
NSLog(@"i found %@",thingie);
}
//
for(NSString *string in mutableArr){
NSLog(@"for in == %@",string);
}
//NSDictionary
/*
NSDictionary ( NSString ) ( )。 。
NSDictionary 。 , 。
+(id) dictionaryWithObjectAndKeys:(id) firstObject,....;
, nil 。
**/
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"a",@"bbb",@"b",nil];
NSString *dicStr=[dic objectForKey:@"a"];
if([dicStr isEqualToString:@"aaa"]){
NSLog(@"------------00000000000000000");
}
//
NSMutableDictionary *mutableDic=[NSMutableDictionary dictionaryWithCapacity:50];
[mutableDic setObject:@"1111" forKey:@"1"];
[mutableDic setObject:@"222" forKey:@"2"];
// -(void) removeObjectForKe:(id) key;
[mutableDic removeObjectForKey:@"2"];
NSArray *keyArr=[mutableDic allKeys];
for(NSString *str in keyArr){
NSLog(@"key== %@",str);
NSLog(@"value== %@",[mutableDic objectForKey:str]);
}
// ,NSNumber NSValue
/*
cocoa NSNumber
+(NSNumber *) numberWithChar:(char) value;
+(NSNumber *) numberWithInt:(int) value;
+(NSNumber *) numberWithFloat:(float) value;
+(NSNumber *) numberWthiBool:(BOOL) value;
-(char) charValue;
-(int) intVlaue;
-(float) floatValue;
-(BOOL) boolValue;
-(NSString *) stringValue;
**/
NSNumber *number;
number=[NSNumber numberWithInt:3];
[mutableDic setObject:number forKey:@"int"];
int num=[[mutableDic objectForKey:@"int"] intValue];
NSLog(@"int object value== %d",num);
//NSValue .NSNumber NSValue ,NSValue
/**
+(NSValue *) valueWithBytes:(const void *) value objCType:(const char *) type;
, , ( c & ), , struct 。
,@encode
*/
NSRect rect= NSMakeRect(1, 2, 30, 40);
NSValue *value;
value=[NSValue valueWithBytes:&rect objCType:@encode(NSRect)];
NSMutableArray *mr=[NSMutableArray arrayWithCapacity:50];
[mr addObject:value];
//getValue
/**
-(void) getValue:(void *) value;
*/
/***
value=[mr objectAtIndex:0];
NSRect r;
NSLog(@"00000 ===%@",r);
[value getValue:&r];
NSLog(@"111== %@",r);
*/
/**
+(NSValue *) valueWithPoint:(NSPoint) point;
+(NSValue *) valueWithSize:(NSSize) size;
+(NSValue *) valueWithRect:(NSRect) rect;
-(NSPoint) pointValue;
-(NSSize) sizeValue;
-(NSRect) rectValue;
*/
//NSNull
/*
*+(NSNull *) null;
*/
[mutableDic setObject:[NSNull null] forKey:@"fax"];
id fax;
fax=[mutableDic objectForKey:@"fax"];
if(fax==[NSNull null]){
NSLog(@"pppppppppppppppppp");
}
[pool drain];
return 0;
}