Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Problem: Loading data from Plist to array

EvandroEvandro Posts: 7New Users
Hello Guys!

I trying to load data from a Plist... Into a array. But doesnt work!
When i print the array i receive: Array: (Null)

I looked in several places, but nothing tells me something important about it.

Someone can help me with that? Or tell me a correctly form to manage data info in app?

I'm using the last XCode version.

Controller Function:

- (void)viewDidLoad
{

dataDictItem = [NSDictionary dictionary];

dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

dataArray = [[NSArray alloc] initWithContentsOfFile:dataPath];

for(dataDictItem in dataArray)
{
NSLog(@"Dictionary Item: %@",dataDictItem);
}

NSLog(@"Array: %@",dataArray);

[dataArray release];

[super viewDidLoad];
}


Plist: Data.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "<a href="http://www.apple.com/DTDs/PropertyList-1.0.dtd" target="_blank" rel="nofollow">http://www.apple.com/DTDs/PropertyList-1.0.dtd">

User - 1

name
Evandro
lasName
Eisinger
imagePath
image.png

User - 2

name
Evandro
lasName
Eisinger
imagePath
image.png


Thanks! I'm waiting for a feedback! :)
Post edited by Evandro on

Replies

  • iiswordiisword Posts: 552Registered Users
    just do
    [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@\"Data\" ofType:@\"plist\"]]
  • EvandroEvandro Posts: 7New Users
    iisword;334284 said:
    just do
    [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@\"Data\" ofType:@\"plist\"]]
    Hello iisword!

    Thanks for the answer! You help me a lot!
    But i have one more question... How can i store each dictionary value into a array? I tried, but doesnt work...

    My improved code, with your tips:


    - (void)viewDidLoad
    {

    dataDictItem = [NSDictionary dictionary];

    dataPath = [[NSBundle mainBundle] pathForResource:@\"Data\" ofType:@\"plist\"];

    dataDict = [[NSDictionary alloc] initWithContentsOfFile:dataPath];

    dataArray = [[NSArray alloc] init];

    for(dataDictItem in dataDict)
    {

    [dataArray addObject:dataDictItem];
    NSLog(@\"Dictionary Item: %@\",dataDictItem);
    }

    [dataArray release];

    NSLog(@\"Array: %@\", dataArray);

    [super viewDidLoad];
    }

  • iiswordiisword Posts: 552Registered Users
    I would suggest you read up on Loading… for future advice. Looking at a class' reference will help you save time and energy.

    As for the method, I believe [dataDict allValues] is what you want.
  • EvandroEvandro Posts: 7New Users
    iisword;334341 said:
    I would suggest you read up on Loading… for future advice. Looking at a class' reference will help you save time and energy.

    As for the method, I believe [dataDict allValues] is what you want.
    Hello man!

    Thanks again for help me with my problems! But i still have some conflicts...

    You told me to use [dataDict allValues], but doesnt work... Always apear this msg: Thread 1: Program received signal: "SIGABRT"

    What this mean?

    My actual code:

    - (void)viewDidLoad
    {

    dataDictItem = [NSDictionary dictionary];

    dataPath = [[NSBundle mainBundle] pathForResource:@\"Data\" ofType:@\"plist\"];

    dataDict = [[NSDictionary alloc] initWithContentsOfFile:dataPath];

    dataArray = [[NSMutableArray alloc] init];

    for(dataDictItem in dataDict)
    {

    [dataArray addObject:[dataDictItem allValues]];
    NSLog(@\"Dictionary Item: %@\",dataDictItem);
    }

    [dataArray release];

    NSLog(@\"Array: %@\", dataArray);

    [super viewDidLoad];
    }


    Can you tell me whats going wrong?

    Thx for help! I still trying to resolve the problem...

    []'s
  • EvandroEvandro Posts: 7New Users
    Hello guys!

    I created one data model when i tried to resolve my problem, but i couldnt create my DataModel object and send the value of each dictionary element to him. Apear this msg into this line: Unsed variable 'dataItem'

    DataModel *dataItem = [[DataModel alloc] initWithDictionary:dataDictItem];

    DataModel.h :


    #import <Foundation/Foundation.h>


    @interface DataModel : NSObject {

    NSString *name;
    NSString *lastName;
    NSString *imagePath;

    }

    - (id)initWithDictionary:(NSDictionary *)aDictionary;

    @property (nonatomic, retain) IBOutlet NSString *name;
    @property (nonatomic, retain) IBOutlet NSString *lastName;
    @property (nonatomic, retain) IBOutlet NSString *imagePath;

    @end


    DataModel.m :


    #import \"DataModel.h\"


    @implementation DataModel

    @synthesize name;
    @synthesize lastName;
    @synthesize imagePath;

    - (id)initWithDictionary:(NSDictionary *)aDictionary {

    self.name = [aDictionary valueForKey:@\"name\"];
    self.lastName = [aDictionary valueForKey:@\"symbol\"];
    self.imagePath = [aDictionary valueForKey:@\"state\"];

    return self;
    }

    - (void)dealloc {
    [name release];
    [lastName release];
    [imagePath release];
    [super dealloc];
    }

    @end


    Controller:


    - (void)viewDidLoad
    {

    dataDictItem = [NSDictionary dictionary];

    dataPath = [[NSBundle mainBundle] pathForResource:@\"Data\" ofType:@\"plist\"];

    dataDict = [[NSDictionary alloc] initWithContentsOfFile:dataPath];

    dataArray = [[NSMutableArray alloc] init];

    for(dataDictItem in dataDict)
    {

    DataModel *dataItem = [[DataModel alloc] initWithDictionary:dataDictItem];

    }

    [dataArray release];

    NSLog(@\"Array: %@\", dataArray);

    [super viewDidLoad];
    }



    Thx for help! I still trying to resolve the problem...

    []'s
  • EvandroEvandro Posts: 7New Users
    Sorry about that, but: Anyone know how to fix this problem?

    Thx! []'s
Sign In or Register to comment.