[Swift]文字列分割

文字列分割

let datetimeString:String = "2016-02-17 14:51:21"
let tmp:[String] = datetimeString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:"- :"))
print(tmp)

出力結果

["2016", "02", "17", "14", "51", "21"]

[Swift]dispatch_after

dispatch_after

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    //処理
}

[Swift]UIView Extension #6

UIView Extension #6

func animateBG2Black(duration:CGFloat=0.3){
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.backgroundColor = UIColor.blackColor()
        },
        completion: { (finish:Bool) -> Void in
            
        }
    )
}

[Swift]Array Extension #1

Array Extension #1

extension Array {
    mutating func firstToLast(){
        let elm = self.removeFirst()
        self.append(elm)
    }
    mutating func lastToFirst(){
        let elm = self.removeLast()
        self.insert(elm, atIndex: 0)
    }
}

使用

var tmp1:[Int] = [0,1,2,3]
tmp1.firstToLast()
print(tmp1)
tmp1.lastToFirst()
print(tmp1)

出力結果

[1, 2, 3, 0]
[0, 1, 2, 3]

[Swift]UIScrollViewのページ番号

UIScrollViewのページ番号
・paging Enabledをチェックしておく。
・delegateを紐付けておく。

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
    let page:Int = Int( round(scrollView.contentOffset.x / scrollView.frame.size.width) )
    print("------begin \(page)")//スクロール開始時ページ番号
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let page:Int = Int( scrollView.contentOffset.x / scrollView.frame.size.width )
    print("------end \(page)")//スクロール完了時ページ番号
}
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    let to:Int = Int( targetContentOffset.memory.x / scrollView.frame.size.width )//スクロール完了時ページ番号
    print("\n------endDrag \(to)")
}

[Xcode]アップデートしたらコミット出来ない

XCodeをアップデートしたらコミット出来ず、下記メッセージが出る。

Couldn’t communicate with a helper application.

下記をターミナルで実行したら直った。

xcrun git config --global user.email sample@mail.com
xcrun git config --global user.name samplename

[Swift]singleton@Swift3

singleton@Swift3

class クラス名 {
    class var sharedInstance :クラス名 {
        struct Static {
            static let instance = クラス名()
        }
        return Static.instance
    }
    var 変数名:Int=0
}

使用時

let v:クラス名 = クラス名.sharedInstance

[Swift]UITextField Extension #1

UITextField Extension #1

func setTintColorToClearButton(){
    let anyobj:AnyObject? = self.valueForKey("_clearButton")
    if let anyobj:AnyObject = anyobj {
        if anyobj.isMemberOfClass(UIButton) {
            let button:UIButton = anyobj as UIButton
            if let image:UIImage = button.imageForState(.Highlighted) {
                let paintedImage:UIImage = image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
                button.setImage(paintedImage, forState: .Normal)
                button.setImage(paintedImage, forState: .Highlighted)
            }
        }
    }
}

[Swift]UIButton Extension #1

UIButton Extension #1

var titleText:String{
    get{
        if let titleLabel:UILabel = self.titleLabel {
            if let titleLabelText:String = titleLabel.text {
                return titleLabelText
            }
        }
        return ""
    }
    set{
        self.setTitle(newValue, forState: .Normal)
        self.setTitle(newValue, forState: .Highlighted)
        self.setTitle(newValue, forState: .Selected)
        self.setTitle(newValue, forState: .Reserved)
        self.setTitle(newValue, forState: .Disabled)
    }
}