objective-c

[objective-c]NSURLDownload

NSURLDownload

- (void)startDownloadWithURL:(NSString*)urlstr
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:5.0];
    NSURLDownload  *download = [[NSURLDownload alloc] initWithRequest:request delegate:self];
    if (!download) {

    }
}
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
    NSString *destinationFilename = @"ローカルファイルパス";
    [download setDestination:destinationFilename allowOverwrite:YES];
}
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
{

}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
{

}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{

}
- (void)downloadDidFinish:(NSURLDownload *)download
{

}

[objective-c]NSViewのフェード

NSViewのフェード

view.wantsLayer = YES;
view.hidden = NO;
view.layer.opacity = 1.0;
CABasicAnimation * alphaAnimation  = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.removedOnCompletion =  YES;
alphaAnimation.autoreverses = NO;
alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
alphaAnimation.duration = fadeDuration_;
alphaAnimation.fromValue = [NSNumber numberWithFloat:0.0];
alphaAnimation.toValue = [NSNumber numberWithFloat:1.0];
alphaAnimation.byValue = [NSNumber numberWithFloat:1.0];
[view.layer addAnimation:alphaAnimation forKey:@"opacityAnimation"];

[Swift]objective-cで書かれたクラスを扱う

objective-cで書かれたクラスを扱う

1.「Common-Bridging-Header.h」というヘッダーファイルを作成する
*ファイル名は任意

2.Common-Bridging-Header.hに下記を記述する

#import "EGODatabase.h"

3.PROJECT -> Build Settings -> Swift Compiler – Code Generation
-> Objective-C Bridging Headerに作成したファイル名を設定する
Objective-C Bridging Header

4.普通に扱う

let ego:EGODatabase = EGODatabase(path: "\(NSHomeDirectory())/Documents/ultrawoman.db")
private init(){
    self._initSystemTable()
}
private func createTableWithTableName(tableName:String, columnInfo:String) -> Bool{
    let query:String = "CREATE TABLE IF NOT EXISTS '\(tableName)' (\(columnInfo));"
    let result:EGODatabaseResult = self.ego.executeQuery(query)
    if 0 < result.errorCode {
        return false
    }
    return true
}
private func _initSystemTable(){
    let table:String = "id INTEGER PRIMARY KEY AUTOINCREMENT,key TEXT DEFAULT '',value TEXT DEFAULT '',modified TEXT,created TEXT"
    let result:Bool = self.createTableWithTableName(self.tableNameOfSystem, columnInfo: table)
}

[objective-c]NSTimerがFireしない

NSTimerがFireしない
UIAlertViewのYes,Noボタンをタップしたタイミングと重なったり、
何らかの要因によりNSTimerがサブスレッドで実行されることが原因

対処法

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(methodName) userInfo:nil repeats:NO];

これを

dispatch_async(dispatch_get_main_queue(), ^{
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(methodName) userInfo:nil repeats:NO];
});

こうする

[objective-c]日付文字列まとめ

日付文字列まとめ

_currentDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];

NSDateFormatter* form = [[NSDateFormatter alloc] init];
[form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[form setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
_currentDatetimeString = [form stringFromDate: _currentDate ];
[form setDateFormat:@"yyyy-MM-dd"];
_currentDateString = [form stringFromDate: _currentDate ];
[form setDateFormat:@"yyyy-MM"];
_currentYMString = [form stringFromDate: _currentDate ];
[form setDateFormat:@"HH:mm:ss"];
_currentTimeString = [form stringFromDate: _currentDate ];

[objective-c]RecurrenceなEKEventを全て削除

RecurrenceなEKEventを全て削除

EKEventStore*eventstore = [[EKEventStore alloc] init];
EKEvent*firstEvent = [eventstore eventWithIdentifier:targetRecurrenceEvent.eventIdentifier];
NSError*error;
BOOL result = [eventstore removeEvent:firstEvent span:EKSpanFutureEvents commit:YES error:&error];