objective-c

[objective-c]XMLDictionary

XMLDictionary

NSDictionary*xmldic=[NSDictionary dictionaryWithXMLString:xmlString];

<ZIP_result>
<result name="ZipSearchXML"/>
<result version="1.01"/>
<result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D0600000"/><result request_zip_num="0600000"/>
<result request_zip_version="none"/>
<result result_code="1"/>
<result result_zip_num="0600000"/>
<result result_zip_version="0"/>
<result result_values_count="1"/>
<ADDRESS_value>
<value state_kana="ホッカイドウ"/>
<value city_kana="サッポロシチュウオウク"/>
<value address_kana="イカニケイサイガナイバアイ"/>
<value company_kana="none"/>
<value state="北海道"/>
<value city="札幌市中央区"/>
<value address="none"/>
<value company="none"/>
</ADDRESS_value>
</ZIP_result>

上記のようなxmlStringだとすると
“北海道”を取得するにはこうする。

NSString*state=[xmldic valueForKeyPath:@"ADDRESS_value.value"][4][@"_state"];


XMLDictionary
https://github.com/nicklockwood/XMLDictionary

[objective-c]シングルトンクラス

シングルトンクラス

DataBaseManager.h

#import <Foundation/Foundation.h>
#import "EGODatabase.h"

@interface DataBaseManager : NSObject{
}
+ (DataBaseManager*)sharedInstance;
@end

DataBaseManager.m

#import "DataBaseManager.h"

@implementation DataBaseManager

+ (DataBaseManager*)sharedInstance {
    static DataBaseManager* _instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[DataBaseManager alloc]initSharedInstance];
    });
    return _instance;
}

- (id)initSharedInstance {
    self = [super init];
    if (self) {
    	//初期化
    }
    return self;
}
@end

使うとき

DataBaseManager*dbm=[DataBaseManager sharedInstance];

[objective-c]NSDateから文字列生成

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]];

[objective-c]EGODatabase #9 カラムの型一覧取得

カラムの型一覧取得

-(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;
}

[objective-c]EGODatabase #8 カラム名一覧取得

カラム名一覧取得

-(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];
}

[objective-c]EGODatabase #7 テーブル一覧取得

テーブル一覧取得

-(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];
}

[objective-c]ファイル存在確認

ファイル存在確認

//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];
}