Why I lose my NSOrderedSet information to call another function

145 Views Asked by At

why the NSOrderedSet information is deleted or information sometimes goes away?

in the application I get a string with data in JSON format so I use a NSOrderedSet to sort the data according to the information I receive from the key as shown in method "finishedReceivingData" but when I take that NSOrderedSet to fill the list PickerVied the information stored in the NSOrderedSet cleared trash or other information appears.

thank someone who can tell me why the information is being deleted when calling the NSOrderedSet in other metos like when I call the method of pickerView "numberOfRowsInComponent"

.h

#import <UIKit/UIKit.h>
#import "RestConnectionDelegate.h"
#import "ViewController.h"

@class RestConnection;

@interface attentionCenterViewController : UIViewController<RestConnectionDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{
    @private RestConnection *restConnection;
    IBOutlet UIPickerView *pvList;

    NSDictionary *json;
    NSData *encodData;
}
- (IBAction)cbDepto:(id)sender;
- (IBAction)cbCity:(id)sender;
- (IBAction)cbAttent:(id)sender;
- (IBAction)cbFind:(id)sender;
 @property (strong, nonatomic) NSOrderedSet *deptoArray;

@end

.m

#import "attentionCenterViewController.h"
#import "RestConnection.h"

@interface attentionCenterViewController (){
    NSString *service;
}

@end

@implementation attentionCenterViewController

#define baseURL @"http://another url"
#pragma mark Setup and Teardown Methods

- (void)viewDidLoad {
    [super viewDidLoad];
    restConnection = [RestConnection new];
    restConnection.baseURLString = baseURL;
    restConnection.delegate = self;
    viewList.hidden=true;
}

- (void)dealloc {
    [restConnection release];
    [txtDepto release];
    [txtCity release];
    [txtAttent release];
    [viewList release];
    [pvList release];
    [super dealloc];
}


#pragma mark RestConnectionDelegate

- (void)finishedReceivingData:(NSData *)data
{
    encodData = [[restConnection stringData] dataUsingEncoding:NSUTF8StringEncoding];
    json = [NSJSONSerialization JSONObjectWithData:encodData options:0 error:nil];
    NSLog(@"Diccionario JSON: %@", json);
    if ([service isEqual:@"Depto"]) {

        //In this place full array

        _deptoArray=[NSOrderedSet orderedSetWithArray:[json valueForKey:@"Nombre"]];
        NSLog(@"solo nombre: %@", _deptoArray);
        pvList.delegate=self;
        viewList.hidden=false;
    }
}


- (IBAction)cbDepto:(id)sender {
    NSString *urlString = [NSString stringWithFormat:@"ConsultaDepartamentos"];
    [restConnection performRequest:
    [NSURLRequest requestWithURL:
    [NSURL URLWithString:urlString]]];
    service=@"Depto";
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if ([service isEqualToString:@"Depto"]) {
        NSLog(@"en el picket: %@", _deptoArray);
        NSLog(@"data: %@", encodData);
        return [_deptoArray count];

    }else{
        return 0;
    }

}

@end

the log of deptoArray in "finishedReceivingData"

2014-11-30 18:40:33.976 Salud Total[2629:132519] solo nombre: ( "Bogot\U00e1 DC", Antioquia, Atlantico )

the log of deptoArray in "numberOfRowsInComponent" 2014-11-30 18:42:27.279 Salud Total[2629:132519] data: { }

or message: Thead 1: EXC_BAD_ACCESS(code=EXC_I386_GPFLT) or 2014-11-30 18:44:34.311 Salud Total[2650:133534] data: (no indexes)

1

There are 1 best solutions below

0
On

Thanks for the awesome support, without this amazing tool called "google" I realized I was wrong NSOrderedSet referencing apparently used it as a pointer to the data received and not as another buffer.

first I change the NSOrderedSet to an NSArray then

I change the

deptoArray=[NSOrderedSet orderedSetWithArray:[json valueForKey:@"Nombre"]];

for

deptoArray=[[NSArray alloc]initWithArray:[json valueForKey:@"Nombre"]];

and works

Please do not qualify me negative I'm about to prevent me from asking questions and I am new to this and this forum has been a great tool to help