Last active
August 16, 2018 02:24
-
-
Save izzuddin91/5d6b43ffbed19bc2eff0f9332878c467 to your computer and use it in GitHub Desktop.
Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //this is an snippet from previous code on UIALert (show up modal) | |
| @IBAction func setExpenditure(_ sender: Any) { | |
| let alert = UIAlertController(title: "Description", message: "Edit or cancel", preferredStyle: .alert) | |
| alert.addTextField { (textfield: UITextField) in | |
| } | |
| alert.addAction(UIAlertAction(title: "Set", style: .default, handler: { (alerted : UIAlertAction ) in | |
| if let textFields = alert.textFields{ | |
| let theTextFields = textFields as [UITextField] | |
| if let enteredText = theTextFields[0].text { | |
| let budgetAmount = Double(enteredText)! | |
| self.budgetAmount.text = "RM\(String(describing: budgetAmount))" | |
| self.globalVar.set(budgetAmount, forKey: "budgetAmount") | |
| } | |
| } | |
| })) | |
| alert.addAction(UIAlertAction(title: "Cancel", style: .destructive )) | |
| self.present(alert, animated: true, completion: nil) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // this is a callback for asynchronous call | |
| //this is for api. DO NOT DELETE! | |
| func fetchData(lastLogsId : Int, completion : @escaping ([Logs]) -> () ) { | |
| var logsArray = [Logs]() | |
| print("going to home Service -> fetch home feed") | |
| let postData = NSMutableData(data: "last_id=\(lastLogsId)".data(using: String.Encoding.utf8)!) | |
| let headers = ["content-type": "application/x-www-form-urlencoded"] | |
| let url = URL(string: "https://buku-lima.herokuapp.com/add_more_data") | |
| var request = URLRequest(url:url!) | |
| request.httpMethod = "POST" | |
| request.allHTTPHeaderFields = headers | |
| request.httpBody = postData as Data! | |
| let session = URLSession.shared | |
| session.dataTask(with: request) { (data, response, error) in | |
| //if there is no error | |
| if (error == nil){ | |
| let json = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String:AnyObject] | |
| let results = json!["return"] as! NSArray | |
| print(results) | |
| for result in results { | |
| let key = (result as! NSDictionary)["amount"] as! String | |
| let date = (result as! NSDictionary)["updated_at"] as? String | |
| let expensesType = (result as! NSDictionary)["expenses_type"] as? String | |
| let id = (result as! NSDictionary)["id"] as? Int | |
| let details = (result as! NSDictionary)["details"] as! String | |
| let log = Logs(float: Float(key)!, date: date!, expensesType: expensesType!, realDate: NSDate()) | |
| log.id = id! | |
| //untuk masukkan array of descriptions | |
| log.convertStringOfArrayIntoArray(stringArray: details) | |
| logsArray.append(log) | |
| } | |
| completion(logsArray) | |
| }else{ // if there is an error | |
| print("ERROR OCCURED: \(String(describing: error))") | |
| } | |
| }.resume() //session.dataTask | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let domain = Bundle.main.bundleIdentifier! | |
| UserDefaults.standard.removePersistentDomain(forName: domain) | |
| UserDefaults.standard.synchronize() | |
| print(Array(UserDefaults.standard.dictionaryRepresentation().keys).count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UIViewController { | |
| func hideKeyboardWhenTappedAround() { | |
| let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) | |
| tap.cancelsTouchesInView = false | |
| view.addGestureRecognizer(tap) | |
| } | |
| func dismissKeyboard() { | |
| // let point = CGPoint(x: 0,y :0) | |
| view.endEditing(true) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment