Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTom Coomer
1,331 PointsPicker view not updating textview
I have an array that is created from a Parse query. The NSLog displays the correct information however It crashes when I ask it to display in the textview. I am also able to have the textview display a set string (on the 3rd line that is commented out). Any ideas? Here is my code below:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self->documentContentString = [self->contentArrayPicker objectAtIndex:row];
//self->documentContentString = @"test";
NSLog(@"%@",self->documentContentString);
self->documentContent.text = self->documentContentString;
}
Tom Coomer
1,331 PointsIt is declared in the ViewController.h file
@interface ViewController : UIViewController {
IBOutlet UITextField *documentName;
IBOutlet UITextView *documentContent;
//Picker
IBOutlet UIPickerView *documentPicker;
NSArray *documentPickerData;
NSArray *documentArray;
NSArray *contentArrayPicker;
NSString *documentNameString;
NSString *documentContentString;
}
2 Answers
John Wheal
27,969 PointsAt a quick glance I can't see anything wrong with the code. I think contentArrayPicker must be populated with something other than NSString. I'm not understanding why you are using a separate array, can you not simply write?
self->documentContentString = [pickerView objectAtIndex:row];
If this doesn't work, can you provide the code that populates contentArrayPicker.
Tom Coomer
1,331 PointsI am using a separate array as the picker view title is populated by a Parse query array of all the @"title" properties and the document content string from an array filled from the @"body" property from a Parse query.
The thing that I find odd is that the NSLog line prints the correct value for the documentContentString. It only crashes when it tries to fill the textview with the documentContentString value.
John Wheal
27,969 PointsI probably should have asked this first, but when you say "crashes" what error message is it giving? I'm guessing it's something to do with incompatible type.
Tom Coomer
1,331 Points[PFObject copyWithZone:]: unrecognized selector sent to instance 0x170119aa0 (lldb)
I have an Exception breakpoint and it points to the line below:
self->documentContent.text = self->documentContentString;
Thanks
John Wheal
27,969 PointsThat message is saying that documentContentString is a PFObject and is not an NSString object for some reason. I'm not sure how this is happening unless you are using casting when you populate contentArrayPicker. Make sure you are defiantly populating contentArrayPicker with valueForKey:@"body".
If this doesn't work I'll need to see the code that populates contentArrayPicker.
Tom Coomer
1,331 PointsThis is the code that populates the contentArrayPicker:
PFQuery *contentQuery = [PFQuery queryWithClassName:@"Post"];
[contentQuery selectKeys:@[@"body"]];
NSArray *contentArray = [contentQuery findObjects];
NSLog(@"%@",contentArray);
[contentQuery findObjectsInBackgroundWithBlock:^(NSArray *contentObjects, NSError *contentError) {
}];
self->contentArrayPicker = contentArray;
John Wheal
27,969 PointsAlternatively try:
self->documentContent.text = [self->documentContentString valueForKey:@"body"];
This code shouldn't work unless something very strange is happening.
Tom Coomer
1,331 PointsThat has worked! Thank you so much for your help!
John Wheal
27,969 PointsThere are multiple things going wrong here. You are making two network calls and not getting the valueForKey. contentArray will be full of PFObjects and not NSStrings. You were also copying the array memory address and not the contents. Try the following code:
PFQuery *contentQuery = [PFQuery queryWithClassName:@"Post"];
[contentQuery selectKeys:@[@"body"]];
NSMutableArray *contentArray = [NSMutableArray array];
[contentQuery findObjectsInBackgroundWithBlock:^(NSArray *contentObjects, NSError *contentError) {
for (PFObject *contentObject in contentObjects) {
[contentObject addObject:[contentObjects valueForKey:@"body"]];
}
}];
self->contentArrayPicker = [contentArray copy];
John Wheal
27,969 PointsI've only just seen your message that it worked. That's great but it's only working through a combination of mistakes. Anyway, at least it's sorted.
Tom Coomer
1,331 PointsOkay. I will take a look at improving if. Thanks again.
John Wheal
27,969 PointsJohn Wheal
27,969 PointsCan you please provide the declaration of self->documentContentString and self->contentArrayPicker