Swift Nspredicate Throwing Exc_Bad_Access(Code=1, Address=0X1) When Compounding Statements

Swift NSPredicate throwing EXC_BAD_ACCESS(Code=1, address=0x1) when compounding statements

The %@ placeholder in predicate format strings is for Objective-C
objects, so you have to wrap the integer into an NSNumber:

fetchRequest.predicate = NSPredicate(format: "level = %@", NSNumber(integer: level))

or use ld instead to format a (long) integer:

fetchRequest.predicate = NSPredicate(format: "level = %ld", level)

Note also that

fetchRequest.predicate = NSPredicate(format: ...)
fetchRequest.predicate = NSPredicate(format: ...)

does not create a compound predicate, the seconds assignment simply
overwrites the first. You can use an NSCompoundPredicate:

let p1 = NSPredicate(format: "level = %ld", level)!
let p2 = NSPredicate(format: "section = %ld", section)!
fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates([p1, p2])

or simply combine the predicates with "AND":

fetchRequest.predicate = NSPredicate(format: "level = %ld AND section = %ld", level, section)

swift - NSPREDICATE fails

let predicate = NSPredicate(format: "articleID == %@", articleID!)

articleID is an integer. I used %ld instead of %@ and it worked...

Swift NSPredicate throwing EXC_BAD_ACCESS(Code=1, address=0x1) when compounding statements

App crash on sign in (xcode 9.3) EXC_BAD_ACCESS (code=1, address=0x1)

Have a similar issue with Facebook login - a work for me was found in the Xcode 9.3 release notes:

https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-DontLinkElementID_1

To quote them

In circumstances where protocol methods or base class methods defined
in Objective-C claim to take non-null arguments of type id in their
headers, but get invoked with nil values at runtime, Swift code
compiled by Xcode 9.3 that overrides those methods may crash when the
Swift implementations are invoked. (38675815) Workaround: Change the
Swift override to take a value of type Any?. For example, if you
implement the UIApplicationDelegate protocol's
application(_:open:sourceApplication:annotation:) method:

class AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return true
}

The program may crash when passed nil as the annotation argument.
Avoid the crash by making the annotation argument have type Any?:

class AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any?) -> Bool {
return true
}

Core Data NSPredicate not working

The problem is %@ format for value:Int , cast it to value as NSObject or AnyObject like following:

NSPredicate(format: "%K == %@", colummName, value as NSObject)

how to query using NSpredicate

Steps with dealing with Core Data are:

Create a fetch request to pull objects into the managed object context

// Assuming you have an entity called Rooms:
[NSFetchRequest fetchRequestWithEntityName:@"Rooms"];

Now create the predicates to be applied to that entity to filter what is returned

// Assuming that the Rooms entity has attributes for "roomType" and "roomStatus"
// I'd actually use %K and attributes - but this will do until you learn about them.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"roomType == %@ and roomStatus == %@", @"ac single", @"YES"];

[request setPredicate:predicate];

Run the fetch request

// Assuming you have the managed Object Context in a property
NSError *error;
NSArray *results = [self.moc executeFetchRequest:request error:&error];

// Make sure the results are not nil, if they are handle the error
if (!results) {
// Handle error in here using the error parameter you passed in by reference.
}

The results are now in an array and you can get the number of entities that satisfy the predicate simply with:

NSUInteger resultCount = [results count];

This is all standard stuff when working with Core Data. If you work your way through it and try to understand the steps - you'll be a long way to writing your own fetch requests.

NSLocalizedString with variables in Swift doesn't work

You are using the wrong format specifier.

%@ is for objects, an Int (assuming that secondiRimanenti is Int) is %ld.

let appSec = String(format: NSLocalizedString("Cerco sismometri disponibili... %ld secondi", comment: ""), secondiRimanenti)

Filtering out integer from NSManaged object

The code you've written, i.e.

fetchRequest.predicate = NSPredicate(format:"part == %@", 1)

In the above code, you're trying to filter all the fetched rows based on whether part is 1 or not. So basically here you're using part as an Int. But as per the declaration, part is an array [Int].

It will definitely throw an exception.

You need to filter each row after fetching like:

let filteredPart = part.filter({ $0 == 1 })


Related Topics



Leave a reply



Submit