[Swift]言語が日本語かどうか判定
言語が日本語かどうか判定
func isCurrentLangJa() -> Bool{ let lang:[AnyObject] = NSLocale.preferredLanguages() return lang[0] as String == "ja" }
言語が日本語かどうか判定
func isCurrentLangJa() -> Bool{ let lang:[AnyObject] = NSLocale.preferredLanguages() return lang[0] as String == "ja" }
sort
var w:[AnyObject] = self.subviews w.sort { (v1:AnyObject, v2:AnyObject) -> Bool in return v1.tag < v2.tag }
tagが0以上のsubviewを取得するクラス拡張
extension UIView { var subviewsTagIsGreaterThan0:[AnyObject]{ get{ return self.subviews.filter({(w:AnyObject) -> Bool in w.tag > 0}) } } }
tagでソートするクラス拡張
extension UIView{ var subviewsSortByTag:[AnyObject]{ get{ var w:[AnyObject] = self.subviews w.sort { (v1:AnyObject, v2:AnyObject) -> Bool in return v1.tag < v2.tag } return w } } }
名前付タプル配列
var draws:[(x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat)] = [] self.draws.append( (x:0.0, y:0.0, width:100.0, height:0.0) )
クラス拡張 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 } }
String整形
String(format: "%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,min,sec)
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]; });
こうする
日付文字列まとめ
_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 ];