Category Archives: Swift

プロパティ変数の取得方法(Ruby, Swift比較)

Swift

class Human {
    var name: String!

    init(name: String) {
            self.name = name
    }
}

let human = Human(name: "test")

// 以下のようにプロパティの取得が可能
print(human.name)

Ruby

class User
  def initialize(input)
    @name = input
  end

  // アクセサメソッドの設定が必要
  attr_accessor :name
end

user = User.new("ttt")
user.name

システム音量の変更を検知

override func viewDidLoad() {
    super.viewDidLoad()

    listenVolumeButton()
}

func listenVolumeButton(){

    // audiosession作成
    let audioSession = AVAudioSession()
    try! audioSession.setActive(true)

    // observer追加
    audioSession.addObserver(self, forKeyPath: "outputVolume",
        options: NSKeyValueObservingOptions.New, context: nil)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == "outputVolume"{

        print(change)
        pushCount = pushCount + 1
    }
}


Dic for文

## Code

var dic = Dictionary<String,Array<Int>>()
dic["1"] = [1,2,3]
dic["2"] = [4,5,6]

for (key,element) in dic {
    print("key:\(key) element: \(element)")
}

戻り値

key:2 element: [4, 5, 6]
key:1 element: [1, 2, 3]

Init カスタマイズ

class Human {
    var name: String

    init(name:String) {
        self.name = name
    }
}

class Human2:Human {
    let age: Int

    init(name: String, age: Int) {
        self.age = age
        super.init(name: name)
    }
}


let human2 = Human2(name: "yohei", age: 35)
print("name:\(human2.name) age:\(human2.age)")

SubViewのプロパティ変更

//
//  ViewController.swift
//  reload
//
//  Created by YoheiMiyamoto on 12/29/15.
//  Copyright © 2015 YoheiMiyamoto. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var cube:UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // cube生成
        self.cube = UIView()
        cube.backgroundColor = UIColor.redColor()
        cube.frame = CGRect(x: 500, y: 50, width: 50, height: 50)

        // cubeの中にview生成
        for i in 1...2{
            let cube2 = UIView()
            cube2.backgroundColor = UIColor.greenColor()
            cube2.frame = CGRect(x: 0+i*12, y: 0, width: 10, height: 10)
            cube.addSubview(cube2)
        }

        self.view.addSubview(cube)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func button(sender: UIButton) {
        // cubeの中にsubviewが2つあることを確認できる
        print(self.cube.subviews.count)

        // subviewのプロパティを変更
        for obj in self.cube.subviews {
            obj.backgroundColor = UIColor.blueColor()
        }

    }

}

Segue 遷移先にプロパティを渡す(Navigation Controller)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

  // navigation controllerを取得
  let destinationNavigationController = segue.destinationViewController as! UINavigationController

  // topViewControllerでnavigationControllerのさらに先のcontrollerを取得できる
  let targetController = destinationNavigationController.topViewController as! InputViewController

  // プロパティを設定
  targetController.title = "test"
}