Swift OSX Firebase app cannot read data - swift

I am trying to create an OSX Swift app with Firebase. I can write to Firebase following the examples, but is not able to read the data back correctly. I have tried using the .Value listener and println(snapshot.value), it appears that the value returned is not correct.
let userRoot = "https://inkcloud.firebaseio.com/users/" + user
ref = Firebase(url:userRoot)
ref!.observeEventType(.ChildChanged, withBlock: { snapshot in
let result = snapshot.value.objectForKey("test") as? String
println("the result is \(result)")
}, withCancelBlock: { error in
println(error.description)
})
I get compiler error: '() -> AnyObject!' does not have a member named 'objectForKey'
Any help would be appreciated.
Ben

Found the soluiton, need to use functions calls instead of property.

Related

Contextual closure type firebase / swift

Somewhat confused by this error, very new to Firebase but I think this is more a swift programming issue on my part. A lot of the swift coding I'm trying to implement with firebase is new to me.
"Contextual closure type '(Result<StorageListResult, any Error>) -> Void' expects 1 argument, but 2 were used in closure body"
Starting with:
textRef.listAll { (result, error) in
for item in result!.items {
If I use 'item' within this closure alone such as:
let downloadTask = item.write(toFile: localURL) { url, error in
if let error = error {
print ("UNABLE TO DOWNLOAD FILES")
} else {
print("NEW FILE DOWNLOADED")
print(item)
}
}
Works perfectly.
But if I try and use item a 2nd time within the closure, such as:
let serverTimestamp = dateFormatter.string(from: itemTemp.getMetadata.updated())
I get the above error.
The aim of my code is to gain a list all items on my Firebase storage, then against each storage item firstly check its metadata updated date before deciding whether to download or not. However within the closure I can't seem to use item more than once. I either check its metadata or download...not both.
I've tried looking at closures, but struggling to see how I could potential expand the closure to incorporate what I want.
Any advice apprecaited

How do I load a URL using NSItemProvider's new Async loadItem method?

I am having difficulty grabbing a fileURL in a DropDelegate in SwiftUI, when dragging a file from my desktop into my app.
I am using the async loadItem call on NSItemProvider which returns an NSSecureCoding. I assumed that I could cast the result to an NSURL but it always fails.
let url = try await (urlProvider
.loadItem(forTypeIdentifier: UTType.url.identifier) as? NSURL
If I try to force the cast I get the following error:
Could not cast value of type 'Foundation.__NSSwiftData' to 'NSURL'.
I am successfully using the same call and casting to an NSDictionary elsewhere in my code.
This is in Xcode 13.4, Swift 5.
I have read access to user selected files in my sandbox capabilities.
Can anyone help point out what I am doing wrong?
Thank you.
I had to cast to Data and then construct the URL from that:
if let data = try await urlProvider
.loadItem(forTypeIdentifier: UTType.fileURL.identifier) as? Data {
url = URL(dataRepresentation: data, relativeTo: nil) }

swift - populating WKInterfaceTable

following this tutorial... here
for (index, labelText) in stringData.enumerate() {
let row = myTable.rowControllerAtIndex(index)
as! MyRowController
row.myLabel.setText(labelText)
getting an error message that says:
Value of type '[String]' has no member 'enumerate'
is there a Swift code update change?
That's a really old tutorial, the function you're looking for is called enumerated now.
for (index, labelText) in stringData.enumerated() {
...
}

Cannot convert value of type 'Data' to specified type 'Data'

I have the following code which takes a screenshot of the user's screen and allows them to send it to a friend as an attachment via SMS.
func sendSmsToFriend() {
UIGraphicsBeginImageContext(view.frame.size)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if MFMessageComposeViewController.canSendText() && MFMessageComposeViewController.canSendAttachments() {
let smsController = MFMessageComposeViewController()
smsController.body = "Can you please tell me what colour this is?"
let screenshotImageData: Data = UIImagePNGRepresentation(screenshotImage!)!
smsController.addAttachmentData(screenshotImageData, typeIdentifier: "data", filename: "screenshotImage.png")
smsController.messageComposeDelegate = self
self.present(smsController, animated: true, completion: nil)
} else {
print("User cannot send texts or attachments")
}
}
The above works fine on a separate project, on the latest stable public release of Xcode.
I attempting to add the code to a project that will be running on the latest iOS (11.3 beta 2 I believe) and thus I'm using Xcode 9.3 Beta 2 (Released Feb 6th, 2018) to develop.
Is this a bug in the beta?
The error I am receiving is for the line:
let screenshotImageData: Data = UIImagePNGRepresentation(screenshotImage!)!
and subsequently on the line below too.
Getting:
Cannot convert value of type 'Data' to specified type 'Data'
The error message
Cannot convert value of type 'Data' to specified type 'Data'
indicates that there is another Data type defined in your application
or some included module, which conflicts with struct Data from the
Foundation framework. Here is a self-contained example to demonstrate
the problem:
import Foundation
struct Data { }
let d: Data = "abc".data(using: .utf8)!
// Cannot convert value of type 'Data' to specified type 'Data'
You can always prepend the module name to refer to a type from that module explicitly:
let screenshotImageData: Foundation.Data = UIImagePNGRepresentation(screenshotImage!)!
But actually you don't need the type annotation at all, with
let screenshotImageData = UIImagePNGRepresentation(screenshotImage!)!
the type of screenshotImageData is inferred automatically from
the expression on the right-hand side (as Foundation.Data).
Of course it would be preferable to avoid such a ambiguity and not
define another Data type.

Getting an array from Firebase Database

I've a [String] Array under the "urls" key. How can i get it out? Here is my structure:
You can read it by using an observer to the urls reference and initializing an array with its value.
ref = Database.database().reference()
ref.child("sectionList").child("name of").child("urls")observe(.value, with: { (snapshot:FIRDataSnapshot) in
var urls : [String] = snapshot.children
}
"This function takes two parameters: an instance of FIRDataEventType and a closure.
The event type specifies what event you want to listen for. The code listens for a .value event type, which in turn listens for all types of changes to the data in your Firebase database—add, removed, and changed.
When the change occurs, the database updates the app with the most recent data.
The app is notified of the change via the closure, which is passed an instance of FIRDataSnapshot. The snapshot, as its name suggests, represents the data at that specific moment in time. To access the data in the snapshot, you use the value property."
Source:(https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2)
I got it like this
let ref = Database.database().reference(withPath: "sectionList")
ref.child("name of").child("urls").observeSingleEvent(of: .value, with: { (dataSnapshot:DataSnapshot) in
for object in dataSnapshot.children.allObjects as! [DataSnapshot] {
for obj in object.value as! NSArray {
print("value = \(obj)")
}
}
})
Basicly, I highly recomend you to take a look at this tutorial, especially the part about retrieving Data. To devide your project in standard MVC model will be very useful. I hope my answer will help you ;)