Issue with Xmartlabs Eureka form builder on iOS in Swift - ios

I am having a bit of trouble with a part of the Eureka framework for Swift in iOS. It works fine, except for when I try to clear out a form programatically which is already on the screen all the fields seem to clear out fine, except for the DecimalRow type field.
(form.rowBy(tag: "name") as? TextRow)?.value = ""
**(form.rowBy(tag: "amount") as? DecimalRow)?.value = Double(0.0)**
(form.rowBy(tag: "date") as? DateRow)?.value = Date()
(form.rowBy(tag: "reimb") as? SwitchRow)?.value = false
The DecimalRow type field stays whatever value was in it and does not respond to the line above in bold (asterisk-ed).
Any insight appreciated. Many thanks.

Have you reloaded the row?
This worked fine on my code.
(self.form.rowBy(tag: "user_weight") as? DecimalRow)?.value = Double(0.0)
(self.form.rowBy(tag: "user_weight")?.reload()
Where
<<< DecimalRow("user_weight")
And if I run
self.form.values() I get:
"user_weight": Optional(0.0)
So its cleared
You can also clear the form in this way
self.form.setValues(["user_weight": 0])
An example made by them
form.setValues(["IntRowTag": 8, "TextRowTag": "Hello world!", "PushRowTag": Company(name:"Xmartlabs")])
https://github.com/xmartlabs/Eureka#how-to-set-the-form-values-using-a-dictionary

Related

UIImage not being read properly after entering foreground

I am in a real rough situation trying to squish this bug.
In a perfect world, I can press a "success" button, it plays a sound and vibrates, and changes the image in a UIImageView. This works perfectly until I press the home button and reopen the app. This glitch ALWAYS occurs.
Here is the relevant code:
#IBAction func Success(_ sender: Any) {
var boxNum = 1
generator.impactOccurred()
if "" == defaults.value(forKey: "frequency") as? String{
//One of 2 frequencies. It doesn't make a difference, however because the glitch always happens.
playVictorySound()
let formatter = DateFormatter()
for box in Boxes{
if box.image == UIImage(named:"blank.png"){
if "" == defaults.value(forKey: mode) as? String{
formatter.dateFormat = "dd.MM.yyyy"
let activeDate = Date()
defaults.set(Date(), forKey: "dateLast")
print(activeDate)
}
box.image = UIImage(named:"Arm.png")
defaults.set("Active", forKey: "boxState" + String(boxNum))
print("Active set for" + " boxState" + String(boxNum))
if "" == defaults.value(forKey: "mode") as? String{
successButton.isHidden = true
failButton.isHidden = true
}
break
}
else{
boxNum = boxNum + 1
print ("Added 1 to" + String(boxNum))
}
}
}
NOTE: This section has the user defaults that are set during the button press.
When the app is re-opened, the success button plays a sound (which is desired), and vibrates the phone (also desired), but the images do not update. In fact, the console prints a series of the else case.
Added 1 to38
Added 1 to39
Added 1 to40
Etc etc.
When any other modes or frequencies are set, the glitch also occurs. I am focused on fixing this case because I think I will be able to fix the others as well.
Is there some underlying reason causing this? I believe it may be because either something isn't being kept in memory, but I cannot figure out why. Thank you very much.
EDIT: Ok, I set the "blank" image to a selfie, and the app loads up "blank.png" for every square. There seems to be something wrong with another part of the code above.
I added print statements to help a bit and the if statement is the part that is failing, however, the ImageViews all have blank.png set as their image.
I fixed my issue using a workaround. I stopped checking the content of the UIImageView (Which for some reason didn't work), and instead used UserDefaults to see what the image should be.
I set a value in UserDefaults for all the possible images, and then check what the value of the default is.

iOS Swift: retrieve an entry from the database for the user currently logged in

How do I retrieve a value (other than username and user id, which seem easier to get) for the current user from the database.
Ironically, I can set the value as follows and that works just fine:
let databaseRef = FIRDatabase.database().reference()
userID = (FIRAuth.auth()?.currentUser?.uid)! as String
databaseRef.child("users").child(userID!).child("TermCond").setValue("Yes")
But for the life of me I cannot work out what to put instead of setValue if I simply want to retrieve the current TermCond value. I thought just using value as for example in
let DesiredValue = databaseRef.child("users").child(userID!).child("TermCond").value as? String
Would suffice, but nothing works. I am confused why retrieving the value should be more difficult than setting it.
To "read" a value from Firebase, you need to add a reference listener that gets called every time that value changes.
In your case, that could be something like:
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("users").child(userID!).child("TermCond").observe(FIRDataEventType.value, with: { (snapshot) in
let desiredValue = snapshot.value as? String
})
This block of code will get triggered every time your value changes. If you only want to read it once, you can use observeSingleEvent:of:with instead of observe:with.
This is as described in the Firebase documentation: https://firebase.google.com/docs/database/ios/read-and-write
I recommend you read their entire Documentation to get an idea of how Firebase works, as it is very different from traditional databases.
I can also recommend the following tutorial if you'd like to learn a bit more about the Firebase Database and how it works: https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2
I've solved this now (based on Aleksander's reply). The way I did it is as follows.
databaseRef.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.desiredValue = value?["TermCond"] as? String ?? ""
self.LabelToShow.text = self.desiredValue!
}) { (error) in
print(error.localizedDescription)
}
This works absolutely fine and shows the value of TermCond in the LabelToShow on my iOS screen.

How to detect First launch of app on every new version upgrade?

I have a requirement of detecting the first launch of app after the user upgrades the app to a newer version. I need to perform certain task only on first launch of app after the user upgrades the app to a new version. Many links available online but none answer clearly to my query. How to achieve this in Swift 2 , iOS 9.
Most of the answers available says to maintain a key in NSUserDefaults and set its value to false and after first launch make it true. But the problem is after I upgrade my app the variable still will be true and thus my scenario fails on app upgrade. Any help would be much appreciated. Thanks!
Try this:
let existingVersion = NSUserDefaults.standardUserDefaults().objectForKey("CurrentVersionNumber") as? String
let appVersionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
if existingVersion != appVersionNumber {
NSUserDefaults.standardUserDefaults().setObject(appVersionNumber, forKey: "CurrentVersionNumber")
NSUserDefaults.standardUserDefaults().synchronize()
//You can handle your code here
}
updating Yogesh's perfect, yet simple solution to swift 4
let existingVersion = UserDefaults.standard.object(forKey: "CurrentVersionNumber") as? String
let appVersionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
if existingVersion != appVersionNumber {
print("existingVersion = \(String(describing: existingVersion))")
UserDefaults.standard.set(appVersionNumber, forKey: "CurrentVersionNumber")
// run code here.
}

AEXMLDocument loadXMLData() not working in Swift

I'm working with AEXML to write and read xml documents in Swift. I have the writing working no problem. And I have everything setup for the reading, but I can't seem to turn the saved text xml into the document object. It only ever gets the first element and none of the children. I've tried removing all the lines and spaces but still nothing. The content is reading into the String just fine and I've tried converting the data back to a string and it isn't getting messed up in conversion. Is this even possible with AEXML or am I just doing it wrong?
let doc = AEXMLDocument()
let content = try String(contentsOf:NSURL(string:file) as! URL)
let data = content.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let xml = NSString(data:data, encoding:String.Encoding.utf8.rawValue)
try doc.loadXMLData(data)
So I figured out that I was actually using an outdated version of AEXML which clearly wasn't working anymore. The updated code looks like this.
let content = try String(contentsOf:NSURL(string:file) as! URL)
let data = content.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let options = AEXMLOptions()
let doc = try AEXMLDocument(xml:data,options:options)

Swift 2 - Variable initiation failed (lldb)

I'm just starting developing apps with Swift 2.0, and as I'm working having problem with initiating a variable with the value of a text field in my app.
this is the function I'm having problems with. It's called when a button under the text field is pressed as submit.
#IBAction func checkName(sender: AnyObject) {
guard let name : String = nameField.text else { print("Name not valid.");return}
let checker = NameChecker()
let result = checker.nameChecker(name)
print(result)
}
this only thing this code returns on the XCode shell is "lldb". I also tried to use the debugger to figure out where I was messing up but unfortunately I found out that was harder than expected and I wasn't able to.
If you have any idea why my code is only returning "lldb" I would really appreciate is you could let me know since I've been experiencing this error quite often lately.

Resources