last day (15 days later) » 

9:39 PM
0
A: Code that has run smoothly for days suddenly not working. Not sure how to fix the exception that is being thrown

user965972Did you update Xcode recently. Apple is busy updating their cocoa libraries. Every return value was explicitly being unwrapped (like return String!). Now they are going through every function and evaluating whether it should return an instance (return String) or an optional (String?). So check y...

 
I have the most recent version of Xcode, and I've had it since I started this project. My code does include a fair amount of optionals, but a lot of those Xcode prompted me to include. Like I said before however, my code was working last night, but when I tried running it this morning I got that error. Do you have any other ideas?...Thanks for the response!
 
1) Clean the build (folder). 2) Add more breakpoints or 3) selectively comment out parts of your code. Narrow it down as much as you can. I am sure you can find the offending line code that way. BTW, are your sure your data still exists or is accessible?
 
Yes, I'm using Parse and I can still see the data that I'm trying to access on the data browser. I can also print out the variables associated with the data on my console. After cleaning the build I'm finally getting an error in my console stating "fatal error: unexpectedly found nil while unwrapping an Optional value". So at least that's a start. I believe the offending lines occur when I'm trying to getDataInBackground. What I don't understand though, is that I'm printing the data on to my console right before I try to access it. So i'm not quite sure why it's being returned as nil.
 
if you do println(), do you get ... or Optional(...)? Println can handle either but maybe your receiver can't. What does getDataInBackground do? Is it using queues? Are your importing in a background queue but displaying it on the main queue? It's a bad, bad idea to send data to UIKit or core data from a background queue. It will work for a while but inevitable it will bite you in the ass.
 
I don't get the "or Optional(...)" line you are talking about, but when I println() the content i'm trying to access, it returns a dictionary with all of the content's variables. The content class is a Photo, and the Photo's variables include: sender, recipients(array), imageFile which is a PFFile, etc... None of those are set to nil when I println(), so I know they're there. getDataInBackgroundWithBlock() is a Parse API method which makes an asynchronous call to the server to retrieve the imageFile, which is then converted into NSData so that I can convert it from there into a UIImage.
I guess this is me getting bitten in the ass, haha
 
9:39 PM
oh, it's a third party library. Well using their async calls should be safe then (if they know what they are doing). Try this: don't fetch a picture from the server but use just a local file that you load with the standard UIImage or NSData or ... methods. If the stand-in data gets displayed properly, then you know its a problem with getDataInBackgroundWithBlock.
Are you there?
 
Yes I am! Thanks so much for working through this with me
Really appreciate it
 
no problem.
I think I know where you problem is now.
You are getting a NSDictionary back from the async call.
yes?
 
yes
 
are you trying to access the data within like this let imageData = dict["imagedata"]?
because NSDictionary is using [NSObject, AnyObject?] and that has been causing me problems lately
I have to do this:
let myData : AnyObject? = dict["key"]
 
yes that's how i'm accessing it
 
9:42 PM
let myAssumedData = myData as? NSData
 
it's technically saved as a PFFile, but the asynchronous method is supposed to return it in NSData format
 
UIImage(myAssumedData!)
yes, but swift doesn't know that
 
let me give that a shot
 
every time I get a NSDictionary from a cocoa library (of at least a non-swift class), I run into this issue. It leads to ugly code to transform it into the correct swift type, but at least it works
Swift only sees a AnyObject in that dictionary. You have to tell it explicitly what type it is and whether or not it's an optional.
If that doesn't fix it, then I am all out of ideas I am afraid.
 
let userImageFile = content["imageFile"] as? NSData
userImageFile.getDataInBackgroundWithBlock { (imageData:NSData!, error:NSError!) -> Void in
if error == nil
{
var contentImage = UIImage(data: imageData)
self.feedView.image = contentImage
}
}
 
9:45 PM
no, that is not working, is it?
 
that's why I tried, and i get an error that states
NSData does not have a member named getDataInBackgroundwithBlock
 
let userImageFile : AnyObject = content["imageFile"]
if let assumedIamgeFile = userImageFile as? NSdata
 
what i tried*
 
{
var contentImage = UIImage(data: assumedIamgeFile)
}
 
ahh ok, I see now, my mistake, sorry. let me correct
 
9:47 PM
no problem. I have the same issue every time. It's ugly code I know. I was planning to ask stackoverflow if there is a nicer way.
 
and then I put the getdatainbackgroundwithblock call within the if statement correct?
i'll put the entire code once i type it
 
yes, everything within the if block and only use the assumedIamgeFile => assumedImageFile (typo)
 
loading on phone.....
 
ok.
can you paste the code?
 
No dice :(
let userImageFile: AnyObject = content["imageFile"]
if let assumedImageFile = userImageFile as? NSData {
assumedImageFile.getDataInBackgroundWithBlock { (imageData:NSData!, error:NSError!) -> Void in
if error == nil
{
var contentImage = UIImage(data: imageData)
self.feedView.image = contentImage
}
}
}
 
9:51 PM
oh... :(
 
i couldn't use assumedimagefile with getdatainbackgroundwithblock
 
wait, wait
 
so i tried using userimagefile again and it didn't work either
 
where is the NSData coming from? From getDataInBackgroundWithBlock?
 
yes
var contentImage = UIImage(data: imageData)
 
9:54 PM
and what is PFFile?
 
UIImage is being set with the imageData property coming from getDataInBackgroundWithBlock
PFFile is a Parse file class that you use to store various files...images, videos, sounds, etc...
 
and content is a NSDictionary?
if content is the NSDictionary, then what I described applies to the userImageFile rather than the NSData
although that would be bit strange because the block is getting executed?
 
content is an nsdictionary yes
or rather a Parse Class that I created called "Photo" but the class objects get returned in the form of an nsdictionary
what I don't understand is why the original code worked for 5 days before bugging out like this
 
you could try to save the image file to disk and see if the picture is what you expected. Then you know the image is correctly generated and the problem is in displaying it.
ah, yes, maybe a server problem?
 
yes, the image is correctly generated. I can view it in my data browser
 
10:00 PM
so you have a correct UIImage within the block statement of getDataInBackgroundWithBlock ?
 
It could be a server problem, but i can post photos to the server
and access the server other ways... it seems strange that just this one method isn't working
yes, the correct uiimage
 
try this: dispatch_async(dispatch_get_main_queue()){
self.senderProfilePic.image = picture
}
 
but when the block gets executed it returns to nil
 
ah, so you are not retaining the data?
You need to keep a strong reference to the image
 
well that's what I assume the issue is
i keep getting the same "fatal error: unexpectedly found nil while unwrapping an Optional value"
GCD didn't work unfortunately :/
 
10:07 PM
if let assumedImage = picture
{
}
and set the senderProfilePic.image to assumedImage within the brackets
if not, then the problem is probably with senderProfilePic. No idea how that looks like or how the rest or you app is coded (or what the bindings are). So I am afraid I'll have to leave you where we started (it's past midnight here).
Good luck with your app!
 
thanks a bunch, i appreciate the effort!
i'll let you know once i get it resolved
 
no problem. Ok. Hope to hear from you soon.
 

  last day (15 days later) »