I am using xcode 8 swift 3. I have a mini App where I time myself how long I can answer the given questions, I have made it pass my time to the next VC and actually save it. I am wondering how do I save all the times I get in the App.
Button to save score
#IBAction func saveScore(_ sender: Any) {
label.text = label.text
UserDefaults.standard.set(label.text, forKey: "score")
}
User Defaults part
override func viewDidAppear(_ animated: Bool) {
if let x = UserDefaults.standard.object(forKey: "score") as? String {
label.text = x
}
}
You can store Array of Dictionary to UserDefaults which contains more values
let dict = ["score": "","userID":"1"]
let dict2 = ["score": "","userID":"2"]
let array = [dict,dict2]
UserDefaults.standard.set(array, forKey: "scoreCard")
And fetch like
if let x = UserDefaults.standard.array(forKey: "scoreCard") as? [[String:Any]] {
for dict in x {
//DO something
yourLabel.text = dict["score"] as! String
}
}
Hope it is helpful to you
Related
I tried to solve this problem for a couple of days before submitting the app, so here it goes. I am developing an app that uses UserDefaults to save a string from a different view controller with a press of a button and to transfer it a table view in the next view controller. However, for some unknown reason, the app works perfectly on iPhone X (and iPhone XR), but it does not save the string and react to a press of a button whenever I run the app on a different iPhone model. Here is my code:
//FirstViewController
#IBAction func buttonIsPressed(_ sender: Any) {
if var items = UserDefaults.standard.object(forKey: "items") as? [String]{
var newitems = textField.text!.components(separatedBy: CharacterSet(charactersIn: ", []()\n.:"))
print(items)
if newitems.contains(""){
newitems.removeAll { $0 == ""}
items.append(contentsOf: newitems)
UserDefaults.standard.set(items, forKey: "items")
}else{
let newitems = textField.text!.components(separatedBy: CharacterSet(charactersIn: ", []()\n.:"))
UserDefaults.standard.set(newitems, forKey: "items")
}
textField.text = ""
}
}
//SecondViewController
var scannedText: String = "Detected text can be edited here." {
didSet {
textView.text = scannedText
let str = scannedText.uppercased()
let allergens = UserDefaults.standard.array(forKey: "items") as! [String]
let string = str.components(separatedBy: CharacterSet(charactersIn: ", []()\n.:"))
print(string)
for allergen in allergens{
if string.contains(String(Substring(allergen))) == true {
print("I found the string \(allergen)")
allegenLabel.text = "Not safe"
allegenLabel.alpha = 1 //Make the label visible
allegenLabel.textColor = .red
// let attributedString = allergen.highlight([allergen], this: .red)
// textView.attributedText = attributedString
allergensFound.append(allergen)
print(allergensFound)
}
if string.contains(String(Substring(allergen))) == false {
allegenLabel.text = "Safe"
allegenLabel.alpha = 1 //Make the label visible
allegenLabel.textColor = UIColor.colorGreen
// table.reloadData()
}
}
}
}
Please ask me if you need further description in the code. Thanks!
Edit: To be more exact I am creating a code that will find a string in the array of strings that will then notify the user with AllegenLabel.text. The whole app is divided into different view controllers. The user inputs in a string using a text field in FirstViewController and then that string has to be found in a different array of strings in SecondViewController. For some reason UserDefaults successfully stores and retrieves the string on my phone but does not work on different devices. For example, whenever I click the save button in FirstViewController, a string has to be stored and shown in the console, but for some unknown reason it does not work. Sorry if I was unclear
I think you can try:
UserDefaults.standard.synchronize()
maybe...
I want the code to run once a day, but the way I want to accomplish this is by disabling the button after it is clicked and then reenabling when it has been more than 24 hours.
Would the code below be correct to just save the date the user pressed the button?
if distance < radius{
Total_Points += 10
pointsLabel.text = "Total Points: \(Total_Points)"
getPointsOutlet.isEnabled = false
let clickdate = UserDefaults.standard
if var timeList = clickdate.object(forKey: "timeList") as? [Date]{
timeList.append(Date())
clickdate.set(timeList, forKey: "timeList")
} else {
clickdate.set([Date()], forKey: "timeList")
}
clickdate.synchronize()
}
let PointsDefault = UserDefaults.standard
PointsDefault.setValue(Total_Points, forKey: "Total Points")
Your code
let clickdate = UserDefaults.standard
if var timeList = clickdate.object(forKey: "timeList") as? [Date]{
timeList.append(Date())
clickdate.set(timeList, forKey: "timeList")
} else {
clickdate.set([Date()], forKey: "timeList")
}
clickdate.synchronize()
Is fine for adding a date to an array of saved dates. You could pull that out into a separate function:
func addDateToDefaults(date: Date? = nil) {
let defaults = UserDefaults.standard
if date = nil {
date = Date()
}
if var timeList = defaults.object(forKey: "timeList") as? [Date]{
timeList.append(date)
defaults.set(timeList, forKey: "timeList")
} else {
defaults.set([date!], forKey: "timeList")
}
}
Then you could call that from a button action:
#IBAction func buttonClicked(_ sender: UIButton) {
addDateToDefaults()
//The rest of your button action code goes here
}
With the function addDateToDefaults() above, you can pass in a specific date, or leave off the date parameter and it will append the current date to your array of dates.
You can achieve in this way when you tapped on button save the date value using Userdefaults and then inside your ViewDidAppear, ViewDidload and UIApplicationWillEnterForeground notification method put the check to get dateValue from user defaults and then take the difference of current date and last stored date and accordingly enabled your button.
lazy var userDefaults: UserDefaults = {
return UserDefaults.standard
}()
func ViewDidLoad() {
UserDefaults.set(Date(), forKey:"date")
userDefaults.synchronize()
}
func someMethodWhereYouWantToGetValue() {
guard let value = userDefaults.object(forKey: "date") as? Date else
{return}
print(value)
}
I want the user to add Friends. However whenever I click on addFriend button I get a breakpoint with an thread 1. There is no further error information rather than 11db.
I have found out by now that the NSDictionary of "otherUser" has the value of none. Thats the reason everything is not working. However I have no clue why that is. Out of the same NSDictionary , I get the username and the profile Pic. Why is it nil then?
var otherUser: NSDictionary?
override func viewDidLoad() {
super.viewDidLoad()
databaseRef.child("user").child(self.otherUser?["uid"] as! String).observe(.value, with: { (snapshot) in
let uid = self.otherUser?["uid"] as! String
self.otherUser = snapshot.value as? NSDictionary
self.otherUser?.setValue(uid, forKey: "uid")
}) { (error) in
print(error.localizedDescription)
}
}
#IBAction func addFriend(_ sender: Any) {
if self.befreunden.titleLabel?.text == "Als Freund hinzufügen" {
let friendRef = "befreundet/\(self.loggedinUSerData?["uid"] as! String)/\(self.otherUser?["uid"] as! String)"
let BefreundenData = ["username":self.otherUser?["username"] as? String,
"ProfilePic":self.otherUser?["urltoImage"] as! String!]
let childUpdats = [friendRef:BefreundenData]
databaseRef.updateChildValues(childUpdats)
}else {
FreundeAnzahl = self.otherUser?["AnzahlFreunde"] as! Int + 1
}
}
Edit:
I've just found out, that the NSDictionary of otherUser has all the values needed inside the viewdidLoad. But as soon as I call the uid of the otherUser in the addFriend function it gives back a value of nil.
Why is it happening?
I fixed the issue by simple giving the uid of the otheruser NSDictionary to a new variable which I created in the viewdidLoad.
This way I do not face any issues regarding unwrapping optionals later on.
I am on a Timer project that takes a value from a variable through unwind segue to pass it to another view controller, then append its value to an Array that should be used to insert a row with the task name whenever the user pressed save button I need to save the result permanently, but I am confused which data should I save the value of the variable itself, the row, or the array?
var taskList = [String]()
#IBAction func saveToTaskList (segue: UIStoryboardSegue) {
let newItemViewController = segue.source as! AddTaskTableViewController
let name = newItemViewController.itemName
let date = newItemViewController.date
print("itemName passed is: \(name)")
if name == "" {
}
else {
print(date)
taskList.append(name!)
let indexToInsert = taskList.count == 0 ? 0 : taskList.count - 1
let indexPath = IndexPath(row: indexToInsert, section: 0)
tableView.insertRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
add task view controller
Timer / Task List
Ok, thanks to the hint of Matt Le Fleur
I solved the issue by using objects as below:
#IBAction func saveToTaskList (segue: UIStoryboardSegue) {
let newItemViewController = segue.source as! AddTaskTableViewController
var name = newItemViewController.itemName
let date = newItemViewController.date
let itemsObject = UserDefaults.standard.object(forKey: "items")
var items:[String]
if let tempItems = itemsObject as? [String] {
items = tempItems
items.append(name!)
print(items)
} else {
items = [name!]
}
UserDefaults.standard.set(items, forKey: "items")
name = ""
}
Then added a ViewDidAppear as below:
override func viewDidAppear(_ animated: Bool) {
let itemsObject = UserDefaults.standard.object(forKey: "items")
if let tempItems = itemsObject as? [String] {
items = tempItems
}
tableView.reloadData()
}
I'm trying to get access to the values stored in firebase dashboard to use them in different functions and methods in the class.
I used this method in this question
I have tried to print their values, the whole app has crashed and it gave me that their nil!
They are not nil actually!
I used a similar method in viewDidLoad and I could retrieve the values to labels!
let refer = FIRDatabase.database().reference().child("UserDevices")
var globalEmail : String!
var globalPhone : String!
var globalImageUrl: String!
override func viewWillAppear(_ animated : Bool){
super.viewWillAppear(animated)
retrieveUserData{(email,phone,ImageUrl) in
self.globalEmail = email
self.globalPhone = phone
self.globalImageUrl = ImageUrl
}
}
func retrieveUserData(_ completionBlock : #escaping ((_ email : String?, _ phone : String?, _ ImageUrl: String?)->Void)){
refer.child(byAppendingPath: self.strUserid as String).observe(.value , with: {snapshot in
if let userDict = snapshot.value as? [String:AnyObject] {
completionBlock(userDict["email"] as! String, userDict["phone"] as! String, userDict["ImageUrl"] as! String)
}
})
}
var strUserid : NSString!
override func viewDidLoad() {
super.viewDidLoad()
print(globalEmail)
print(globalImageUrl)
print(globalPhone)
self.navigationController?.navigationBar.tintColor = UIColor.white
print("idis \(self.strUserid)")
let ref = FIRDatabase.database().reference().child("UserDevices")
self.navigationController?.navigationBar.tintColor = UIColor.white
ref.child(byAppendingPath: self.strUserid as String).observe(.value, with: { snapshot in
if let dict = snapshot.value as? NSMutableDictionary{
print("dict is \(dict)")
if let Provider = dict["name"] as? String
{
self.DeviceDetailsProvider.text = Provider
// self.navigationItem.title = Provider
}
if let name = dict["DeviceName"] as? String
{
self.DeviceDetailsName.text = name
self.navigationItem.title = name
}
if let ShortDescription = dict["Description"] as? String
{
self.DeviceDetailsDescription.text = ShortDescription
}
if let City = dict["city"] as? String
{
self.DeviceDetailsCity.text = City
}
}
})
self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl)
}
Why I'm getting a crash here!
Change ref.child(byAppendingPath: self.strUserid as String)
To:-
ref.child(self.strUserid)
Also remove let refer = FIRDatabase.database().reference().child("UserDevices").
You can not initialise your reference globally outside a scope because you don't know in which order your classes are being initialised, and probable scenario is that your FIRDatabase hasn't even been initialised yet when you try to initialise let refer.
Instead of refer in retrieveUserData use
FIRDatabase.database().reference().child("UserDevices")
You see in a viewController's LIFE CYCLE, viewdidLoad is called before than viewWillAppear:
So what you need is:-
override func viewDidLoad() {
super.viewDidLoad()
..
retrieveUserData{(email,phone,ImageUrl) in
self.globalEmail = email
self.globalPhone = phone
self.globalImageUrl = ImageUrl
self.DeviceDetailsImageView.downloadedFrom(link: globalImageUrl)
// .. Do your stuff...
}
}
Read: Viewcontroller's Lifecycle