[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)
    }
}

[Swfit]UIView Extension #5

UIView Extension #5

func fade(fadein:Bool, duration:CGFloat=0.3, operateHidden:Bool=true){
    if operateHidden {
        self.hidden = false
    }
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.animateWithDuration(NSTimeInterval(duration), delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveLinear,
        animations: { () -> Void in
            self.alpha = (fadein) ? 1.0 : 0.0
        },
        completion: { (finish:Bool) -> Void in
            if operateHidden {
                self.hidden = !fadein
            }
        }
    )
}

[javascript]isArray

isArray

var isArray = function(target){
	return getTypeString(target) == 'array';
};
var getTypeString = function(target){
	if(target == null){
		return "null";
	}
	var toString = {}.toString;
	return (typeof target === "object" || typeof target === "function") ? toString.call(target).split(' ').pop().toLowerCase().replace(/\]/g,'') : typeof obj;
};