[Swift]sort

sort

var w:[AnyObject] = self.subviews
w.sort { (v1:AnyObject, v2:AnyObject) -> Bool in
    return v1.tag < v2.tag
}

[Swift]クラス拡張

クラス拡張 objective-cにおけるカテゴリ

extension UIView {
    var left:CGFloat{
        get{
            return self.frame.origin.x
        }
        set{
            self.frame = CGRectMake(newValue, self.frame.origin.y, self.frame.size.width, self.frame.size.height)
        }
    }
    func toUIImage() -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
        self.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

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