How can I init NSData to nil ?
Because later, I need to check if this data is empty before using UIImageJPEGRepresentation.
Something like :
if data == nil {
data = UIImageJPEGRepresentation(image, 1)
}
I tried data.length == 0 but I don't know why, data.length isn't equal to 0 while I haven't initialized.
One thing you can do is ensure your NSData
property is an optional. If the NSData object has not been initialized yet, then you can perform your if nil
check.
It would look like this:
var data: NSData? = nil
if data == nil {
data = UIImageJPEGRepresentation(image, 1)
}
Because optionals in Swift are set to nil by default, you don't even need the initial assignment portion! You can simply do this:
var data: NSData? //No need for "= nil" here.
if data == nil {
data = UIImageJPEGRepresentation(image, 1)
}