第十一天:SwiftLoveTweet

Posted CHM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十一天:SwiftLoveTweet相关的知识,希望对你有一定的参考价值。

 

参考链接:https://github.com/soapyigu/Swift-30-Projects

 

 1 import UIKit
 2 import Social
 3 
 4 enum Gender: Int {
 5     case Male = 0, Female
 6 }
 7 
 8 extension Selector {
 9     static let endEditing = #selector(UIView.endEditing(_:))
10 }
11 
12 extension UIViewController {
13     func showAlert(title: String, message: String, buttonTitle: String) {
14         let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
15         alert.addAction(UIAlertAction.init(title: buttonTitle, style: .default, handler: nil))
16         present(alert, animated: true, completion: nil)
17     }
18 }

 

 1 import Foundation
 2 
 3 struct Tweet {
 4     var gender: Gender
 5     var name: String
 6     var age: Int
 7     var work: String
 8     var salary: String
 9     var isStraight: Bool
10     
11     var interestedGender: String {
12         switch gender {
13         case .Female:
14             return isStraight ? "Men" : "Women"
15         case .Male:
16             return isStraight ? "Women" : "Men"
17         }
18     }
19     
20     var info: String {
21         return "Hi, I am \(name). As a \(age)-year-old \(work) earning \(salary)/year, I am interested in \(interestedGender). Feel free to contact me!"
22     }
23 }

 

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4     
 5     var tweet: Tweet?
 6     
 7     // MARK: Outlets
 8     @IBOutlet weak var salaryLabel: UILabel!
 9     @IBOutlet weak var straightSwitch: UISwitch!
10     @IBOutlet weak var nameTextField: UITextField!
11     @IBOutlet weak var workTextField: UITextField!
12     @IBOutlet weak var birthdayPicker: UIDatePicker!
13     @IBOutlet weak var genderSeg: UISegmentedControl!
14 
15     override func viewDidLoad() {
16         super.viewDidLoad()
17         // Do any additional setup after loading the view, typically from a nib.
18         
19         birthdayPicker.maximumDate = Date()
20         
21         // dismiss keyboard
22         self.view.addGestureRecognizer(UITapGestureRecognizer.init(target: self.view, action: Selector.endEditing))
23         
24         nameTextField.delegate = self
25         workTextField.delegate = self
26     }
27     
28     @IBAction func salarySliderValueDidChange(_ sendr: UISlider) {
29         salaryLabel.text = "$\((Int)(sendr.value))k"
30     }
31     
32     @IBAction func tweetButtonDidTap(_ sender: UIButton) {
33         func getLabelsInfo() -> (name: String?, work: String?, salary: String?) {
34             guard let name = nameTextField.text,
35                 let work = workTextField.text,
36                 let salary = salaryLabel.text
37                 else {
38                     return (nil, nil, nil)
39             }
40             
41             if name.isEmpty || work.isEmpty || salary.isEmpty {
42                 return (nil, nil, nil)
43             }
44             
45             return (name, work, salary)
46         }
47         
48         func getAge() -> Int? {
49             let ageComponents = Calendar.current.dateComponents([.year], from: birthdayPicker.date, to: Date())
50             return ageComponents.year
51         }
52         
53         // get tweet info
54         let labelsInfo = getLabelsInfo()
55         
56         if let name = labelsInfo.name, let work = labelsInfo.work, let salary = labelsInfo.salary, let age = getAge() {
57             tweet = Tweet(gender: Gender(rawValue: genderSeg.selectedSegmentIndex)!, name: name, age: age, work: work, salary: salary, isStraight: straightSwitch.isOn)
58         } else {
59             tweet = nil
60         }
61         
62         switch tweet {
63         case .some(let tweet):
64             showAlert(title: "Love Tweet", message: tweet.info, buttonTitle: "OK")
65         case .none:
66             showAlert(title: "Info miss or invalid", message: "Please fill out correct personal info", buttonTitle: "OK")
67         }
68     }
69     
70     override func didReceiveMemoryWarning() {
71         super.didReceiveMemoryWarning()
72         // Dispose of any resources that can be recreated.
73     }
74 
75 }
76 
77 extension ViewController: UITextFieldDelegate {
78     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
79         textField.resignFirstResponder()
80         return true
81     }
82 }

 

以上是关于第十一天:SwiftLoveTweet的主要内容,如果未能解决你的问题,请参考以下文章

前端开发常用代码片段(中篇)

onBackPressed 上的 Android 片段 NullPointerException

寒假每一天

Vue实战-第一天开发工具的使用

代码第一天

课程设计第二十一天,09.08