0

I created a class called "event". This class has three variables.

Main file initialize an instance of this class, after I parse an xml file but when I go to retrieve the created instance I did not recognize.

Where am I wrong?

#import "mkViewController.h"
#import "evento.h"

@interface mkViewController ()    
@end

@implementation mkViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://xxxxx.it/xxx.xml"];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

    NSMutableArray *datiEvento = [NSMutableArray array];
    evento *eventoTrovato = [[evento init]alloc];
}

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

    if ([element isEqualToString:@"evento"]) {        
        // Create la array dell'evento
        eventoTrovato.nome = string;  **<--- not found eventoTrovato instace**
    }    
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

You created only local variable eventoTrovato in viewDidLoad. If you want to use it not only in one method then you have to define the variable or property in your class, like:

@interface mkViewController ()    
@property (nonatomic, strong) evento *eventoTrovato;
@end

.....

- (void)viewDidLoad
{
    ....... 
    self.eventoTrovato = [evento new];
}

- (void)parser.........
{            
     .......
     _eventoTrovato.nome = string;  
}

Another thing is if you want to store many elements into array. The code will looks like:

@interface mkViewController ()    
@property (nonatomic, strong) NSMutableArray *datiEvento;
@end

- (void)viewDidLoad
{
    ....... 
    self.datiEvento = [NSMutableArray array];
}

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

    if ([element isEqualToString:@"evento"]) {  
        evento *eventoTrovato = [evento new];      
        eventoTrovato.nome = string;

        [self.datiEvento addObject:eventoTrovato];
    }    
}
kabarga
  • 803
  • 6
  • 11
  • Thank you very much for your answer, but doing so whenever it finds an element not always overwrites the same instance? If volesi that for every element found I create a new event object and adds it to me at the end in an array? – Michael Md Di Pietro Oct 18 '14 at 13:50
  • but thanks so eventoTrovato * object is not overwritten cn always find that the new data? when I go to add the array after the call and I do not find many elements all equal? – Michael Md Di Pietro Oct 18 '14 at 14:04
  • it is local instance, so you create one, set string and add to the array and keep it there. next time you add another one...If you will not find anything then [self.datiEvento count] will be 0. if you will find 5 then [self.datiEvento count] == 5. 5 elements isKindOfClass evento – kabarga Oct 18 '14 at 14:08