Fwd: Load plist into array question

  • Hi all

    I'm trying to load a plist into an array using this code:

    NSArray *myPlist = [[NSArray alloc] initWithContentsOfFile:[[[NSBundle
    mainBundle] resourcePath]
    stringByAppendingPathComponent:@"myplist.plist"]];

    However, myPlist is always null. Are there any additional steps
    involved in loading a plist into an array?

    Thanks for reading,
    Filipe Varela
  • On Dec 10, 2003, at 09:10, Filipe Varela wrote:

    > Hi all
    >
    > I'm trying to load a plist into an array using this code:
    >
    > NSArray *myPlist = [[NSArray alloc]
    > initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath]
    > stringByAppendingPathComponent:@"myplist.plist"]];
    >
    > However, myPlist is always null. Are there any additional steps
    > involved in loading a plist into an array?

    Are you sure that the plist is where you think it is? (i.e. it's not
    in some "*.lproj" subdirectory)
    Are you sure that the plist actually represents and array? (and not
    some other object like a dictionary)

    This may be unrelated, but your code for finding the path of your plist
    seems a little on the complex side, what's wrong with:
    NSArray *myPlist = [[NSArray alloc] initWithContentsOfFile: [[NSBundle
    mainBundle] pathForResource: @"myplist" ofType:@"plist"];

    --
    Clark S. Cox III
    <clarkcox3...>
    http://homepage.mac.com/clarkcox3/
    http://homepage.mac.com/clarkcox3/blog/B1196589870/index.html
  • On Wednesday, December 10, 2003, at 03:10 PM, Filipe Varela wrote:

    > Hi all
    >
    > I'm trying to load a plist into an array using this code:
    >
    > NSArray *myPlist = [[NSArray alloc]
    > initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath]
    > stringByAppendingPathComponent:@"myplist.plist"]];
    >
    > However, myPlist is always null. Are there any additional steps
    > involved in loading a plist into an array?

    At least, there are additional steps to know where the problem is
    coming from:

    NSArray * myPlist;
    NSString * tPath;

    tPath= [[NSBundle mainBundle] pathForResource:@"myplist"
    ofType:@"plist"];

    if (tPath!=nil)
    {
    myPlist= [[NSArray alloc] initWithContentsOfFile: tPath];

    if (myPlist!=nil)
    {
      [...]
    }
    else
    {
      NSLog(@"The myplist.plist is not a valid plist or does not contain
    the data I thought there was inside it. I should try to open it with
    the Plist Editor to check it.");
    }
    }
    else
    {
    NSLog(@"The file myplist.plist is not where I think it was.");
    }
  • Thanks to everyone who replied.

    I'm using that code as the third alternative for loading a plist. Both
    other have the same effect, a null myPlist. I was already debugging
    path variable with NSLog and the string is correct and points to a
    valid path. Furthermore, the plist opens just fine in the Editor and i
    triple checked it's content. Just to make sure i wasnt doing anything
    wrong, i replaced NSArray with NSDictionary and it just dies with a
    signal 10 (SIGBUS).

    I tried several other things like using a different plist (preferences
    from other applications, system settings, etc) and they all produce the
    same null myPlist. By this time you're thinking i'm catching the right
    path and using a wrong one because system settings's plist can't
    possibly be corrupt (not all of them at least). But i can get a string
    with the contents of the file and display it.

    Has anyone experienced anything like this before?

    Filipe
  • On Dec 10, 2003, at 6:51 AM, Filipe Varela wrote:

    > I tried several other things like using a different plist (preferences
    > from other applications, system settings, etc) and they all produce
    > the same null myPlist. By this time you're thinking i'm catching the
    > right path and using a wrong one because system settings's plist can't
    > possibly be corrupt (not all of them at least). But i can get a string
    > with the contents of the file and display it.

    What happens if you do this?

        NSString * path = [[NSBundle mainBundle] pathForResource:
    @"myplist" ofType:@"plist"];
        NSString * fileContents = [NSString stringWithContentsOfFile: path];
        id someObject = [fileContents propertyList];
        NSLog(@"someObject: %@", someObject);

    That allows NSString to decide whether to return a dictionary, array,
    etc. from the file contents.

        - Scott

    --
    Tree House Ideas
    http://treehouseideas.com/
  • It's not confidential and it's not mine either.

    The application i'm building is basically a syntax-highlighting rule
    maker for SubEthaEdit. It's supposed to read SEE's syntax coloring
    rules and display them. It will support adding new rules and changing
    them but so far i haven't been able to load them :/ . You can download
    it (http://www.codingmonkeys.de/) and open it's php.plist for example.
    That was the one i chose to test with because it's a fairly large (in
    fact, it's huge) one.

    I checked permissions and gave everyone read and write access but with
    no success.

    Filipe Varela

    On Dec 10, 2003, at 15:20, Stephane Sudre wrote:

    > Is the plist confidential or you can send it so that I can have a look
    > at it?
    >
    > Other idea: permissions on the file?
    >
    > On Wednesday, December 10, 2003, at 03:51 PM, Filipe Varela wrote:
    >
    >> Thanks to everyone who replied.
    >>
    >> I'm using that code as the third alternative for loading a plist.
    >> Both other have the same effect, a null myPlist. I was already
    >> debugging path variable with NSLog and the string is correct and
    >> points to a valid path. Furthermore, the plist opens just fine in the
    >> Editor and i triple checked it's content. Just to make sure i wasnt
    >> doing anything wrong, i replaced NSArray with NSDictionary and it
    >> just dies with a signal 10 (SIGBUS).
    >>
    >> I tried several other things like using a different plist
    >> (preferences from other applications, system settings, etc) and they
    >> all produce the same null myPlist. By this time you're thinking i'm
    >> catching the right path and using a wrong one because system
    >> settings's plist can't possibly be corrupt (not all of them at
    >> least). But i can get a string with the contents of the file and
    >> display it.
    >>
    >> Has anyone experienced anything like this before?
  • I just wrote a quick program to load the php.plist file from
    SubEthaEdit, and had no problems with:

        NSString *file = [[NSBundle mainBundle] pathForResource:@"php"
    ofType:@"plist"];
        NSDictionary *dict = [NSDictionary
    dictionaryWithContentsOfFile:file];

        NSLog(@"dict = %@", dict); //really loaded into an NSDictionary

    Good luck,

    Lon

    On Dec 10, 2003, at 8:54 AM, Filipe Varela wrote:

    > It's not confidential and it's not mine either.
    >
    > The application i'm building is basically a syntax-highlighting rule
    > maker for SubEthaEdit. It's supposed to read SEE's syntax coloring
    > rules and display them. It will support adding new rules and changing
    > them but so far i haven't been able to load them :/ . You can download
    > it (http://www.codingmonkeys.de/) and open it's php.plist for example.
    > That was the one i chose to test with because it's a fairly large (in
    > fact, it's huge) one.
    >
    > I checked permissions and gave everyone read and write access but with
    > no success.
    >
    > Filipe Varela
    >
    > On Dec 10, 2003, at 15:20, Stephane Sudre wrote:
    >
    >> Is the plist confidential or you can send it so that I can have a
    >> look at it?
    >>
    >> Other idea: permissions on the file?
    >>
    >> On Wednesday, December 10, 2003, at 03:51 PM, Filipe Varela wrote:
    >>
    >>> Thanks to everyone who replied.
    >>>
    >>> I'm using that code as the third alternative for loading a plist.
    >>> Both other have the same effect, a null myPlist. I was already
    >>> debugging path variable with NSLog and the string is correct and
    >>> points to a valid path. Furthermore, the plist opens just fine in
    >>> the Editor and i triple checked it's content. Just to make sure i
    >>> wasnt doing anything wrong, i replaced NSArray with NSDictionary and
    >>> it just dies with a signal 10 (SIGBUS).
    >>>
    >>> I tried several other things like using a different plist
    >>> (preferences from other applications, system settings, etc) and they
    >>> all produce the same null myPlist. By this time you're thinking i'm
    >>> catching the right path and using a wrong one because system
    >>> settings's plist can't possibly be corrupt (not all of them at
    >>> least). But i can get a string with the contents of the file and
    >>> display it.
    >>>
    >>> Has anyone experienced anything like this before?
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • Yes i know. I mentioned having changed my NSArray to an NSDictionary
    and it blows up with a SIGBUS 10 when i try to determine the number of
    items inside it with [myPlist count];

    Filipe

    On Dec 10, 2003, at 16:10, Stephane Sudre wrote:

    > Hum, the file php.plist, I'm looking at has a NSDictionary as its root
    > object not a NSArray...
    >
    > On Wednesday, December 10, 2003, at 04:54 PM, Filipe Varela wrote:
    >
    >> It's not confidential and it's not mine either.
    >>
    >> The application i'm building is basically a syntax-highlighting rule
    >> maker for SubEthaEdit. It's supposed to read SEE's syntax coloring
    >> rules and display them. It will support adding new rules and changing
    >> them but so far i haven't been able to load them :/ . You can
    >> download it (http://www.codingmonkeys.de/) and open it's php.plist
    >> for example. That was the one i chose to test with because it's a
    >> fairly large (in fact, it's huge) one.
    >>
    >> I checked permissions and gave everyone read and write access but
    >> with no success.
    >
  • Blows up with a signal 10 (SIGBUS) here...

    Filipe

    On Dec 10, 2003, at 16:39, Lon Varscsak wrote:

    > I just wrote a quick program to load the php.plist file from
    > SubEthaEdit, and had no problems with:
    >
    > NSString *file = [[NSBundle mainBundle] pathForResource:@"php"
    > ofType:@"plist"];
    > NSDictionary *dict = [NSDictionary
    > dictionaryWithContentsOfFile:file];
    >
    > NSLog(@"dict = %@", dict); //really loaded into an NSDictionary
    >
    > Good luck,
    >
    > Lon
    >
    > On Dec 10, 2003, at 8:54 AM, Filipe Varela wrote:
  • On Dec 10, 2003, at 8:44 AM, Filipe Varela wrote:

    > Yes i know. I mentioned having changed my NSArray to an NSDictionary
    > and it blows up with a SIGBUS 10 when i try to determine the number of
    > items inside it with [myPlist count];

    Something else must be wrong then because nil is returned if the
    property list isn't loaded, and nil doesn't care what messages you send
    it. Calling count on nil wouldn't cause a crash.

    Are you trying to log the count to the console using NSLog? If so did,
    you happen to use the wrong format? For example this would probably
    cause a crash:

    NSLog(@"the count: %@", [myPlist count])

    In the above case, I've mistakenly used %@ to try to output an 'int'
    variable. I should have used %d Is it possible you have a similar typo
    in the code?

        - Scott

    --
    Tree House Ideas
    http://treehouseideas.com/
  • On Dec 10, 2003, at 8:48 AM, Filipe Varela wrote:

    >> I just wrote a quick program to load the php.plist file from
    >> SubEthaEdit, and had no problems with:
    >>
    >> NSString *file = [[NSBundle mainBundle] pathForResource:@"php"
    >> ofType:@"plist"];
    >> NSDictionary *dict = [NSDictionary
    >> dictionaryWithContentsOfFile:file];
    >>
    >> NSLog(@"dict = %@", dict); //really loaded into an NSDictionary
    >
    > Blows up with a signal 10 (SIGBUS) here...

    The bug is probably elsewhere then. Even if the file didn't exist, the
    above shouldn't cause a crash.

    Is it possible one of the following is happening (unrelated to the
    plist loading)?

    1. You're over-releasing an object somewhere?
    2. Trying to initialize an array with a list of objects, but omitted
    the nil at the end of the list?
    3. Have an NSLog call that's missing the @ in front of the double
    quotes?

    Those are really common causes of crashes, and sometimes quite hard to
    track down.

        - Scott

    --
    Tree House Ideas
    http://treehouseideas.com/
  • Solved... My REAL bug disappeared when i checked php.plist and noticed
    it's root was a dictionary. I corrected the code accordingly but still
    got the sigbus.

    So i decided to uncomment my check for nil again and it wasnt nil
    anymore. i thought it was because the app kept crashing.

    My hidden bug was the argument to NSLog as someone pointed out it could
    be. I was doing this

    NSLog(@"This dict has %@ elements", [dict count]);

    Instead of

    NSLog(@"This dict has %d elements", [dict count]);

    NOW!!!!!!!!!!! Sorry for posting such a large number of messages and
    missing such a stupid typo...

    Thanks do everyone who helped...

    Filipe
  • lol, those are the worst kind of bugs! :)

    Lon

    On Dec 10, 2003, at 10:21 AM, Filipe Varela wrote:

    > Solved... My REAL bug disappeared when i checked php.plist and noticed
    > it's root was a dictionary. I corrected the code accordingly but still
    > got the sigbus.
    >
    > So i decided to uncomment my check for nil again and it wasnt nil
    > anymore. i thought it was because the app kept crashing.
    >
    > My hidden bug was the argument to NSLog as someone pointed out it
    > could be. I was doing this
    >
    > NSLog(@"This dict has %@ elements", [dict count]);
    >
    > Instead of
    >
    > NSLog(@"This dict has %d elements", [dict count]);
    >
    > NOW!!!!!!!!!!! Sorry for posting such a large number of messages and
    > missing such a stupid typo...
    >
    > Thanks do everyone who helped...
    >
    > Filipe
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
previous month december 2003 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today