Parsing XML in my iPhone application - iphone

I parsing some elements from a xml file located on my server to my app.This is the code to parse all elements:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:#"ipayregs"] ) {
msgAdded = [[attributeDict objectForKey:#"added"] retain];
msgId = [[attributeDict objectForKey:#"id"] intValue];
msgNome = [[NSMutableString alloc] init];
msgName = [[NSMutableString alloc] init];
msgMatricula = [[NSMutableString alloc] init];
msgSenderCode = [[NSMutableString alloc] init];
inNome = NO;
inName = NO;
inMatricula = NO;
inSenderCode = NO;
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = YES; }
if ( [elementName isEqualToString:#"nome"] ) { inName = YES; }
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = YES; }
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = YES; }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( inNome ) { [msgNome appendString:string]; }
if ( inName ) { [msgName appendString:string]; }
if ( inMatricula ) { [msgMatricula appendString:string]; }
if ( inSenderCode ) { [msgSenderCode appendString:string]; }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:#"ipayregs"] ) {
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,#"added",msgNome,#"nome",msgMatricula,#"matricula",msgSenderCode,#"sendercode",msgName,#"name",nil]];
[[messages reverseObjectEnumerator] allObjects];
lastId = msgId;
[msgAdded release];
[msgNome release];
[msgName release];
[msgMatricula release];
[msgSenderCode release];
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = NO;}
if ( [elementName isEqualToString:#"name"] ) { inName = NO;}
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = NO;}
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = NO;}
}
As you can see all data is placed in a array to be displayed after on a UITableView,using this code:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return ( messages == nil ) ? 0 : [messages count];
}
-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:#"newsCustomCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"newsCustomCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *timeDate = (UILabel *)[cell viewWithTag:1];
timeDate.text = [itemAtIndex objectForKey:#"added"];
UILabel *userL = (UILabel *)[cell viewWithTag:2];
userL.text = [itemAtIndex objectForKey:#"nome"];
UILabel *textL2 = (UILabel *)[cell viewWithTag:3];
textL2.text = [itemAtIndex objectForKey:#"matricula"];
UILabel *textL3 = (UILabel *)[cell viewWithTag:4];
textL3.text = [itemAtIndex objectForKey:#"sendercode"];
return cell;
}
Ok,now is my problem,how can i make a UILabel display the value of the strings,per example the #"nome",be displayed in a UILabel when the view loads.It's related to this piece of code:
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
But i don't know how i can set it up.

Related

iphone- UITableView displaying only one value

My UITableView is not displaying all values after parsing XML. It returns only one row. After parsing xml, why is only one row being shown and not the whole ArrayList? I have checked my xml php page. It is showing all values fine.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[xmlcont xmlbanktransfer] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSUInteger const kMagnitudeImageTag = 0;
static NSUInteger const kLocationLabelTag = 4;
static NSUInteger const klocationtag = 6;
static NSUInteger const kacctag = 8;
static NSUInteger const knameLabelTag = 9;
UIImageView *magnitudeImage = nil;
UILabel*locationLabel = nil;
UILabel*locationtag=nil;
UILabel*acc=nil;
UILabel*nameLabel= nil;
//NSMutableArray *cont = [xmlcont tweets];
// Tweet *current       = [cont objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
/*//common settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
cell.imageView.clipsToBounds = YES;
*/
}
for(UIView *view in cell.contentView.subviews)
{
if ([view isKindOfClass:[UIView class]])
{
[view removeFromSuperview];
}
}
NSMutableArray *cont = [xmlcont xmlbanktransfer];
trans *current= [cont objectAtIndex:indexPath.row];
NSLog(#"sd %#",current.date);
NSLog(#"sd %#",current.name);
locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,10, 250, 20)];
locationLabel.tag = kLocationLabelTag;
locationLabel.font = [UIFont boldSystemFontOfSize:15];
[cell.contentView addSubview:locationLabel];
locationLabel.text=current.name;
locationLabel.textColor=[UIColor colorWithRed:0.050 green:0.278 blue:0.392 alpha:1];
//locationLabel.backgroundColor=[UIColor lightGrayColor];
locationLabel.backgroundColor=[UIColor colorWithRed:225 green:225 blue:225 alpha:0] ;
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(180,10, 250, 20)];
nameLabel.tag = knameLabelTag;
nameLabel.font = [UIFont systemFontOfSize:13];
[cell.contentView addSubview:nameLabel];
nameLabel.text=current.date ;
nameLabel.textColor=[UIColor colorWithRed:0.050 green:0.278 blue:0.392 alpha:1];
//locationLabel.backgroundColor=[UIColor lightGrayColor];
nameLabel.backgroundColor=[UIColor colorWithRed:225 green:225 blue:225 alpha:0] ;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
code for parsing xml:
xmlbanktransfer=[[NSMutableArray alloc] init];
NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
parser=[[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
NSLog(#"testing12 %# ",url);
// NSLog(#"tweets %# ", xmlbanktransfer);
NSLog(#"Total values = %d",[xmlbanktransfer count]);
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"item"])
{
currentTweet = [trans alloc];
banktransNodeContent =[[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"date"])
{
currentTweet.date = [currentTweet.date stringByReplacingOccurrencesOfString:#"&" withString:#" "];
currentTweet.date = banktransNodeContent;
}
if([elementName isEqualToString:#"name"])
{
currentTweet.name =banktransNodeContent;
}
if ([elementName isEqualToString:#"type"])
{
currentTweet.type=banktransNodeContent;
}
if ([elementName isEqualToString:#"gross"])
{
currentTweet.gross=banktransNodeContent;
}
if ([elementName isEqualToString:#"fee"])
{
currentTweet.fee=banktransNodeContent;
}
if ([elementName isEqualToString:#"net"])
{
currentTweet.net=banktransNodeContent;
NSLog(#"current tweet %#",currentTweet.net);
}
if ([elementName isEqualToString:#"category"])
{
currentTweet.category=banktransNodeContent;
NSLog(#"current tweet %#",currentTweet.category);
}
if ([elementName isEqualToString:#"account"])
{
currentTweet.account=banktransNodeContent;
NSLog(#"current tweet %#",currentTweet.account);
}
if([elementName isEqualToString:#"item"])
{
[xmlbanktransfer addObject:currentTweet];
currentTweet = nil;
banktransNodeContent = nil;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(#"currentNodeContent are %#",banktransNodeContent);
banktransNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"currentNodeContentstring test are %#",banktransNodeContent);
}
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
banktransNodeContent = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[xmlcont xmlbanktransfer] count];
}
this method tells the tableview how many cells to populate
Log the value and see how much it is returning [it must be 1 returning now,since the table shows one row].Correct the return value and you have all values displayed
Things to check
Returning value mentioned above
Make sure the array xmlbanktransfer is a property which is of strong type

not getting entire xml data into tableview

im parsing xml data into table view,here iam retrieving question and answer tags,problem occured in question tag only one question was displayed remaining questions not displayed ,this is the web service http://www.cmellc.com/DesktopModules/CaseChallenge/quiz.xml
this is the code i wrote
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentTag = elementName;
if ([currentTag isEqualToString:#"question"])
{
model = [[ModelDataView alloc]init];
}
if([currentTag isEqualToString:#"answer"])
{
model_2=[[Model2 alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"question"])
{
[dataControllerArray addObject:model];
}
if ([elementName isEqualToString:#"answer"])
{
[dataControllerArray2 addObject:model_2];
model_2 = nil;
}
currentTag = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentTag isEqualToString:#"result"])
{
model_2.result = string;
}
if ([currentTag isEqualToString:#"notes"])
{
model_2.notes = string;
}
if ([currentTag isEqualToString:#"question"])
{
model.question = string;
}
}
this is code for tableview methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
static NSString *cellIdentifier2 = #"cell2";
UITableViewCell *cell;
if(tableView.tag==2)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
Model2 *obj=[modelController.dataControllerArray2 objectAtIndex:indexPath.row];
NSString *res=obj.result;
cell.textLabel.text=res;
return cell;
}
else if(tableView.tag==1)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
}
ModelDataView *obj=[modelController.dataControllerArray objectAtIndex:indexPath.row];
NSString *res=obj.question;
UIWebView *_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 190)];
[self.view addSubview:_webView];
CGRect frame = _webView.frame;
frame.size.height = 190;
_webView.frame = frame;
[_webView loadHTMLString:res baseURL:[NSURL URLWithString:res]];
return cell;
}
}
thank you for helping me

Display vars in UILabels

I'm getting some data from a XML file on my server,here's the parse of the file:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:#"ipayregs"] ) {
msgAdded = [[attributeDict objectForKey:#"added"] retain];
msgId = [[attributeDict objectForKey:#"id"] intValue];
msgNome = [[NSMutableString alloc] init];
msgName = [[NSMutableString alloc] init];
msgMatricula = [[NSMutableString alloc] init];
msgSenderCode = [[NSMutableString alloc] init];
inNome = NO;
inName = NO;
inMatricula = NO;
inSenderCode = NO;
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = YES; }
if ( [elementName isEqualToString:#"nome"] ) { inName = YES; }
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = YES; }
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = YES; }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( inNome ) { [msgNome appendString:string]; }
if ( inName ) { [msgName appendString:string]; }
if ( inMatricula ) { [msgMatricula appendString:string]; }
if ( inSenderCode ) { [msgSenderCode appendString:string]; }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:#"ipayregs"] ) {
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,#"added",msgNome,#"nome",msgMatricula,#"matricula",msgSenderCode,#"sendercode",msgName,#"name",nil]];
[[messages reverseObjectEnumerator] allObjects];
lastId = msgId;
[msgAdded release];
[msgNome release];
[msgName release];
[msgMatricula release];
[msgSenderCode release];
}
if ( [elementName isEqualToString:#"nome"] ) { inNome = NO;}
if ( [elementName isEqualToString:#"name"] ) { inName = NO;}
if ( [elementName isEqualToString:#"matricula"] ) { inMatricula = NO;}
if ( [elementName isEqualToString:#"sendercode"] ) { inSenderCode = NO;}
}
As you can notice,the data downloaded is stored in vars(#"nome",#"matricula"..).But how can i display the values in UILabels,like | label.text = #"nome" |?
it looks like the data is actually being stored in msgNome. #"nome" is a string expression, not an instance variable. So, you would set label.text = msgNome;
also, you should just have:
inNome = [elementName isEqualToString:#"nome"];
instead of all the extra if statements.

Display dynamic data in UIPickerView in iPhone

I am using NSXMLParsing and UIPickerView in my application but i am not able to display
data in uipicker view after parsing. can anyone help me out about this problem.
I just want to know that how to store data in array and display it in uipickerview.
thnx frnds
Thanks mahboudz... I have done that what u said but its not working.. actually i have parsed some data and stored it in nsmutablearray using dictionary... and want to display it in uipicker view but its not working.. my code is:-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:#"news"] ) {
//msgAdded = [[attributeDict objectForKey:#"added"] retain];//fetch session from message tag.
//msgId = [[attributeDict objectForKey:#"id"] intValue];//fetch id from message tag.
newId = [[NSMutableString alloc] init];
newsTitle =[[NSMutableString alloc]init];
newsOverview = [[NSMutableString alloc]init];
newsOfDay = [[NSMutableString alloc] init];
createdOn = [[NSMutableString alloc]init];
newsDescription = [[NSMutableString alloc]init];
idNews = NO;
ttlNews = NO;
overvwNews = NO;
dyNews = NO;
onCreated = NO;
desNews = NO;
//inFromUserName = NO;
}
if ( [elementName isEqualToString:#"news_id"] ) {
idNews = YES;
}
if ( [elementName isEqualToString:#"news_title"] ) {
ttlNews = YES;
}
if ( [elementName isEqualToString:#"news_overview"] ) {
overvwNews = YES;
}
if ( [elementName isEqualToString:#"is_news_of_the_day"] ) {
dyNews = YES;
}
if ( [elementName isEqualToString:#"created_on"] ) {
onCreated = YES;
}
if ( [elementName isEqualToString:#"news_description"] ) {
desNews = YES;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( idNews ) {
[newId appendString:string];
}
if (ttlNews) {
[newsTitle appendString:string];
}
if ( overvwNews ) {
[newsOverview appendString:string];
//NSLog(#"USER_NAME ===== %d",fromUserName);
}
if (dyNews) {
[newsOfDay appendString:string];
}
if (onCreated) {
[createdOn appendString:string];
}
if (desNews) {
[newsDescription appendString:string];
// NSLog(#"MESSAGE ==== %#",chatComment);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:#"news"] ) {
//[news addObject:[NSDictionary dictionaryWithObjectsAndKeys:chatSession,#"chatSession",fromUserName,#"fromUserName",chatComment,#"chatComment",nil]];
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:newsTitle,#"title",newId,#"id",nil]];
}
if ( [elementName isEqualToString:#"news_id"] ) {
idNews = NO;
}
if ( [elementName isEqualToString:#"news_title"] ) {
ttlNews = NO;
}
if ( [elementName isEqualToString:#"news_overview"] ) {
overvwNews = NO;
}
if ( [elementName isEqualToString:#"is_news_of_the_day"] ) {
dyNews = NO;
}
if ( [elementName isEqualToString:#"created_on"] ) {
onCreated = NO;
}
if ( [elementName isEqualToString:#"news_description"] ) {
desNews = NO;
}
}
and now i want to display the contents of array in uipicker view. that is objectForKey:#"newstitle"
i have tried it display through following code:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *partyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
partyLabel.text= [[messages objectAtIndex:row]objectForKey:#"title"];
partyLabel.font = [UIFont boldSystemFontOfSize:13];
partyLabel.textAlignment = UITextAlignmentCenter;
partyLabel.backgroundColor = [UIColor clearColor];
return partyLabel;
}
but its not working ...please can any one help me out..
Thnx
Kunal
You are free to store the parsed data in whatever format you wish, such as an NSArray or a C array. When the UIPicker needs to display each row, it will call one of the following, depending on how you are managing the Picker:
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
You need to implement only one of the two. The first one is easiest. When you get called for each row, then just return the NSString that you would like to have displayed in that row. The second one is more complex, but allows you to have more flexibility such as formatting the row or adding images.
If you are changing the display data after the UIPicker is on screen, you will need to call reloadComponent: or reloadAllComponents to update the display.
I think that at first you have to parse the xml and store data into an nsmutable array, then you have to display data in picker view from your array

Need help with memory leaks in RSS Reader

I'm trying to write a simple RSS reader for the iPhone, and it appeared to be working fine, until I started working with Instruments, and discovered my App is leaking massive amounts of memory.
I'm using the NSXMLParser class to parse an RSS feed. My memory leaks appear to be originating from the overridden delegate methods:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
and
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
I'm also suspicious of the code that populates the cells from my parsed data, I've included the code from those methods and a few other key ones, any insights would be greatly appreciated.
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.currentElement isEqualToString:#"title"]) {
[self.currentTitle appendString:string];
} else if ([self.currentElement isEqualToString:#"link"]) {
[self.currentURL appendString:string];
} else if ([self.currentElement isEqualToString:#"description"]) {
[self.currentSummary appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
//asdf
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentURL forKey:#"URL"];
[item setObject:currentSummary forKey:#"summary"];
[self.currentTitle release];
[self.currentURL release];
[self.currentSummary release];
[self.stories addObject:item];
[item release];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
// Set up the cell
int index = [indexPath indexAtPosition: [indexPath length] - 1];
CGRect contentRect = CGRectMake(8.0, 4.0, 260, 20);
UILabel *textLabel = [[UILabel alloc] initWithFrame:contentRect];
if (self.currentLevel == 0) {
textLabel.text = [self.categories objectAtIndex: index];
} else {
textLabel.text = [[self.stories objectAtIndex: index] objectForKey:#"title"];
}
textLabel.textColor = [UIColor blackColor];
textLabel.font = [UIFont boldSystemFontOfSize:14];
[[cell contentView] addSubview: textLabel];
//[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: #"title"]];
[textLabel autorelease];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"item"]) {
self.currentTitle = [[NSMutableString alloc] init];
self.currentURL = [[NSMutableString alloc] init];
self.currentSummary = [[NSMutableString alloc] init];
}
if (currentElement != nil) {
[self.currentElement release];
}
self.currentElement = [elementName copy];
}
- (void)dealloc {
[currentElement release];
[currentTitle release];
[currentURL release];
[currentSummary release];
[currentDate release];
[stories release];
[rssParser release];
[storyTable release];
[super dealloc];
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
int index = [indexPath indexAtPosition: [indexPath length] - 1];
if (currentLevel == 1) {
StoryViewController *storyViewController = [[StoryViewController alloc] initWithURL:[[stories objectAtIndex: index] objectForKey:#"URL"] nibName:#"StoryViewController" bundle:nil];
[self.navigationController pushViewController:storyViewController animated:YES];
[storyViewController release];
} else {
RootViewController *rvController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
rvController.currentLevel = currentLevel + 1;
rvController.rssIndex = index;
[self.navigationController pushViewController:rvController animated:YES];
[rvController release];
}
}
I figured out my problem, all of my memory leaks stemmed from this statement:
self.stories = [[NSMutableArray alloc] init];
This causes the retain count of stories to be incremented by 2, since the setter calls retain on the newly allocated array.
I replaced the above statement with this one and it solved my problem:
NSMutableArray *array = [[NSMutableArray alloc] init];
self.stories = array;
[array release];
Another way of fixing your code is this replacing
self.stories = [NSMutableArray alloc] init];
with
self.stories = [NSMutableArray arrayWithCapacity:10];
The arrayWithCapacity method is autoreleased so you don't need to manually call release. (This is true for other classes i.e. setWithCapacity, stringWithFormat etc)
Thanks, Sam
PS Not helping your question but these lines look a little unusual :
[self.currentTitle release];
You should probably be doing this :
self.currentTitle = nil;
That will release currentTitle the same as your code does but it will also set it to nil which means you can't use it again by mistake!