NSPredicate | Gives bad exception on Entity Fetch Request | Swift 4
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
add a comment |
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
Possible duplicate of EXC_BAD_ACCESS when building nspredicate
– Larme
Nov 16 '18 at 9:04
add a comment |
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
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
swift core-data nspredicate
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of EXC_BAD_ACCESS when building nspredicate
– Larme
Nov 16 '18 at 9:04