iPhone Exception throw [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
this class is not key value coding-compliant for the key
I am facing this type of problem as when I am creating an application in XCode for iphone .
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x4d2eb20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key singlePicker.'
*** Call stack at first throw:

you are setting a value for a NULL Key
Edit:
befor you start using the picker you need to:
in the .h file:
UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
NSArray *array;
}
then in the .m file:
-(void)viewDidload{
[super viewDidload];
array = [[NSArray alloc]initWithObjects:#"Lake",#"Diana",#"Jone",#"Alice",#"Byber",#"Nuces",nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
/* do what you want if the picker selected */
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}

Related

error while selecting row in a picker view iphone

i want to show picker view with years , i am storing year array in my userDC entitly class but i am getting an error on log message
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
userdc = [years objectAtIndex:row];
NSLog(#"%d",userdc.user_year);
}
my error is like this
-[__NSCFString user_year]: unrecognized selector sent to instance 0xaaa7220
If your user_year is string then
NSLog(#"%#",userdc.user_year);
And if it is integer then
NSLog(#"%#",[NSString stringWithFormat:#"%d",userdc.user_year]);
ANd if your userdc is dictionary then use
NSLog(#"%#",[NSString stringWithFormat:#"%d",[userdc objectForKey:#"user_year"]);
yeppii .. i solved my own problem :):) by doing this, thanx all who helped me, cheers
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
userdc=[[userDC alloc]init];
userdc.user_year = [years objectAtIndex:row];
NSLog(#"%#",userdc.user_year);
}

UIPickerView with empty arrays

I've set up a UIPickerView successfully, but I'm wondering about the best way to handle the case when the data is initially empty. For my current iteration I use the following code for the pickerdelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([flash_cards count])
{
return [[flash_cards allKeys] objectAtIndex:row];
}
return #"";
}
where
flash_cards = NSMutableDictionary *
Whenever I try to scroll it creates an error of indexoutofbounds which is only logical. How do I handle the case of an empty array?
EDIT:
Also as part of the code I implemented the following
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponen: (NSInteger)component
{
return [flash_cards count];
}
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
To solve this problem you should return 1 instead 0 in the method numberOfRowsInComponent from the UIPickerViewDataSource when the array is empty, like the example below:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.myArray count] == 0 ? 1 : [self.myArray count];
}
You should also implement the UIPickerViewDataSource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
You can still use flash_cards (or [flash_cards count]) to derive the return values of these methods; if your data is empty, you can return 0, then later call -reloadAllComponents on your picker view when you get data loaded.

UIPickerView NSUnknownKeyException [duplicate]

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
I've seen a lot of similar questions but none of them solve my problem or made the solution clear to me. I've a made a nib file with a UIPickerView.
When I run the application I get this error.
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<UIApplication 0x68633e0> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key pickerview.'
My ViewController.h
#import <UIKit/UIKit.h>
#define CATEGORY 0
#define VALUES 1
#interface ViewController : UIViewController <UIPickerViewDelegate , UIPickerViewDataSource> {
NSDictionary* dictionary;
NSArray *keys,*values;
IBOutlet UIPickerView *pickerview;
}
#property (retain,nonatomic) NSArray *keys, *values;
#property (retain,nonatomic) NSDictionary *dictionary;
#property (retain,nonatomic) UIPickerView *pickerview;
#end
My ViewController.m
#import "ViewController.h"
#implementation ViewController
#synthesize keys,values,dictionary;
#synthesize pickerview;
-(void)dealloc {
[keys release];
[values release];
[dictionary release];
[pickerview release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSBundle* bundle = [NSBundle mainBundle];
NSString* str = [bundle pathForResource:#"testlist" ofType:#"plist"];
NSDictionary* tempd = [[NSDictionary alloc] initWithContentsOfFile:str];
self.dictionary = tempd;
[tempd release];
self.keys = [dictionary allKeys];
self.values = [dictionary objectForKey: [keys objectAtIndex:0]];
[super viewDidLoad];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == CATEGORY) {
return keys.count;
}
return values.count;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == CATEGORY) {
NSArray* tvalues = [dictionary objectForKey:[keys objectAtIndex:row]];
self.values = tvalues;
[pickerview selectRow:0 inComponent:VALUES animated:YES];//Xreiazetai?
[pickerview reloadComponent:VALUES];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == CATEGORY) {
return [keys objectAtIndex:row];
}
return [values objectAtIndex:row];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I made sure that dataSource , delegate outlets of the Picker are pointing to the File's Owner as also the pickerview referencing outlet.
UPDATE
The whole stack trace
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 1236.
2012-04-18 17:07:33.444 TestPlist[1236:f803] *** Terminating app due
to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x6a68e90> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key pickerview.'
*** First throw call stack:
(0x13bb052 0x154cd0a 0x13baf11 0x9b2032 0x923f7b 0x923eeb 0x93ed60 0x23191a 0x13bce1a
0x1326821 0x23046e 0x232010 0x1214a 0x12461 0x117c0 0x20743 0x211f8 0x14aa9 0x12a5fa9 0x138f1c5
0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x112a7 0x12a9b 0x20a2 0x2015)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
(gdb)
Here is the file for anyone who wonders what is the problem.
I'll appreciate a lot.
Go into the TestPlist-Info.plist file in your project and remove this line:
Main nib file base name ViewController
That's incorrect, you do not need this reference as it's all handled in your application:didFinishLaunchingWithOptions: method.
I'm not much of an IB user, but shouldn't you define the property as IBOutlet, not the attribute itself?
#property (retain,nonatomic) IBOutlet UIPickerView *pickerview;
And why are you not using ARC for memory management - that simplifies things a lot (no more retain and release), so if you don't have a particular reason for not using it you should switch to ARC.

UIPicker crashing

My UIPicker is crashing if the NSArray of objects is greater than 3, with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAutoreleasePool pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance
Here is my code for the functions:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.glassPickerOptions = [[NSArray alloc] initWithObjects:#"3mm",#"4mm",#"DG4+4",#"DG4+6",nil];
[glassPicker setFrame:CGRectMake(0, 0, 320, 162)];
[glassPicker selectRow:1 inComponent:0 animated:NO];
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger glassPickerOptionsCount = self.glassPickerOptions.count;
NSLog(#"%i", glassPickerOptionsCount);
return glassPickerOptionsCount;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return (NSString*) [self.glassPickerOptions objectAtIndex:row];
}
Hopefully I havent missed anything. Thanks in advance
It seems that you overrelease your picker view, you can see this because the message is being sent to an autoreleasepool and not the object you expect, you should check out your retain/releases for your picker see whats going on, cant really tell from the code posted...

iPhone SDK: NSInvalidArgumentException -UIView numberOfComponentsInPickerView

I am trying to develop a simple search mechanism on a UIPicker. The approach I am using is to keep two arrays. My problem is that for some reason I am getting this run-time error.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView numberOfComponentsInPickerView:]
Here are the array declarations.
//data source for UIPicker NSArray
*arrayCountryChoices;
//search results buffer
NSMutableArray *arraySearchResults;
//properties
#property(nonatomic,retain) NSArray*arrayCountryChoices;
#property(nonatomic,retain) NSMutableArray *arraySearchResults;
Here is where I initialize the data
//create data
arrayCountryChoices = [NSArray arrayWithObjects:#"foo",#"bar",#"baz",nil];
//copy the original array to searchable array
arraySearchResults = [arrayCountryChoices mutableCopy];
An the picker methods.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arraySearchResults count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arraySearchResults objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//grab the selected country
strUserChoice = [arraySearchResults objectAtIndex:row];
}
Here is the search code for completeness although not really relevant yet as the app dies before we ever get here.
//filter on search term
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", strSearchText];
[arraySearchResults filterUsingPredicate: predicate];
[pickerCountry reloadComponent:0];
I have also dragged datasource and delegate connections from the UIPicker to Files Owner in Interface Builder.
Any ideas?
Thanks in advance.
Looks like you've got the picker's data source set to something other than the object that implements the code you've posted there—apparently a UIView somewhere. Make sure the picker's outlets point to your actual data-source object.