I am unable to populate the previewData NSMutableArray with this code. XML request is successfully retrieved, parsed but I am unable to populate the previewData array for tableviewController.
PreviewsController.h code:
#import <UIKit/UIKit.h>
@interface PreviewsController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
UITableView *previewList;
NSString *enteredUsername;
NSString *enteredPassword;
NSMutableString *current_xml_element;
NSMutableString *current_xml_value;
NSMutableDictionary *item;
NSMutableArray *previewData;
}
@property (nonatomic, retain) IBOutlet UITableView *previewList;
@property (nonatomic, retain) NSString *enteredUsername;
@property (nonatomic, retain) NSString *enteredPassword;
@property (nonatomic, retain) NSMutableString *current_xml_element;
@property (nonatomic, retain) NSMutableString *current_xml_value;
@property (nonatomic, retain) NSMutableArray *previewData;
@property (nonatomic, retain) NSMutableDictionary *item;
@end
PreviewsController.m code:
#import "PreviewsController.h"
@implementation PreviewsController
@synthesize previewList;
@synthesize enteredUsername, enteredPassword;
@synthesize current_xml_element, previewData, item, current_xml_value;
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.previewData = nil;
[super viewDidUnload];
}
- (void)dealloc {
[item release];
[previewData release];
[enteredUsername release];
[enteredPassword release];
[current_xml_element release];
[super dealloc];
}
- (void)viewDidLoad {
previewData = [[NSMutableArray alloc] init];
// Fetch the XML output from API - Start
NSString *APIURL = [[NSString alloc] initWithFormat:@"http://mysite.com/api/%@/%@", enteredUsername, enteredPassword];
NSURL *url = [[NSURL alloc] initWithString:APIURL];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[APIURL release];
[url release];
[xmlParser setDelegate:self];
[xmlParser parse];
// Fetch the XML output from API - End
[super viewDidLoad];
}
#pragma mark -
#pragma mark XML Request Methods
-(void)parserDidStartDocument:(NSXMLParser *)parser {
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
current_xml_element = [elementName copy];
if ([elementName isEqualToString:@"subject"]) {
item = [[[NSMutableDictionary alloc] init] autorelease];
current_xml_value = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([current_xml_element isEqualToString:@"subject"]) {
[current_xml_value appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"subject"]) {
[item setObject:current_xml_value forKey:@"subject"];
[previewData addObject:[item copy]];
// NSLog(@"array count: %@", [previewData count]);
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser {
}
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"Error occurred: %d - %@", [parseError code], [parseError localizedDescription]);
}
#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [previewData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [previewData objectAtIndex:row];
return cell;
}
@end
When I build and run this code, the following error is returned in console:
2009-10-15 23:13:55.296 PreviewMyEmail[29964:207] *** -[NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x3839a60
2009-10-15 23:13:55.298 PreviewMyEmail[29964:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x3839a60'
2009-10-15 23:13:55.298 PreviewMyEmail[29964:207] Stack: (
29307995,
2531364681,
29689915,
29259382,
29112002,
3812419,
13553,
3063392,
3029655,
3106211,
3070291,
55820976,
55820399,
55818438,
55817530,
55851064,
29094482,
29091423,
29088840,
37398413,
37398610,
2781187,
8436,
8290
)
Where am I doing wrong? Thanks for all your help.
Cheers.
[item copy]
returns a retained object, which you lose the reference to by immediately adding it topreviewData
– Dave DeLong[[item copy] autorelease]
you don't retain that extra reference. – Alex