Friday, 15 June 2012

ios - How to populate a UIPickerView with results from a NSFetchRequest using Core Data -


"itemprop =" text ">

I am trying to populate a UIPickerView with the results of a NSFetchRequest pulling data from a managedObjectContext. When I start my UIPickerView with the following, KCModalPickerView * pickerView = [[KCModalPickerView alloc] initWithValues: _userNames]; Xcode does not throw errors or errors, but when I run the build and the application I am running, the following error is taking place

* Expected 'NSInvalidArgumentException' Because of their end application: The first 'throw' [account copyWithZone:] sent to non-recognized selector example 0x7a60ec70 'call stack:

Before you say this error, CopyWithZone are not applicable to the method, I have to say that somewhere in my class files I am using the keyword copy

I was told that the The reason for the crash is that it is related to the KCModalPicker class implementation file. The method looks like the following:

  // Edit this method - (NSString *) pickerView: (UIPickerView *) pickerView titleForRow: (NSInteger) line forComponent: (NSInteger) component {return [Self.values ​​objectAtIndex: line]; }  

What do I need to change / add / add to prevent this app from crashing?

Update

_usernames is an NSArray ... results below,

  NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription * entity = [NSEntityDescription EntityForName: @ Managed Object Contact in "Account": _MediaAid Object Contain]; [FetchRequest Set Entity: Entity]; FetchRequest.propertiesToFetch = [NSArray arrayWithObject: [[ObjectForKey by name of entity properties]: @ "Username"]]; NSError * Error = Zero; NSArray _usernames = [self.managedObjectContext executeFetchRequest: fetchRequest error: & amp; Error];    

casemod picture view to NSString , You give it an array of account . The structure tries to copy your example because it assumes that it is a NSString , which corresponds to the NSCopying protocol and copyWithZone: Applies. Your object is not there, and will be a [account copywitzone]: for example sent to an unrecognized selector exception

Just by using the appropriate attribute from its core data object, one of NSStrings Create an array

There are probably clever ways to do this, but this would be a clear solution:

  NSMutableArray * name = [NSMutableArray arrayWithCapacity: [_ User name count]]; (For account * account in _usernames) {NSString * accountName = account.name; If (! AccountName) {accountName = @ "& lt; Unknown account»;); } [To add names: accountName]; } CaseyMold Pictureview * PickView = [[Casey Model Picture Alcohol] Init With Values: Name];  

I have just seen that you have set the propertiesToFetch . In addition, you have to set the result type from fetchRequest to NSDictionaryResultType . In this case, executeFetchRequest: error: returns an array of dictionaries and you NSArray * names = [_usernames valueForKey: @ "username"]; should be able to use.


No comments:

Post a Comment