Countforfetchrequest in Swift 2.0

countForFetchRequest in Swift 2.0

Your code is almost correct, but error needs to be a variable, in order to be passed as
inout-argument with &:

var error: NSError? = nil
let count = managedObjectContext.countForFetchRequest(fetchRequest, error: &error)

Update: As of Swift 3, countForFetchRequest
throws an error:

do {
let count = try managedObjectContext.context.count(for:fetchRequest)
return count
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
return 0
}

reflect property value on swift 2.0

This is the usage of reflection is swift 2.

let mirror = Mirror(reflecting: obj)
for child in mirror.children {
guard let key = child.label else {
continue
}
let value = child.value

// use value or key here
}

do try catch swift 2

Rewriting your code to work the same as your original code

do {
try managedObjectContext!.save()

//this happens when save did pass
NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)

//this error variable has nothing to do with save in your original code
if error != nil {
print(error?.localizedDescription)
}
}
catch {
//this happens when save() doesn't pass
abort()
}

what you probably want to write is the following:

do {
try managedObjectContext!.save()

//this happens when save did pass
NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil)
}
catch let saveError as NSError {
//this happens when save() doesn't pass
print(saveError.localizedDescription)
abort()
}

Swift 2.3 NSFetchRequest

Swift 3.1

This is work for me.

class func objectCountForEntity (entityName:String, context:NSManagedObjectContext) -> Int {

let request = NSFetchRequest(entityName: entityName)
var error:NSError?
let count = try! context.count(for: request)

if let _error = error {
print("\(#function) Error: \(_error.localizedDescription)")
} else {
print("There are \(count) \(entityName) object(s) in \(context)")
}
return count
}

PFArrayResultBlock(parse) is causing an error while converting to swift 2.0

The problem that your completion block is

PFArrayResultBlock

While the query expects

PFQueryArrayResultBlock

The types are slightly different - one expects [PFObject]? the other [AnyObject]?

Swift 2.0 interop with Objective C not working as expected?

It is not possible.

If you annotate your method with @objc you will see the problem.

Throwing method cannot be marked @objc because it returns a value of
type 'Int'; return 'Void' or a type that bridges to an Objective-C
class

You can return only objects, primitives are not supported.

How to check if core data is empty

To check if the Core Database is empty you have to make a NSFetchRequest on the entity you want to check, and check if the results of the request are empty.

You can check it with this function:

func entityIsEmpty(entity: String) -> Bool
{

var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context = NSManagedObjectContext()

var request = NSFetchRequest(entityName: entity)
var error = NSErrorPointer()

var results:NSArray? = self.context.executeFetchRequest(request, error: error)

if let res = results
{
if res.count == 0
{
return true
}
else
{
return false
}
}
else
{
println("Error: \(error.debugDescription)")
return true
}

}

Or simplier and shorter solution: (using .countForFetchRequest)

func entityIsEmpty(entity: String) -> Bool
{

var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context = NSManagedObjectContext()

var request = NSFetchRequest(entityName: entity)
var error = NSErrorPointer()

var results:NSArray? = self.context.executeFetchRequest(request, error: error)

var count = context.countForFetchRequest(request, error: error)

if error != nil
{
println("Error: \(error.debugDescription)")
return true
}
else
{
if count == 0
{
return true
}
else
{
return false
}

}

}


Related Topics



Leave a reply



Submit