user4184036
user4184036

Reputation:

Checking if NSData exist or not in swift

I have an NSData object stored in my core data. I want to be able to check if it exist or not using an if statement. I can't seem to work it out. I have the variable set up as:

var coreImage : NSData?

and I have tried using:

if (coreImage != nil) {

            println("Use core image")

        }else {

           println("Do something else")

}

I know I have NSData stored in core data but it never runs the if statement as I want it too so I must be doing something wrong?

Can anyone help?

Thanks.

Upvotes: 2

Views: 6283

Answers (2)

Vivienne Fosh
Vivienne Fosh

Reputation: 1778

Check if NSData's length is > 0, that's it. Example:

if (coreImage.length > 0) {
//we're cool
}

Upvotes: 11

Vidya
Vidya

Reputation: 30310

I am newer to Swift than you, but I think you need to do some special work to access and utilize items from Core Data. Try something like this:

let fetchRequest = NSFetchRequest(entityName: "coreImage")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [NSData] {  
   println("Use core image")    
} else {
   println("Do something else")
}

Upvotes: 0

Related Questions