본문 바로가기
프로그래밍/iOS

여러가지 Util

by 백룡화검 2011. 3. 29.
이번에 아이폰 관련 어플개발하면서 필요한것 만들어 봤습니다.

어플 개발시작한지 얼마 안되어서 소스가 허접하더라도 이해해 주세요..


// 금액으로 표시 
+ (NSString *) numberFormat:(NSString*)price {
NSNumber *result = nil;
NSString* ret = nil;
@try {
result = [NSNumber numberWithLongLong: [price longLongValue]];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setDecimalSeparator:@","];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
ret = [numberFormatter stringFromNumber:result];
[numberFormatter release];
}
@catch (NSException * e) {
ret = @"0";
}
@finally {

}

return ret;
}

// 금액으로 표시
+ (NSString *) numberFormat:(NSString*)price str:(NSString*) str {
return [[JUtil numberFormat:price] stringByAppendingString:str];
}

// substring
+ (NSString *) substr:(NSString*)str index:(NSInteger)idx {
NSString *ret = nil;
if (idx<0) {
ret = [str substringFromIndex:[str length]+idx];
}
else {
ret = [str substringToIndex:idx];
}
return ret;
}

// substring
+ (NSString *) substr:(NSString*)str startIndex:(NSUInteger)sidx endIndex:(NSUInteger)eidx {
NSRange strRange = {sidx,eidx};
NSString *ret = [str substringWithRange:strRange];
return ret;
}

// trim
+ (NSString *) trim:(NSString*)str {
NSString *ret = [str stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
return ret;
}

// explode
+ (NSArray *) explode:(NSString*)delimiter string:(NSString*)str {
NSArray *ret = [str componentsSeparatedByString:delimiter];
return ret;
}

// strReplace
+ (NSString *) strReplace:(NSString *)search replace:(NSString *)replace string:(NSString*)str {
NSString *ret = [str stringByReplacingOccurrencesOfString:search withString:replace];

return ret;
}

// 원하는 날짜 가져오기 format : yyyymmdd
+ (NSString*) getDays:(NSInteger)gab format:(NSString*)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
NSDate *today = [[NSDate alloc] init];
NSTimeInterval secondsPerDay = (24 * 60 * 60)*gab;
NSString* ret = [formatter stringFromDate:[today addTimeInterval:secondsPerDay]];
[formatter release];
[today release];
return ret;
}

// 2010.03.10 포맷으로
+ (NSString*) dateViewFormat:(NSString*)dateStr divStr:(NSString*)divStr {
NSString* ret = @"";
if (dateStr.length>0) {
ret = [[[NSString alloc] initWithFormat:@"%@%@%@%@%@"
 , [JUtil substr:dateStr startIndex:0 endIndex:4], divStr
 , [JUtil substr:dateStr startIndex:4 endIndex:2], divStr
 , [JUtil substr:dateStr startIndex:6 endIndex:2]] autorelease];
}else {
ret = @"";
}

return ret;
}


// int to string convert
+ (NSString*) intToString:(NSInteger)intValue{
NSString* ret = [NSString stringWithFormat:@"%d", intValue];
return ret;
}


// iphone 여부
+ (BOOL) isIPhone {
NSString* model = [[UIDevice currentDevice] model];
NSRange range = [model rangeOfString:@"iPhone"];
if (range.length>0) {
return YES;
}
else {
return NO;
}
}

// 문자/전화/지도관련 어플 실행
+ (void) systemAppRun:(NSString*)protocol param:(NSString*)param {
NSString* paramStr = [NSString stringWithFormat:@"%@:%@", protocol, param];
NSString* url = [[NSString stringWithString:paramStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

// 공백값인가
+ (BOOL) isEmpty:(NSString*)str {
if ([@"" isEqualToString:str]) {
return YES;
}
else {
return NO;
}
}

// alert 메세지 출력
+ (void) showAlertMessage:(NSString*)msg {
[JUtil showAlertMessage:msg title:@""];
}

// alert 메세지 출력
+ (void) showAlertMessage:(NSString*)msg title:(NSString*)title {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg
  delegate:nil
 cancelButtonTitle:@"확 인"
 otherButtonTitles:nil];
[alertView show];
[alertView release];
}