Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:25:22+00:00 2026-06-18T02:25:22+00:00

I’m writing an app for iOS and i have to read an external xml

  • 0

I’m writing an app for iOS and i have to read an external xml file. My program logic is working very well. I checked that several times with NSLog messages. My problem is, when I’m adding an new object to my NSMutuableArray masterNewsList then is after the insertion every object overwritten by the last inserted object. What’s my mistake here? I can’t find it.

//  NewsData.h

#import <Foundation/Foundation.h>

@interface NewsData : NSObject

@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *date;
@property (nonatomic,copy) NSString *detail;
@property (nonatomic,copy) NSString *content;

-(id) initWithTitle:(NSString *)title  date:(NSString *)date detail:(NSString *)detail content:(NSString *)content;

@end


//NewsData.m

#import "NewsData.h"

@implementation NewsData

-(id)initWithTitle:(NSString *)title date:(NSString *)date detail:(NSString *)detail content:(NSString *)content{
 self = [super init];
 if (self) {
    _title = title;
    _date = date;
    _detail = detail;
    _content = content;
    return self;
}
return nil;
}

@end



//  NewsDataController.h


#import <Foundation/Foundation.h>

@class NewsData;

@interface NewsDataController : NSObject <NSXMLParserDelegate>

@property (nonatomic, copy) NSMutableArray *masterNewsList;

- (NSUInteger)countOfList;
- (NewsData *)objectInListAtIndex:(NSUInteger)theIndex;

@end



//  NewsDataController.m


#import "NewsDataController.h"
#import "NewsData.h"

@interface NewsDataController()

@property NSMutableString *title;
@property NSMutableString *description;
@property NSMutableString *content;
@property NSMutableString *date;

@property BOOL itemValue;
@property BOOL titleValue;
@property BOOL descriptionValue;
@property BOOL contentValue;
@property BOOL dateValue;

-(void) initializeDataList;
- (void)addNewsData:(NewsData *)newsData;

@end

@implementation NewsDataController

- (void) initializeDataList {
    NSMutableArray *newsList = [NSMutableArray array];
    self.masterNewsList = newsList;

    self.title = [NSMutableString stringWithString:@""];
    self.description = [NSMutableString stringWithString:@""];
    self.content = [NSMutableString stringWithString:@""];
    self.date = [NSMutableString stringWithString:@""];

    self.itemValue = false;
    self.contentValue = false;
    self.dateValue = false;
    self.titleValue = false;
    self.descriptionValue = false;

    NSData *xmlData = nil;
    xmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.somesite.de/?type=100"]];
    if (xmlData != nil) {
        NSXMLParser *theParser = [[NSXMLParser alloc] initWithData:xmlData];
        theParser.delegate = self;
        [theParser parse];
    }
     else{
        NewsData *newsData;
        newsData = [[NewsData alloc] initWithTitle:@"Es konnten keine News geladen werden" date: @"---" detail:@"Keine Verbindung zum Server" content:@"Bitte Netzwerkverbindung überprüfen!"];
        [self addNewsData:newsData];
    }
}

-(void) setMasterNewsList:(NSMutableArray *)newList{
    if (_masterNewsList != newList) {
        _masterNewsList = [newList mutableCopy];
    }
}

-(id) init{
    if (self = [super init]) {
        [self initializeDataList];
        return self;
    }
    return nil;
}

- (NSUInteger) countOfList{

    return [self.masterNewsList count];
}

- (NewsData *)objectInListAtIndex:(NSUInteger)theIndex{

    return [self.masterNewsList objectAtIndex:theIndex];
}

Method who add the object to masterNewsList

- (void) addNewsData:(NewsData *)newsData{

    [self.masterNewsList addObject:newsData];

}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    if ([elementName isEqualToString:@"item"]) {
        self.itemValue = true;
    }
    if ([elementName isEqualToString:@"title"]) {
        [self.title deleteCharactersInRange:NSMakeRange(0, self.title.length)];
        self.titleValue = true;
    }
    if ([elementName isEqualToString:@"description"]) {
        [self.description deleteCharactersInRange:NSMakeRange(0, self.description.length)];
        self.descriptionValue = true;
    }
    if ([elementName isEqualToString:@"content:encoded"]) {
        [self.content deleteCharactersInRange:NSMakeRange(0, self.content.length)];
        self.contentValue = true;
    }
    if ([elementName isEqualToString:@"pubDate"]) {
        [self.date deleteCharactersInRange:NSMakeRange(0, self.date.length)];
        self.dateValue = true;
    }
 }

Here I’m creating the new object of kind NewsData and calling then the addNewsData Method

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"item"]) {
        NewsData *newsData;
        newsData = [[NewsData alloc] initWithTitle:self.title date:self.date detail:self.description content:self.content];
        [self addNewsData:newsData];
        self.itemValue = false;
    }
    if ([elementName isEqualToString:@"title"]) {
        self.titleValue = false;
    }
    if ([elementName isEqualToString:@"description"]) {
        self.descriptionValue = false;
    }
    if ([elementName isEqualToString:@"content:encoded"]) {
        self.contentValue = false;
    }
    if ([elementName isEqualToString:@"pubDate"]) {
        self.dateValue = false;
    }
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (self.itemValue && self.titleValue) {
        [self.title appendString:string];
    }
    if (self.itemValue && self.descriptionValue) {
        [self.description appendString:string];
    }
    if (self.itemValue && self.contentValue) {
        [self.content appendString:string];
    }
    if (self.itemValue && self.dateValue) {
        [self.date appendString:string];
    }
}

-(void) parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
    if (self.itemValue && self.contentValue) {
        [self.content appendString:[[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]];
    }
}



@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:25:23+00:00Added an answer on June 18, 2026 at 2:25 am

    Your mistake is here:

    if (self) {
        // None of these assignments copies the incoming mutable strings.
        // When strings change later on, so do titles, details, content, and so on.
        _title = title;
        _date = date;
        _detail = detail;
        _content = content;
        return self;
    }
    

    You are using assignments to backing variables of properties marked copy. Switch to assigning to properties, and your problem will be fixed:

    if (self) {
        // Since your property is correctly marked `copy` (a good idea for NSString)
        // these assignments will make copies of mutable strings,
        // preventing the unwanted modifications.
        self.title = title;
        self.date = date;
        self.detail = detail;
        self.content = content;
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am writing an app with both english and french support. The app requests
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am writing an app for my school newspaper, which is run completely online
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.