override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "section-\(section)" }
エミュレーターに反映される時とされない時があるな。。
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return "section-\(section)" }
エミュレーターに反映される時とされない時があるな。。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MemoTableViewCell", for: indexPath) // Configure the cell... cell.textLabel?.text = self.memos[indexPath.section][indexPath.row] return cell }
import UIKit class MemoTableViewController: UITableViewController { var memos = ["blue", "red", "pink"] override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return self.memos.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MemoTableViewCell", for: indexPath) // Configure the cell... cell.textLabel?.text = self.memos[indexPath.row] return cell } /* // Override to support conditional editing of the table view. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } */ /* // Override to support editing the table view. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // Delete the row from the data source tableView.deleteRows(at: [indexPath], with: .fade) } else if editingStyle == .insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the item to be re-orderable. return true } */ /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
なるほど
import UIKit class ViewController: UIViewController { @IBOutlet weak var timerLabel: UILabel! var startTime: TimeInterval? = nil var timer = Timer() var elapsedTime: Double = 0.0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } setButtonEnabled(start: true, stop: flase, reset: false){ } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setButtonEnabled(start: Bool, stop: Bool, reset: Bool){ self.startButton.isEnabled = start self.stopButton.isEnabled = stop self.resetButton.isEnabled = reset } @objc func update(){ // print(Date.timeIntervalSinceReferenceDate) if let startTime = self.startTime { let t: Double = Date.timeIntervalSinceReferenceDate - startTime + self.elapsedTime print(t) let min = Int(t / 60) let sec = Int(t) % 60 let msec = Int((t - Double(sec)) * 100.0) self.timerLabel.text = String(format: "%02d:%02d:%02d", min, sec, msec) } } @IBAction func startTimer(_ sender: Any) { self.startTime = Date.timeIntervalSinceReferenceDate self.timer = Timer.scheduledTimer( timeInterval: 0.01, target: self, selector: #selector(self.update), userInfo: nil, repeats: true) } @IBAction func stopTimer(_ sender: Any) { if let startTime = self.startTime { self.elapsedTime += Date.timeIntervalSinceReferenceDate - startTime } self.timer.invalidate() } @IBAction func resetTimer(_ sender: Any) { self.startTime = nil self.timerLabel.text = "00:00:00" self.elapsedTime = 0.0 } }
[
はて?
いらない接続を解除すると消えるようだが、消えんぞ。
#selector(self.hoge)がエラーになる。
func update()を@objc func update()とするとエラーが解消してbuild出来る。
@objc func update(){ print(Date.timeIntervalSinceReferenceDate) } @IBAction func startTimer(_ sender: Any) { Timer.scheduledTimer( timeInterval: 0.01, target: self, selector: #selector(self.update), userInfo: nil, repeats: true) } @IBAction func stopTimer(_ sender: Any) { } @IBAction func resetTimer(_ sender: Any) { }
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { if identifier == "showResult" { guard self.nameText.text != "" else { let alertController = UIAlertController(title: "Error", message: "Please enter your name", preferredStye: alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) self.present(alertController, animated: true, completion: nil) return false } return true } return true }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard let identifier = segue.identifier else { return } if identifier == "showResult"{ let resultVC = segue.destination as! ResultViewController resultVC.myName = self.nameText.text! } }
iphone8のemulatorの立ちあがりが異常に遅いんだが。。。
あれ?
失敗してるーーーーーーーーーーーーーー
あ、いいのか
@IBAction func getOmikuji(sender: AnyObject) { // 0 - n // arc4random_uniform(n + 1) let random = arc4random_uniform(10) self.myLabel.text = String(random) }
おーおーお
@IBOutlet weak var myLabel: UILabel! @IBAction func getOmikuji(sender: AnyObject) { // 0 - n // arc4random_uniform(n + 1) let results = ["大吉", "中吉","吉", "大凶"] let random = arc4random_uniform(UInt32(results.count)) self.myLabel.text = results[Int(random)] } override func viewDidLoad() { super.viewDidLoad() // myLabel.layer.borderColor = UIColor.orange.cgColor // myLabel.layer.borderWidth = 5 // myLabel.layer.cornerRadius = 50 myLabel.layer.masksToBounds = true myLabel.layer.cornerRadius = myLabel.bounds.width / 2 // Do any additional setup after loading the view, typically from a nib. }
generic
// Generics func getThree<T>(x: T){ print(x) print(x) print(x) } getThree(x: 5) getThree(x: "Hello") getThree(x: 2.3)
guard
// func sayHi(_ msg: String?){ // if let s = msg{ // print(s) // } else { // print("value is not set!") // } // } // sayHi(nil) // sayHi("hello") func sayHi(_ msg: String?){ guard let s = msg else{ print("value not set!") return } print(s) } sayHi(nil) sayHi("hello")
例外処理
// 例外処理 enum LoginError: Error{ case emptyName case shortName } class User { let name: String init(_ name: String){ self.name = name } func login() throws{ guard name != "" else { throw LoginError.emptyName } guard name.characters.count > 5 else { throw LoginError.shortName } print("login success") } } let tom = User("tom") do { try tom.login() } catch LoginError.emptyName { print("please enter your name") } catch LoginError.shortName{ print("too short") }
optional
// class User { // var name: String = "" // } // let user: User // user = User() // user.name = "him" // let s = user.name.uppercased() // print(s) class User { var name: String = "" } let user: User? user = User() user?.name = "him" if let s = user?.name?.uppercased(){ print(s) }
// Implicity Unwrapped Optional // var msg: String? var msg: String! msg = "hello" // if msg != nil { // print(msg!) // } print(msg)