Reading FireDatabase in Swift, while calling function [duplicate] - swift

This question already has answers here:
Finish all asynchronous requests before loading data?
(6 answers)
Make code with Firebase asynchronous
(1 answer)
Swift Function returning a value from asynchronous firebase call
(2 answers)
Wait for Firebase to load before returning from a function
(2 answers)
Firebase with Swift 3 counting the number of children
(3 answers)
Closed 2 years ago.
func callData() -> String? {
var name: String = ""
var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("reyddatbase").child("MovieData").child("InitialMovieData").child("Ariel").child("movie_name").observeSingleEvent(of: .value, with: { (snapshot) in
name = ((snapshot.value) as! NSString) as String;
print("\(name)") // prints what I need
})
print("\(name)") // prints nothing??
return "\(name)" // returns nothing?
}
I am attempting to read data from a database based in Firebase.
I would like to simply display the text on screen which I can do from content view.
However the print statement inside the ref.child shows my title in console, while my print statement outside of it remains an empty string.
I am not sure why the different print statements do not yield the same result.
I am very new to swift and have taught myself to this point.
I presume there is a simple solution to my problem. Thank you in advance for the help!

Related

Can we use get instead of function in swift? [duplicate]

This question already has answers here:
Computed read-only property vs function in Swift
(11 answers)
Closed 1 year ago.
I have created getName1 & getName2() as below
var myName = "Jhon"
var salt = " Salt"
var getName1:String {
get {
return myName + salt
}
}
func getName2() ->String {
return myName + salt
}
print("getName1\(getName1)")
print("getName2\(getName2())")
So instead of getName2() can I use getName1 directly does that make any difference ?
There is no difference but getName1 is a computed property while getName2 is a function , in this case you better use getName1 as a function is expected to do some block code or IMO not just a one line

How to fix 'characters' is unavailable: Please use String directly in swift 5 [duplicate]

This question already has answers here:
How to filter characters from a string in Swift 4
(2 answers)
Closed 3 years ago.
i've some code to filtering number inside variable.
Here's the code:
var numbers = String(anotherNumbers.characters.filter { "01234567890.".characters.contains($0) })
In the swift 3, this code working correctly. But in the Swift 5, i get an error 'characters' is unavailable: Please use String directly
How to fix this error?
Thankyou.
You can remove characters to use String directly.
For example
var anotherNumbers = "0123456789"
var numbers = String(anotherNumbers.filter { "01234567890.".contains($0) })
returns "0123456789"

Print contents of array with custom class swift [duplicate]

This question already has answers here:
Overriding description method in NSObject on swift
(1 answer)
How can I change the textual representation displayed for a type in Swift?
(7 answers)
Closed 5 years ago.
I have an array of type custom class as seen below. It has strings, ints, and double values.
class TrendData: NSObject {
var earlyTime = Date()
var recentTime = Date()
var earlyTimePrice = Double()
var recentTimePrice = Double()
}
I have an array as follows
let dataArray = [TrendData]()
I have filled in 2 values into the dataArray.
Right now when I use a print command as follows
print ("Label: \(dataArray)")
it prints this Label: [<App.TrendData: 0x600000472440>] ** [<App.TrendData: 0x600000472440>]
This is the correct behavior but I want to see all the values within each of these elements in the array. What is the best way to do that? I don't want to explicitly list out each element or even put it into a loop if I can avoid it. I am able to do it, it is just really messy right now. Is there any function or command that does this automatically?

How can I get the string value in Optional() in swift, which is followed by nil? [duplicate]

This question already has answers here:
What is an optional value in Swift?
(15 answers)
Closed 5 years ago.
How can I get the string value in Optional() in swift, which is followed by nil?any other proper way to use this evaluateJavaScript() and get the callback value?
webView.evaluateJavaScript("(function() { return 'this'; })();",
completionHandler: {
result in
//Handle your variable
print(result)//(Optional(this), nil)
})
Use if let to parse the optional value:
if let value = result {
print(value)
}

Multiple condition for Firebase query [duplicate]

This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 6 years ago.
I have this code for read data from Firebase with condition, but I need multiple condition. I need one more condition for read. I don't know how to do it, I founded it isn't supported, but I need it. Can me help anyone how to do it?
ref?.queryOrdered(byChild: "project").queryEqual(toValue: "inbox").observe(.value, with: { (snapshot:FIRDataSnapshot) in
var newTasks = [Task]()
for sweet in snapshot.children {
let sweetObject = Task(snapshot: sweet as! FIRDataSnapshot)
newSweets.append(sweetObject)
}
self.tasks = newTasks
self.tableView.reloadData()
}) { (error:Error) -> Void in
print(error.localizedDescription)
}
Data in firebase:
This is not possible. You will either have to
1. filter most on the server, do the rest on the client
or
2. add a property that combines the values that you want to filter on
or
3. create a custom index programmatically
All these quotes come from this excellent answer over here at this question.
Hope this helps.