- (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(4, 3)]; 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 alloc] init]; [comp setMonth:06]; [comp setDay:01]; [comp setYear:2010]; NSCalendar *myCal = [[NSCalendar alloc] initWithCalendarIdentifier: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(0, 2); 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 alloc] dictionaryWithObjectsAndKeys:arr1, @"mobile", arr2, @"computers", nil]; int dictSize = [myDict count]; NSLog(@"%d", dictSize); } - (void)myNotificationExample { // Notification 등록 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:MY_NOTIFICATION object:nil]; // Notification 호출 [[NSNotificationCenter defaultCenter] postNotificationName: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 alloc] initWithFireDate:tomorrow interval:10.0 target:self selector:@selector(myTimerAction) userInfo:nil repeats:NO]; [[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode]; }
'영삼이의 IT정보' 카테고리의 다른 글
XHTML/CSS 무료 템플릿 배포사이트 (0) | 2012.06.01 |
---|---|
메인 runloop 쓰레드에 타이머 동작 시키기 (0) | 2012.06.01 |
NSUserDefaults 유용하고 빠른 파일저장 (0) | 2012.05.31 |
iOS5의 UDID 정책 변경에 대한 퀵 리뷰 (iOS5 beta 6 기준) (0) | 2012.05.31 |
ASIHttpRequest를 ARC와 함께 쓰기 (XCode 4.2) (0) | 2012.05.30 |