0

Here, i have a NSMutableArray in my AppDelegate page, the array will be updated when the user add the values (user name) in the database. Now i want the array values(user name) to be displayed in the next view to be list out in the Picker View. I know there is more related questions is there, but i can't find the solution. Any help appreciated.

Here is my code:

The array i have declared in the AppDelegate page, named jobarray, now i need to copy the items in the jobarray into next view, there i have declared pickerarray.

pickerarray = [[NSMutableArray alloc] initWithArray:appDelegate.jobarray copyItems:YES];

But it returns error message,

['NSInvalidArgumentException', reason: '-[Coffee copyWithZone:]: unrecognized selector sent to instance 0x822d410']     where 'Coffee' is my sqlite3 object file.
Bista
  • 7,869
  • 3
  • 27
  • 55
jireh
  • 89
  • 1
  • 2
  • 11
  • Have a look @ the answer in http://stackoverflow.com/questions/2976910/problem-copying-nsmutablearray. – Neo Aug 03 '12 at 06:41
  • Thanks for your link, i have reviewed it and i change [ Copyitems : YES] to 'NO'. And i am getting this error, -[Coffee isEqualToString:]: unrecognized selector sent to instance 0x8524fa0. Please advice on this. – jireh Aug 03 '12 at 06:50

3 Answers3

1

Your SQLite object does not conform to the NSCopying protocol - you haven't implemented the

- (id)copyWithZone:(NSZone *)zone;

method. You can either implement this method to make your object copyable, but if you only need the copying for displaying, you'd better copy the array itself only:

NSMutableArray *newArray = [originalArray mutableCopy];

this only retains the first array's objects, no need to mess with implementing the copying protocol.

  • Thanks for your answer (H2CO3), can you please explain some more on how to make the 'jobarray' (mentioned in my post) itself copy. – jireh Aug 03 '12 at 06:48
  • @jireh as I said, you have to implement the copyWithZone: method. –  Aug 03 '12 at 07:07
  • can you please give me some code for making array inside 'copyWithZone: method'. – jireh Aug 03 '12 at 13:06
  • @jireh just go for my 2nd method :) `NSMutableArray *newArray = [jobarray mutableCopy];` –  Aug 03 '12 at 13:11
  • hi @H2CO3 i will try it, and let you knw. Thanks for you reply. – jireh Aug 03 '12 at 13:30
  • I'm having almost the same issue. Can you help me? Here's the question I posted: http://stackoverflow.com/questions/14301364/error-reading-copied-nsmutablearray-on-iphone-sdk – jaytrixz Jan 13 '13 at 09:55
0
[arr1 initWithObjects:@"1",@"2", nil];

makes no sense, what you probably want is to use the NSMutableArray:

NSArray *arr1 = [NSArray arrayWithObjects:@"1",@"2", nil];
NSMutableArray *arr2 = [arr1 mutableCopy];

NSLog(@"arr1: %p, %@", arr1, arr1);
NSLog(@"arr2: %p, %@", arr2, arr2);

NSLog output:

arr1: 0x7924c50, (
                 1,
                 2
                 )
arr2: 0x7924f00, (
                 1,
                 2
                 )
Vishal
  • 556
  • 4
  • 18
-1

To populate a UIPickerView from an array you need to first create a connection for the UIPickeView Data Source and Delegate back to the ViewController. You can do this by CTRL dragging in Interface Builder in the normal way - from your UIPickerView control.

You will also need to ensure that you include the delegate protocal to your interface file. So something like this;

@ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> {  

}

Then you will need to create the delegated methods for the population of the UIPickerView in your implementation file.

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
 {
    //One column
    return 1;
 }

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 {
   //set number of rows
     return myArrayObject.count;
 }

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
 {
     //set item per row
     return [myArrayObject objectAtIndex:row];
 }

This assumes that myArrayObject has been properly declared and populated elsewhere. I.e. in viewDidLoad - if content is static.

ross_t
  • 511
  • 5
  • 10
  • Ya i have declared datasource and delegate both in the xib and in the header file but it doesn't work. – jireh Aug 03 '12 at 06:56
  • I apologise as I think I may have interpreted your question wrong. I assumed you were asking how to populate a UIPickerView from an array, but I think in fact you are asking how to copy the contents of an array to another? – ross_t Aug 03 '12 at 07:04
  • Why do you need to copy to a new array? How come you can't just use jobArray as the datasource to your pickerView? – ross_t Aug 03 '12 at 07:05
  • Sorry if this is a silly question, but are using ARC (automatic memory management) in your project? – ross_t Aug 03 '12 at 07:09
  • if i populate the picker view with jobarray(appDelegate.jobarray) in the numberofRowsInPickerView, it returns ' 0 object' in the array, but there is a values inside the array, and it's not showing. – jireh Aug 03 '12 at 07:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14836/discussion-between-jireh-and-ross-t) – jireh Aug 03 '12 at 07:16
  • Sorry I can not access chat at present due to the proxy I'm behind. I fear you've exhausted my ability to answer your question any further anyway, so you might be better off chatting with H2CO3 who is on the right track in terms of the copy approach. – ross_t Aug 03 '12 at 07:26