영삼이의 IT정보2012. 6. 1. 16:18

- (void)myStringExample

{

    // 파일명에서 확장자 얻기

    NSString *fileName = @"somefile.txt";

    NSString *fileExtension = [fileName pathExtension];

    

    // 공백문자 없애기

    NSString *myString = @" one two three ";

    NSString *trimed = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    

    NSString *string1 = @"some string";

    NSString *string2 = @"some other string";

    NSString *string3 = @"Some StRiNg";

    NSString *string4 = @"some other string";

    

    // 문자열 비교

    NSLog(@"%i",[string2 isEqualToString:string4]);

    NSLog(@"%i",[string1 isEqualToString:string3]);

    NSLog(@"%i",([string1 caseInsensitiveCompare:string3] == NSOrderedSame));

    

    // 스트링을 double, int값으로 변경하기 

    NSString *n = @"12345";

    double d = [n doubleValue];

    int i = [n intValue];

    NSLog(@"%f, %d",d,i);

    

    // URL에서 HTML얻기

    NSURL *url = [NSURL URLWithString:@"http://google.com"];

    NSString *pageContents = [NSString stringWithContentsOfURL:url];

    

    NSLog(@"%@", pageContents);

    

    // 숫자 변경

    NSString *numberString = @"one two three";

    NSRange foundRange = [numberString rangeOfString:@"two"];

    NSString *oneString = [numberString substringToIndex:3];

    NSString *twoString = [numberString substringWithRange:NSMakeRange(43)];

    NSString *threeString = [numberString substringFromIndex:8];

    

    NSString *repeatString = [numberString 

                              stringByReplacingOccurrencesOfString:@"three" withString:@"four"];

    BOOL found = ([numberString rangeOfString:@"two1"].location != NSNotFound);

    

    NSString *stringa = @"one";

    NSString *stringb = [stringa stringByAppendingString:@" two"];

    

    NSLog(@"%i - %i",foundRange.location, foundRange.length);

    NSLog(@"%@",repeatString);

    NSLog(@"[%@] [%@] [%@]",oneString,twoString,threeString);

    NSLog(@"%@",[numberString componentsSeparatedByString:@" "]);

}


- (void)myDateExample

{

    // 현재 날짜시간 얻기

    NSDate *myDate = [NSDate date];

    NSLog(@"%@", myDate);

    

    // 다음 날짜 얻기(초를 기반한 interval 계산)

    NSTimeInterval secondsPerDay = 24*60*60;

    NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

    NSLog(@"%@", tomorrow);

    

    // 이전 날짜 얻기

    NSDate *now = [NSDate date];

    NSDate *yesterday = [now addTimeInterval:-secondsPerDay];

    NSLog(@"%@", yesterday);

    

    // 날짜 차이 얻기

    NSTimeInterval secondsBetweenDates = [yesterday timeIntervalSinceDate:now];

    NSLog(@"%@", secondsBetweenDates);


    NSDateComponents *comp = [[NSDateComponents allocinit];

    [comp setMonth:06];

    [comp setDay:01];

    [comp setYear:2010];

    NSCalendar *myCal = [[NSCalendar allocinitWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *myDate2 = [myCal dateFromComponents:comp];

    

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    NSString *friendlyDate = [formatter stringFromDate:now];

    

    NSLog(@"%@", friendlyDate);

    

    [formatter setDateFormat:@"yyyy-mm-dd"];

    NSString *friendlyDate2 = [formatter stringFromDate:now];

    

    NSLog(@"%@", friendlyDate2);

    

}


- (void)myArrayExample 

{

    NSString *string1 = @"one";

    NSString *string2 = @"two";

    NSString *string3 = @"three";

    

    NSArray *myArray = [NSArray arrayWithObjects:string1, string2, string3, nil];

    

    // Array 복사

    NSArray *myArray2 = [NSArray arrayWithArray:myArray];

    

    // 범위를 지정해서 Array 복사

    NSRange range = NSMakeRange(02);

    NSArray *subArray = [myArray subarrayWithRange:range];

    

    int arrayLength = [myArray count];

    NSLog(@"%d", arrayLength);

    

    int arrayLength2 = [subArray count];

    NSLog(@"%d", arrayLength2);



    // 루프를 출력

    for (NSString *obj in myArray) {

        NSLog(@"%@", obj);

    }

    

    // 루프를 거꾸로 출력

    for (NSString *obj2 in [myArray reverseObjectEnumerator]) {

        NSLog(@"%@", obj2);

    }

    

    // 정렬

    [myArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    

}


- (void)myDictionaryExample 

{

    NSArray *arr1 = [NSArray arrayWithObjects:@"iPhone"@"iPod"nil];

    NSArray *arr2 = [NSArray arrayWithObjects:@"iMac"@"Mac Pro"@"Macbook"@"Macbook Pro"nil];

    

    //  부분에서 오류가 

    NSDictionary *myDict = [[NSDictionary allocdictionaryWithObjectsAndKeys:arr1, @"mobile", arr2, @"computers"nil];

    

    int dictSize = [myDict count];

    NSLog(@"%d", dictSize);

    

}


- (void)myNotificationExample

{

    // Notification 등록

    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(doSomething:) name:MY_NOTIFICATION object:nil];

    

    // Notification 호출

    [[NSNotificationCenter defaultCenterpostNotificationName:MY_NOTIFICATION object:nil];

    

}


- (void)doSomething:(NSNotification *) aNote 

{

    NSLog(@"doSomething called...");

    

    // 만약 aNote 통해서 뭔가 전달되었다면

    // [aNote object] 받아서 처리함

    

}


- (void)myTimerExample 

{

    // 타이머 생성

    //NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(myTimerAction:) userInfo:nil repeats:NO];

    

    //[myTimer invalidate];

    // 하루의   

    NSTimeInterval secondsPerDay = 24*60*60;

    NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

    NSTimer *myTimer = [[NSTimer allocinitWithFireDate:tomorrow interval:10.0 target:self selector:@selector(myTimerAction) userInfo:nil repeats:NO];

    

    [[NSRunLoop mainRunLoopaddTimer:myTimer forMode:NSDefaultRunLoopMode];

    

}


Posted by 다오나무