pathForResource:ofType not working

Discussion in 'Mac Programming' started by JohnWi, Jan 4, 2010.

  1. JohnWi macrumors newbie

    Joined:
    Nov 11, 2009
    #1
    Hello everyone,

    I am trying to create a NSString called filePath that I want to use to write entries into a plist. I use

    filePath = [[[NSBundle mainBundle] pathForResource:mad:"test" ofType:mad:"plist"] retain];

    to define the path, but if I log filePath, it returns nil. Does anyone know what is not working here?

    Thx, John
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Use this:

    Code:
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:@"test" ofType:@"plist"];
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    That would give you the path of a plist within your application (if it exists). I'm quite sure you don't want to modify a plist within your application. Even if you _think_ you want to modify it, you still don't want to do it.
     
  4. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #4
    Per the documentation, the method returns nil if the resource could not be located. The method is for checking for a resource before loading. I'm guessing you're wanting to create a new file rather than loading an existing one. If that is the case, try this instead:

    Code:
    NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"test.plist"];
    
     
  5. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    In particular, this has a tendency to break code signing.
     
  6. JohnWi thread starter macrumors newbie

    Joined:
    Nov 11, 2009
    #6
    Hello everyone,

    thx a lot for the help. I used the solution posted by lucasgladding and everything works the way I want it to.
     
  7. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    What happens when you copy your application onto a CD or a read-only disk image? What happens when two users on the same computer use your application?
     
  8. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #8
    For the record, I do agree with everyone else on this thread about storing within the application bundle. See the "Low Level File Management Programming Guide" and "Creating Paths and Locating Directories" for information about alternative system paths. Your application support directory on the Mac or the documents folder on the iPhone make the most sense typically. If you generate Core Data projects for either platform, you will find example code in the application controller.

    Best of luck

    Luke
     
  9. JohnWi thread starter macrumors newbie

    Joined:
    Nov 11, 2009
    #9
    Hm, I see what you mean. But to be honest, the application I write is mostly a prove-of-concept thing so I do not worry about the obstacles you mention (at least not yet). I guess I will have to come up with a better solution if the project grow. Thx again.
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Simple: Preference data goes into the preference folder, not in the application bundle. The preference folder is always writeable, and there is always one preference folder per user. And most likely you don't care about the location of the preference folder at all, you just add or write preferences and Cocoa will put them into the right place.

    So the problem that you are going to run into is not one that you will need to solve, the whole approach is wrong.
     

Share This Page