How to convert timeStamp value wrapped in an optional into a double?
I currently have a function which fetches images from firebase based on some preliminary info, at the same tree level as the images is a time stamp value (for each image). I have been unable to get the value, only being able to get to Optional(123131.432432) which is not what I need of course.
Below is the function:
func fetchAllPostImages() {
print("fetchAllPostImages function")
self.ref.child("Posts").child(self.userID).child(self.postNum).child("Images").observe(.childAdded, with: { snapshot in
var snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl")
var timeStampData = snapshot.childSnapshot(forPath: "timeStamp").value
print("fhdsjaklhfkjdsfksahdjksfjlashfkljdhs")
print(snapshot.childSnapshot(forPath: "ImageUrl"))
print(snapshot.childSnapshot(forPath: "timeStamp").value, "timestamp123")
print(snapshotUrl.value as? [String: String], "value")
if let urlValue = snapshotUrl.value {
print("jkdhsjbbvcbcbcbccbbbbbbbbbbbbbbb")
let snapShotValue = [ "ImageUrl" : urlValue]
let timeStampVal = ["timeStamp" : timeStampData]
print(timeStampVal, "dsfaf")
for (_, value) in snapShotValue {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStamp = 0.0
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
}
})
}
So I was able to do the following and this seems to work however in the tableView I see teh same Optional(5421432.43214) as before. Even though when I print it it comes out as teh double value I needed.
for (value, timeValue) in zip(snapShotValue.values, timeStampVal) {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStampValue = timeValue as! Double
print(timeStampValue, "fgsdfg")
let timeStamp: Double = timeStampValue
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
How to convert timeStamp value wrapped in an optional into a double?
ios swift firebase firebase-realtime-database
add a comment |
I currently have a function which fetches images from firebase based on some preliminary info, at the same tree level as the images is a time stamp value (for each image). I have been unable to get the value, only being able to get to Optional(123131.432432) which is not what I need of course.
Below is the function:
func fetchAllPostImages() {
print("fetchAllPostImages function")
self.ref.child("Posts").child(self.userID).child(self.postNum).child("Images").observe(.childAdded, with: { snapshot in
var snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl")
var timeStampData = snapshot.childSnapshot(forPath: "timeStamp").value
print("fhdsjaklhfkjdsfksahdjksfjlashfkljdhs")
print(snapshot.childSnapshot(forPath: "ImageUrl"))
print(snapshot.childSnapshot(forPath: "timeStamp").value, "timestamp123")
print(snapshotUrl.value as? [String: String], "value")
if let urlValue = snapshotUrl.value {
print("jkdhsjbbvcbcbcbccbbbbbbbbbbbbbbb")
let snapShotValue = [ "ImageUrl" : urlValue]
let timeStampVal = ["timeStamp" : timeStampData]
print(timeStampVal, "dsfaf")
for (_, value) in snapShotValue {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStamp = 0.0
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
}
})
}
So I was able to do the following and this seems to work however in the tableView I see teh same Optional(5421432.43214) as before. Even though when I print it it comes out as teh double value I needed.
for (value, timeValue) in zip(snapShotValue.values, timeStampVal) {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStampValue = timeValue as! Double
print(timeStampValue, "fgsdfg")
let timeStamp: Double = timeStampValue
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
How to convert timeStamp value wrapped in an optional into a double?
ios swift firebase firebase-realtime-database
It's an optional. Unwrap it properly just like you are unwrapping other optionals in the code you posted.
– rmaddy
Nov 16 '18 at 4:12
@rmaddy check update
– Q The Great
Nov 16 '18 at 4:19
564030781.136256 fgsdfg is what I get when i print teh value and yet i get teh optional thing in the tableView
– Q The Great
Nov 16 '18 at 4:25
You have another optional somewhere. Debug your code. Find where it is coming from. Then unwrap. BTW - usingas!is a bad idea. It will crash if it's nil.
– rmaddy
Nov 16 '18 at 4:27
Can you post a snippet of your firebase data in your question? Your timestamp and my timestamp might be two different things. Also, in the future, please reduce your code to the minimum required to reproduce the issue. There's a lot of junk code there which just makes it harder to read. See How to create a Minimal, Complete, and Verifiable example. Also, let snapShotValue = [ "ImageUrl" : urlValue] looks like snapShotValue is a single URL, but then you iterate over it which is odd. for (_, value) in snapShotValue. The code is very confusing.
– Jay
Nov 18 '18 at 13:39
add a comment |
I currently have a function which fetches images from firebase based on some preliminary info, at the same tree level as the images is a time stamp value (for each image). I have been unable to get the value, only being able to get to Optional(123131.432432) which is not what I need of course.
Below is the function:
func fetchAllPostImages() {
print("fetchAllPostImages function")
self.ref.child("Posts").child(self.userID).child(self.postNum).child("Images").observe(.childAdded, with: { snapshot in
var snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl")
var timeStampData = snapshot.childSnapshot(forPath: "timeStamp").value
print("fhdsjaklhfkjdsfksahdjksfjlashfkljdhs")
print(snapshot.childSnapshot(forPath: "ImageUrl"))
print(snapshot.childSnapshot(forPath: "timeStamp").value, "timestamp123")
print(snapshotUrl.value as? [String: String], "value")
if let urlValue = snapshotUrl.value {
print("jkdhsjbbvcbcbcbccbbbbbbbbbbbbbbb")
let snapShotValue = [ "ImageUrl" : urlValue]
let timeStampVal = ["timeStamp" : timeStampData]
print(timeStampVal, "dsfaf")
for (_, value) in snapShotValue {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStamp = 0.0
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
}
})
}
So I was able to do the following and this seems to work however in the tableView I see teh same Optional(5421432.43214) as before. Even though when I print it it comes out as teh double value I needed.
for (value, timeValue) in zip(snapShotValue.values, timeStampVal) {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStampValue = timeValue as! Double
print(timeStampValue, "fgsdfg")
let timeStamp: Double = timeStampValue
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
How to convert timeStamp value wrapped in an optional into a double?
ios swift firebase firebase-realtime-database
I currently have a function which fetches images from firebase based on some preliminary info, at the same tree level as the images is a time stamp value (for each image). I have been unable to get the value, only being able to get to Optional(123131.432432) which is not what I need of course.
Below is the function:
func fetchAllPostImages() {
print("fetchAllPostImages function")
self.ref.child("Posts").child(self.userID).child(self.postNum).child("Images").observe(.childAdded, with: { snapshot in
var snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl")
var timeStampData = snapshot.childSnapshot(forPath: "timeStamp").value
print("fhdsjaklhfkjdsfksahdjksfjlashfkljdhs")
print(snapshot.childSnapshot(forPath: "ImageUrl"))
print(snapshot.childSnapshot(forPath: "timeStamp").value, "timestamp123")
print(snapshotUrl.value as? [String: String], "value")
if let urlValue = snapshotUrl.value {
print("jkdhsjbbvcbcbcbccbbbbbbbbbbbbbbb")
let snapShotValue = [ "ImageUrl" : urlValue]
let timeStampVal = ["timeStamp" : timeStampData]
print(timeStampVal, "dsfaf")
for (_, value) in snapShotValue {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStamp = 0.0
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
}
})
}
So I was able to do the following and this seems to work however in the tableView I see teh same Optional(5421432.43214) as before. Even though when I print it it comes out as teh double value I needed.
for (value, timeValue) in zip(snapShotValue.values, timeStampVal) {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStampValue = timeValue as! Double
print(timeStampValue, "fgsdfg")
let timeStamp: Double = timeStampValue
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
How to convert timeStamp value wrapped in an optional into a double?
ios swift firebase firebase-realtime-database
ios swift firebase firebase-realtime-database
edited Mar 11 at 19:56
Q The Great
asked Nov 16 '18 at 3:52
Q The GreatQ The Great
62119
62119
It's an optional. Unwrap it properly just like you are unwrapping other optionals in the code you posted.
– rmaddy
Nov 16 '18 at 4:12
@rmaddy check update
– Q The Great
Nov 16 '18 at 4:19
564030781.136256 fgsdfg is what I get when i print teh value and yet i get teh optional thing in the tableView
– Q The Great
Nov 16 '18 at 4:25
You have another optional somewhere. Debug your code. Find where it is coming from. Then unwrap. BTW - usingas!is a bad idea. It will crash if it's nil.
– rmaddy
Nov 16 '18 at 4:27
Can you post a snippet of your firebase data in your question? Your timestamp and my timestamp might be two different things. Also, in the future, please reduce your code to the minimum required to reproduce the issue. There's a lot of junk code there which just makes it harder to read. See How to create a Minimal, Complete, and Verifiable example. Also, let snapShotValue = [ "ImageUrl" : urlValue] looks like snapShotValue is a single URL, but then you iterate over it which is odd. for (_, value) in snapShotValue. The code is very confusing.
– Jay
Nov 18 '18 at 13:39
add a comment |
It's an optional. Unwrap it properly just like you are unwrapping other optionals in the code you posted.
– rmaddy
Nov 16 '18 at 4:12
@rmaddy check update
– Q The Great
Nov 16 '18 at 4:19
564030781.136256 fgsdfg is what I get when i print teh value and yet i get teh optional thing in the tableView
– Q The Great
Nov 16 '18 at 4:25
You have another optional somewhere. Debug your code. Find where it is coming from. Then unwrap. BTW - usingas!is a bad idea. It will crash if it's nil.
– rmaddy
Nov 16 '18 at 4:27
Can you post a snippet of your firebase data in your question? Your timestamp and my timestamp might be two different things. Also, in the future, please reduce your code to the minimum required to reproduce the issue. There's a lot of junk code there which just makes it harder to read. See How to create a Minimal, Complete, and Verifiable example. Also, let snapShotValue = [ "ImageUrl" : urlValue] looks like snapShotValue is a single URL, but then you iterate over it which is odd. for (_, value) in snapShotValue. The code is very confusing.
– Jay
Nov 18 '18 at 13:39
It's an optional. Unwrap it properly just like you are unwrapping other optionals in the code you posted.
– rmaddy
Nov 16 '18 at 4:12
It's an optional. Unwrap it properly just like you are unwrapping other optionals in the code you posted.
– rmaddy
Nov 16 '18 at 4:12
@rmaddy check update
– Q The Great
Nov 16 '18 at 4:19
@rmaddy check update
– Q The Great
Nov 16 '18 at 4:19
564030781.136256 fgsdfg is what I get when i print teh value and yet i get teh optional thing in the tableView
– Q The Great
Nov 16 '18 at 4:25
564030781.136256 fgsdfg is what I get when i print teh value and yet i get teh optional thing in the tableView
– Q The Great
Nov 16 '18 at 4:25
You have another optional somewhere. Debug your code. Find where it is coming from. Then unwrap. BTW - using
as! is a bad idea. It will crash if it's nil.– rmaddy
Nov 16 '18 at 4:27
You have another optional somewhere. Debug your code. Find where it is coming from. Then unwrap. BTW - using
as! is a bad idea. It will crash if it's nil.– rmaddy
Nov 16 '18 at 4:27
Can you post a snippet of your firebase data in your question? Your timestamp and my timestamp might be two different things. Also, in the future, please reduce your code to the minimum required to reproduce the issue. There's a lot of junk code there which just makes it harder to read. See How to create a Minimal, Complete, and Verifiable example. Also, let snapShotValue = [ "ImageUrl" : urlValue] looks like snapShotValue is a single URL, but then you iterate over it which is odd. for (_, value) in snapShotValue. The code is very confusing.
– Jay
Nov 18 '18 at 13:39
Can you post a snippet of your firebase data in your question? Your timestamp and my timestamp might be two different things. Also, in the future, please reduce your code to the minimum required to reproduce the issue. There's a lot of junk code there which just makes it harder to read. See How to create a Minimal, Complete, and Verifiable example. Also, let snapShotValue = [ "ImageUrl" : urlValue] looks like snapShotValue is a single URL, but then you iterate over it which is odd. for (_, value) in snapShotValue. The code is very confusing.
– Jay
Nov 18 '18 at 13:39
add a comment |
2 Answers
2
active
oldest
votes
Basically, the easiest way to do this to add an Int to where you call the arrayOfImg[indexPath.row].timeStamp. So it should look like this:
let timeStamp = "(Int(arrayOfImg[indexPath.row].timeStamp))"
add a comment |
Try replacing
let timeStamp: Double = timeStampValue
with
guard let timeStamp = timeStampValue as Double else { return }
This will safely unwrap your optional as well as cast the value as a Double.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331174%2fhow-to-convert-timestamp-value-wrapped-in-an-optional-into-a-double%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Basically, the easiest way to do this to add an Int to where you call the arrayOfImg[indexPath.row].timeStamp. So it should look like this:
let timeStamp = "(Int(arrayOfImg[indexPath.row].timeStamp))"
add a comment |
Basically, the easiest way to do this to add an Int to where you call the arrayOfImg[indexPath.row].timeStamp. So it should look like this:
let timeStamp = "(Int(arrayOfImg[indexPath.row].timeStamp))"
add a comment |
Basically, the easiest way to do this to add an Int to where you call the arrayOfImg[indexPath.row].timeStamp. So it should look like this:
let timeStamp = "(Int(arrayOfImg[indexPath.row].timeStamp))"
Basically, the easiest way to do this to add an Int to where you call the arrayOfImg[indexPath.row].timeStamp. So it should look like this:
let timeStamp = "(Int(arrayOfImg[indexPath.row].timeStamp))"
answered Jan 20 at 20:43
user10817680
add a comment |
add a comment |
Try replacing
let timeStamp: Double = timeStampValue
with
guard let timeStamp = timeStampValue as Double else { return }
This will safely unwrap your optional as well as cast the value as a Double.
add a comment |
Try replacing
let timeStamp: Double = timeStampValue
with
guard let timeStamp = timeStampValue as Double else { return }
This will safely unwrap your optional as well as cast the value as a Double.
add a comment |
Try replacing
let timeStamp: Double = timeStampValue
with
guard let timeStamp = timeStampValue as Double else { return }
This will safely unwrap your optional as well as cast the value as a Double.
Try replacing
let timeStamp: Double = timeStampValue
with
guard let timeStamp = timeStampValue as Double else { return }
This will safely unwrap your optional as well as cast the value as a Double.
answered Nov 16 '18 at 18:48
drobersondroberson
251
251
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331174%2fhow-to-convert-timestamp-value-wrapped-in-an-optional-into-a-double%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's an optional. Unwrap it properly just like you are unwrapping other optionals in the code you posted.
– rmaddy
Nov 16 '18 at 4:12
@rmaddy check update
– Q The Great
Nov 16 '18 at 4:19
564030781.136256 fgsdfg is what I get when i print teh value and yet i get teh optional thing in the tableView
– Q The Great
Nov 16 '18 at 4:25
You have another optional somewhere. Debug your code. Find where it is coming from. Then unwrap. BTW - using
as!is a bad idea. It will crash if it's nil.– rmaddy
Nov 16 '18 at 4:27
Can you post a snippet of your firebase data in your question? Your timestamp and my timestamp might be two different things. Also, in the future, please reduce your code to the minimum required to reproduce the issue. There's a lot of junk code there which just makes it harder to read. See How to create a Minimal, Complete, and Verifiable example. Also, let snapShotValue = [ "ImageUrl" : urlValue] looks like snapShotValue is a single URL, but then you iterate over it which is odd. for (_, value) in snapShotValue. The code is very confusing.
– Jay
Nov 18 '18 at 13:39