objective - c 의 상용 기초 류

몇 가지 상용 기초 류 의 사용 예시
몇 개 밖 에 안 했 어 요. 다음 에 실험실 가서 iMac 테스트 를 계속 할 게 요.
//
//  main.m
//  Array_Test
//
//  Created by mac11 on 12-3-7.
//  Copyright (c) 2012  __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSArray *array = [ [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six", nil];
        int count = [array count];
        NSLog(@"This array has %d elements.
",count); // if([array containsObject:@"Two"]) NSLog(@"Two is in the array"); else NSLog(@"It's not in the array"); // id object = [array lastObject]; NSLog(@"%@",object); object = [array objectAtIndex:3]; NSLog(@"%@",object); NSMutableArray *marray = [[NSMutableArray alloc] init]; // marray = [NSMutableArray arrayWithArray:array]; NSLog(@"NSMUlableArray :%@",marray); /* NSEnumerator *enumerator = [anArray objectEnumerator]; id object; while ((object = [enumerator nextObject])) { // do something with object... */ NSEnumerator *enumerator = [array objectEnumerator]; id object1; int num=1; while(object1 = [enumerator nextObject]) { NSLog(@"The %ith element of the array is :%@",num++,object1); } } return 0; }
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
       //NSLog(@"Hello, World!");
        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1",@"key1",@"value2",@"key2", nil];
        int count = [dic count];
        NSLog(@"The Dictionary is :%@",dic);
        NSLog(@"The count is %i",count);
        
    }
    return 0;
}
//
//  main.m
//  string_test
//
//  Created by mac11 on 12-3-7.
//  Copyright (c) 2012  __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        NSString *str1 = @"I'm a men,this is a NSstring";
        NSLog(@"%@",str1);
        NSString *str2 = [[NSString alloc] initWithString:@"This is the string too"];
        NSLog(@"%@",str2);
        NSString *str3 = [[NSString alloc] initWithFormat:@"My height is %i,and my weight is %i.
",170,65]; NSLog(@"%@",str3); NSString *str4 = @"This is"; if([str2 hasPrefix:str4]) NSLog(@"Oh yes"); else NSLog(@"oh no"); if([str1 isEqualToString:str2]) NSLog(@"We are the same"); else NSLog(@"We are different"); NSUInteger length=[str4 length]; NSLog(@"The length of the string is %i",length); NSMutableString *mstr1 = [[NSMutableString alloc] initWithCapacity:42]; [mstr1 appendString:@"I'm the MutableString ~"]; [mstr1 appendFormat:@", oh no~"]; NSLog(@"%@",mstr1); [mstr1 insertString:@"lallala~~ " atIndex:4]; NSLog(@"%@",mstr1); // alt+NSString } return 0; }
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // NSString usage
	NSString *str0 = [NSString stringWithString:@"Hello"]; // created by class method,don't need to be released by creator
	NSString *str = [[NSString alloc]initWithString:@"Hello,world"];
	NSLog(@"%@",str);
	if([str isEqualToString:@"Hello,world"]==YES){
		NSLog(@"str is equal to Hello,world");
	}
	// seperate the string by ,
	NSArray *subStr = [str componentsSeparatedByString:@","];
	[str release];	
	
	// NSNumber usage
	NSNumber *num = [NSNumber numberWithInt:30];
	NSNumber *myNum=[[NSNumber alloc]initWithBool:YES];
	if([[myNum className] isEqualToString:@"NSCFNumber"]){
		NSLog(@"a numeric value
"); } else if([[myNum className] isEqualToString:@"NSCFBoolean"]){ NSLog(@"a boolean value
"); } NSLog(@"number=%@
",myNum); [myNum release]; // NSArray usage // create an array containing contents of the specified file. the file must be of type .plist //NSArray *myArr2 = [[NSArray alloc] initWithContentsOfFile:@"mydata.plist"]; // create an array from objects(can be different types!),the last one must be nil NSArray *myArr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil]; NSString *myStr = [myArr objectAtIndex:2]; // [myArr lastObject] [myArr count] NSLog(@"%@",myStr); // looping through the array for(id obj in myArr) NSLog(@"array element=%@
",obj); [myArr release]; // NSMutableArray usage NSMutableArray *myMutArr = [[NSMutableArray alloc]initWithObjects:@"One",@"Two",@"Three",nil]; [myMutArr addObject:@"Four"]; // [myMutArr addObject:myNum] OK [myMutArr insertObject:@"Extra" atIndex:1]; NSLog(@"%@",[myMutArr lastObject]); [myMutArr release]; [pool drain]; return 0; }

좋은 웹페이지 즐겨찾기