[Swift]参照渡し
参照渡し
class func addMonthWithYearMonth(inout ym:(year:Int,month:Int),addMonth:Int){ var y:Int = ym.year var m:Int = ym.month + addMonth while 12 < m { m -= 12 y++ } while m < 1 { m += 12 y-- } ym.year=y ym.month=m }
参照渡し
class func addMonthWithYearMonth(inout ym:(year:Int,month:Int),addMonth:Int){ var y:Int = ym.year var m:Int = ym.month + addMonth while 12 < m { m -= 12 y++ } while m < 1 { m += 12 y-- } ym.year=y ym.month=m }
言語が日本語かどうか判定
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)
Swift #5 animateWithDuration
@IBOutlet weak var mainLabel: UILabel! func initUI(){ var message:String = getMessage(message:"いあうえ",message2:"390u8") println(message) message = getMessage(message2:"むぎちゃ") println(message) UIView.animateWithDuration( 2.0, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations:{()in self.mainLabel.frame = CGRectMake(100.0, 300.0, self.mainLabel.frame.size.width, self.mainLabel.frame.size.height) }, completion:{(finished)in println("finished:\(finished)") }) } func getMessage(var message:String="ABC",var message2:String="MBNRKF")->String{ return "\(message) \(message2)" } override func viewDidLoad() { super.viewDidLoad() initUI() }
出力結果
いあうえ 390u8 ABC むぎちゃ finished:true
Swift #4 カメラを扱う
ViewController
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @IBOutlet var mainImageView: UIImageView var picker:UIImagePickerController? override func viewDidLoad() { super.viewDidLoad() picker = UIImagePickerController() picker!.delegate = self picker!.allowsEditing = false } @IBAction func onTapButton1(button:UIButton){ println( "onTapButton - open album" ) if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(picker, animated: true, completion: nil) } } @IBAction func onTapButton2(button:UIButton){ println( "onTapButton - take photo" ) if picker==nil { return } if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { picker!.sourceType = UIImagePickerControllerSourceType.Camera self.presentViewController(picker, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){ println( "imagePickerController" ) self.dismissViewControllerAnimated(true, completion: nil) mainImageView.image = image if picker!.sourceType == UIImagePickerControllerSourceType.Camera { println( "save photo" ) UIImageWriteToSavedPhotosAlbum(image, self, "onSaveImageWithUIImage:error:contextInfo:", nil) } } func onSaveImageWithUIImage(image: UIImage!, error: NSErrorPointer, contextInfo: UnsafePointer<()>){ if error { println( "error" ) return } println( "success" ) } func imagePickerControllerDidCancel(picker: UIImagePickerController!){ println( "imagePickerControllerDidCancel" ) self.dismissViewControllerAnimated(true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }