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
How to Override Private Method and Call Super in Swift
Implicitlyunwrappedoptional in Init VS Later
Add Skin Tone Modifier to an Emoji Programmatically
Anyobject Try Cast to Equatable
Synchronized Realm - Airplane Mode
(Appkit) Tab Insertion Inside of Nstextblock
Create a New Window with Nswindow
How to Create Project Without Story Board in Xcode 6 (Swift)
Combining Watchconnectivity and Complications
Shared Cookies with Wkprocesspool for Wkwebview in Swift
How to Set Interactive Push Notifications on iOS8
A Swiftier Way to Convert String to Unsafepointer<Xmlchar> in Swift 3 (Libxml2)
Margin Between Images in Uiscrollview
Alamofire with Custom Parameter Encoding for Swift Application
How to Save Value in Nsset Core Data (Swift)
Does Untimeintervalnotificationtrigger Nexttriggerdate() Give the Wrong Date