ios - How to populate a UIPickerView with results from a NSFetchRequest using Core Data -
i have uipickerview trying populate results nsfetchrequest pulling data managedobjectcontext. when initialize uipickerview following, kcmodalpickerview *pickerview = [[kcmodalpickerview alloc] initwithvalues:_usernames];
xcode doesn't throw , warnings or errors, when build , run app getting following error.
* terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[account copywithzone:]: unrecognized selector sent instance 0x7a60ec70' * first throw call stack:
now before error due me not implementing copywithzone
method in vc, want point out in class files using keyword copy
the method told causinging crash belongs kcmodalpicker
class implementation file. , method looks following,
// edit method - (nsstring *)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component { return [self.values objectatindex:row]; }
what need change / edit / add prevent app crashing?
update
_usernames nsarray ...the results following,
nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"account" inmanagedobjectcontext:_managedobjectcontext]; [fetchrequest setentity:entity]; fetchrequest.propertiestofetch = [nsarray arraywithobject:[[entity propertiesbyname] objectforkey:@"username"]]; nserror *error = nil; nsarray _usernames = [self.managedobjectcontext executefetchrequest:fetchrequest error:&error];
kcmodalpickerview
expects array of nsstring
, give array of account
. framework tries copy instance because thinks it's nsstring
, conforms nscopying
protocol , implements copywithzone:
. object not, , there [account copywithzone:]: unrecognized selector sent instance
exception.
simply create array of nsstrings using appropriate attribute core data object.
there smarter ways this, obvious solution:
nsmutablearray *names = [nsmutablearray arraywithcapacity:[_usernames count]]; (account *account in _usernames) { nsstring *accountname = account.name; if (!accountname) { accountname = @"<unknown account>"; } [names addobject:accountname]; } kcmodalpickerview *pickerview = [[kcmodalpickerview alloc] initwithvalues:names];
i saw have set propertiestofetch
. additionally have set resultstype
of fetchrequest nsdictionaryresulttype
. in case executefetchrequest:error:
returns array of dictionaries. , should able use nsarray *names = [_usernames valueforkey:@"username"];
instead of loop.
Comments
Post a Comment