NSPredicate | Gives bad exception on Entity Fetch Request | Swift 4












0















I try to get an Entity via an NSPredicate from CoreData model. Initially I insert an entity using its id and now intend to get it. However my code crashes without any further description on specifically my NSPredicate. I have no idea why? My id attribute is Int32:



        var employTime:EmployeeTime?
let context = getContext()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "EmployeeTime")

//code crashes on this bottom line
let predicate = NSPredicate(format: "id == %@", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest) as? [EmployeeTime]
if (employTimes?.count)! > 0 {
for et in employTimes! {
if et.id == id {
employTime = et
}
}
} else {
employTime = EmployeeTime(context: context)
}
return employTime!


Any help most greatly appreciated.










share|improve this question























  • Possible duplicate of EXC_BAD_ACCESS when building nspredicate

    – Larme
    Nov 16 '18 at 9:04
















0















I try to get an Entity via an NSPredicate from CoreData model. Initially I insert an entity using its id and now intend to get it. However my code crashes without any further description on specifically my NSPredicate. I have no idea why? My id attribute is Int32:



        var employTime:EmployeeTime?
let context = getContext()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "EmployeeTime")

//code crashes on this bottom line
let predicate = NSPredicate(format: "id == %@", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest) as? [EmployeeTime]
if (employTimes?.count)! > 0 {
for et in employTimes! {
if et.id == id {
employTime = et
}
}
} else {
employTime = EmployeeTime(context: context)
}
return employTime!


Any help most greatly appreciated.










share|improve this question























  • Possible duplicate of EXC_BAD_ACCESS when building nspredicate

    – Larme
    Nov 16 '18 at 9:04














0












0








0








I try to get an Entity via an NSPredicate from CoreData model. Initially I insert an entity using its id and now intend to get it. However my code crashes without any further description on specifically my NSPredicate. I have no idea why? My id attribute is Int32:



        var employTime:EmployeeTime?
let context = getContext()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "EmployeeTime")

//code crashes on this bottom line
let predicate = NSPredicate(format: "id == %@", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest) as? [EmployeeTime]
if (employTimes?.count)! > 0 {
for et in employTimes! {
if et.id == id {
employTime = et
}
}
} else {
employTime = EmployeeTime(context: context)
}
return employTime!


Any help most greatly appreciated.










share|improve this question














I try to get an Entity via an NSPredicate from CoreData model. Initially I insert an entity using its id and now intend to get it. However my code crashes without any further description on specifically my NSPredicate. I have no idea why? My id attribute is Int32:



        var employTime:EmployeeTime?
let context = getContext()
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "EmployeeTime")

//code crashes on this bottom line
let predicate = NSPredicate(format: "id == %@", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest) as? [EmployeeTime]
if (employTimes?.count)! > 0 {
for et in employTimes! {
if et.id == id {
employTime = et
}
}
} else {
employTime = EmployeeTime(context: context)
}
return employTime!


Any help most greatly appreciated.







swift core-data nspredicate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 5:39









Mohsin Khubaib AhmedMohsin Khubaib Ahmed

7771322




7771322













  • Possible duplicate of EXC_BAD_ACCESS when building nspredicate

    – Larme
    Nov 16 '18 at 9:04



















  • Possible duplicate of EXC_BAD_ACCESS when building nspredicate

    – Larme
    Nov 16 '18 at 9:04

















Possible duplicate of EXC_BAD_ACCESS when building nspredicate

– Larme
Nov 16 '18 at 9:04





Possible duplicate of EXC_BAD_ACCESS when building nspredicate

– Larme
Nov 16 '18 at 9:04












1 Answer
1






active

oldest

votes


















1














This is a very common mistake.



The placeholder %@ is only for objects, for an Int32 you need %d or %i



let predicate = NSPredicate(format: "id == %d", id)


And create the fetch request with the concrete NSManagedObject subclass



let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")


this avoids the type cast later.





Your entire code can be simplified (no optionals needed):



let employTime:EmployeeTime
let context = getContext()
let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")
let predicate = NSPredicate(format: "id == %d", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest)
if let et = employTimes.first {
employTime = et
} else {
employTime = EmployeeTime(context: context)
}
return employTime


However you should avoid try!. Make your function throw and hand over a potential error to the caller.






share|improve this answer


























  • Wohoo! Works like a charm :) Thank you for all the tips as well.

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:12











  • can you however tell me if predicate should be = or == and what is the difference between them

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:17








  • 1





    There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

    – vadian
    Nov 16 '18 at 7:46












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332055%2fnspredicate-gives-bad-exception-on-entity-fetch-request-swift-4%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is a very common mistake.



The placeholder %@ is only for objects, for an Int32 you need %d or %i



let predicate = NSPredicate(format: "id == %d", id)


And create the fetch request with the concrete NSManagedObject subclass



let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")


this avoids the type cast later.





Your entire code can be simplified (no optionals needed):



let employTime:EmployeeTime
let context = getContext()
let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")
let predicate = NSPredicate(format: "id == %d", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest)
if let et = employTimes.first {
employTime = et
} else {
employTime = EmployeeTime(context: context)
}
return employTime


However you should avoid try!. Make your function throw and hand over a potential error to the caller.






share|improve this answer


























  • Wohoo! Works like a charm :) Thank you for all the tips as well.

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:12











  • can you however tell me if predicate should be = or == and what is the difference between them

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:17








  • 1





    There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

    – vadian
    Nov 16 '18 at 7:46
















1














This is a very common mistake.



The placeholder %@ is only for objects, for an Int32 you need %d or %i



let predicate = NSPredicate(format: "id == %d", id)


And create the fetch request with the concrete NSManagedObject subclass



let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")


this avoids the type cast later.





Your entire code can be simplified (no optionals needed):



let employTime:EmployeeTime
let context = getContext()
let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")
let predicate = NSPredicate(format: "id == %d", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest)
if let et = employTimes.first {
employTime = et
} else {
employTime = EmployeeTime(context: context)
}
return employTime


However you should avoid try!. Make your function throw and hand over a potential error to the caller.






share|improve this answer


























  • Wohoo! Works like a charm :) Thank you for all the tips as well.

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:12











  • can you however tell me if predicate should be = or == and what is the difference between them

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:17








  • 1





    There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

    – vadian
    Nov 16 '18 at 7:46














1












1








1







This is a very common mistake.



The placeholder %@ is only for objects, for an Int32 you need %d or %i



let predicate = NSPredicate(format: "id == %d", id)


And create the fetch request with the concrete NSManagedObject subclass



let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")


this avoids the type cast later.





Your entire code can be simplified (no optionals needed):



let employTime:EmployeeTime
let context = getContext()
let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")
let predicate = NSPredicate(format: "id == %d", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest)
if let et = employTimes.first {
employTime = et
} else {
employTime = EmployeeTime(context: context)
}
return employTime


However you should avoid try!. Make your function throw and hand over a potential error to the caller.






share|improve this answer















This is a very common mistake.



The placeholder %@ is only for objects, for an Int32 you need %d or %i



let predicate = NSPredicate(format: "id == %d", id)


And create the fetch request with the concrete NSManagedObject subclass



let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")


this avoids the type cast later.





Your entire code can be simplified (no optionals needed):



let employTime:EmployeeTime
let context = getContext()
let fetchRequest = NSFetchRequest<EmployeeTime>(entityName: "EmployeeTime")
let predicate = NSPredicate(format: "id == %d", id)
fetchRequest.predicate = predicate
let employTimes = try! context.fetch(fetchRequest)
if let et = employTimes.first {
employTime = et
} else {
employTime = EmployeeTime(context: context)
}
return employTime


However you should avoid try!. Make your function throw and hand over a potential error to the caller.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 6:06

























answered Nov 16 '18 at 6:00









vadianvadian

154k17165190




154k17165190













  • Wohoo! Works like a charm :) Thank you for all the tips as well.

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:12











  • can you however tell me if predicate should be = or == and what is the difference between them

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:17








  • 1





    There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

    – vadian
    Nov 16 '18 at 7:46



















  • Wohoo! Works like a charm :) Thank you for all the tips as well.

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:12











  • can you however tell me if predicate should be = or == and what is the difference between them

    – Mohsin Khubaib Ahmed
    Nov 16 '18 at 6:17








  • 1





    There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

    – vadian
    Nov 16 '18 at 7:46

















Wohoo! Works like a charm :) Thank you for all the tips as well.

– Mohsin Khubaib Ahmed
Nov 16 '18 at 6:12





Wohoo! Works like a charm :) Thank you for all the tips as well.

– Mohsin Khubaib Ahmed
Nov 16 '18 at 6:12













can you however tell me if predicate should be = or == and what is the difference between them

– Mohsin Khubaib Ahmed
Nov 16 '18 at 6:17







can you however tell me if predicate should be = or == and what is the difference between them

– Mohsin Khubaib Ahmed
Nov 16 '18 at 6:17






1




1





There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

– vadian
Nov 16 '18 at 7:46





There is no difference, please see developer.apple.com/library/archive/documentation/Cocoa/…

– vadian
Nov 16 '18 at 7:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332055%2fnspredicate-gives-bad-exception-on-entity-fetch-request-swift-4%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values