[objective-c]NSDictionaryからjson文字列を生成
NSDictionaryからjson文字列を生成
NSError*error=nil; NSData*data=[NSJSONSerialization dataWithJSONObject:dic options:2 error:&error]; NSString*jsonstr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionaryからjson文字列を生成
NSError*error=nil; NSData*data=[NSJSONSerialization dataWithJSONObject:dic options:2 error:&error]; NSString*jsonstr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDateから文字列生成
-(NSString*) getDatetimeFromDate:(NSDate*)date { NSDateFormatter* form = [[NSDateFormatter alloc] init]; NSCalendar* cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [form setCalendar: cal]; [form setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString* str = [form stringFromDate:date]; return str; } NSString*dateString=[self getDatetimeFromDate:[NSDate date]];
カラムの型一覧取得
-(EGODatabase*)getDB { EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]]; return database; } -(NSDictionary*)getColumnTypesByTableName:(NSString*)tableName { EGODatabase*db=[self getDB]; NSString*sql=[NSString stringWithFormat:@"pragma table_info(%@)",tableName]; EGODatabaseResult*result=[db executeQuery:sql]; NSMutableDictionary*dic=[NSMutableDictionary dictionary]; for (EGODatabaseRow*row in result) { [dic setObject:[row stringForColumn:@"type"] forKey:[row stringForColumn:@"name"]]; } return dic; }
カラム名一覧取得
-(NSArray*)getColumnNamesByTableName:(NSString*)tableName{ EGODatabase*db=[self getDB]; NSString*sql=[NSString stringWithFormat:@"pragma table_info(%@)",tableName]; EGODatabaseResult*result=[db executeQuery:sql]; NSMutableArray*list=[NSMutableArray arrayWithCapacity:[result count]]; for (EGODatabaseRow*row in result) { [list addObject:[row stringForColumn:@"name"]]; } return [NSArray arrayWithArray:list]; }
テーブル一覧取得
-(NSArray*)getTableNameList{ EGODatabase*db=[self getDB]; NSString*sql=@"select name from sqlite_master where type = 'table' order by name"; EGODatabaseResult*result=[db executeQuery:sql]; NSMutableArray*list=[NSMutableArray arrayWithCapacity:[result count]]; for (EGODatabaseRow*row in result) { [list addObject:[row stringForColumn:@"name"]]; } return [NSArray arrayWithArray:list]; } -(EGODatabase*)getDB{ EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]]; return database; } -(BOOL)isExistTableByName:(NSString*)tableName{ NSArray*tables=[self getTableNameList]; return [tables containsObject:tableName]; }
ファイル存在確認
//Documentsフォルダ内を確認 -(BOOL)isExistFile:(NSString*)fileName{ NSString* path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; path=[path stringByAppendingPathComponent:fileName]; return [[NSFileManager defaultManager] fileExistsAtPath:path]; } //fullパスで確認 -(BOOL)isExistFileByFullPath:(NSString*)path{ return [[NSFileManager defaultManager] fileExistsAtPath:path]; }
UISliderのつまみをアニメーションさせる
[UIView animateWithDuration:0.3f animations:^{ [slider setValue:2.0 animated:YES]; } ];
UIButtonのラベルをフォントサイズ自動縮小にする
UILabel*innerLabel= btn.titleLabel; innerLabel.adjustsFontSizeToFitWidth=YES; innerLabel.lineBreakMode=NSLineBreakByClipping;//これがないとフォントが自動縮小しない innerLabel.numberOfLines=3;
UIButtonのタイトルの位置,サイズ調整
UIButton*btn=[UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitleEdgeInsets:UIEdgeInsetsMake(6.0f, 7.0f, 8.0f, 9.0f];//top margin:6.0, left margin:7.0, bottom margin:8.0, right margin:9.0
バンドルしたテキストファイルを読み込む
NSString *path = [[NSBundle mainBundle] pathForResource:@"testdata" ofType:@"json"]; NSError* error; NSString* dataString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];