1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSWorkspace Basic Question

Discussion in 'Mac Programming' started by RobRiley, Feb 6, 2009.

  1. macrumors member

    #1
    Hi,

    Just trying to get some basic NSWorkspace stuff going but can't compile my app. I thought it was my code so I lifted the following from another site and still get the same errors:

    Code:
    NSString * path    = @"/Developer/About Xcode Tools.pdf";
    NSURL * fileURL = [NSURL fileURLWithPath: path];
    NSWorkspace * ws = [NSWorkspace sharedWorkspace];
    [ws openFile:[fileURL path] withApplication:@"Safari"];
    The errors are:

    Code:
     ".objc_class_name_NSWorkspace", referenced from:
    literal-pointer@__OBJC@__cls_refs@NSWorkspace in AppController.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    I read that it could be that I'm not including the correct libraries. I am including the following in my header file:

    Code:
    #import <Cocoa/Cocoa.h>
    #import <Foundation/Foundation.h>
    #import <ApplicationServices/ApplicationServices.h>
    #import <AppKit/AppKit.h>
    Anyone know what the problem is?

    Thanks in advance..
     
  2. Moderator emeritus

    kainjow

    #2
    The Cocoa framework includes AppKit and Foundation, so including those again is unnecessary.

    How are you compiling? Xcode or Terminal? It looks like you aren't probably linking against the Cocoa or AppKit framework (NSWorkspace is part of AppKit).
     
  3. macrumors member

    #3
    Hey kainjow,

    I'm compiling in XCode. Thanks for the tip re.including files. So how do I link against those frameworks correctly?

    Thanks,
     
  4. Moderator emeritus

    kainjow

    #4
    If this is a standard Cocoa application it should already be included. If not, then you need to add the Cocoa framework into the Frameworks group.

    Under Targets > yourapp, there is a group Link Binary With Libraries. You should see the framework here once you've added it to the Frameworks group.
     
  5. macrumors member

    #5
    It's already there. I've broken it down into the simplest form to try and diagnose the problem. Here is my header file:

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface AppController : NSObject {
    }
    - (IBAction)Test:(id)sender;
    @end
    And here is my implementation file:

    #import "AppController.h"

    Code:
    @implementation AppController
    - (IBAction)Test:(id)sender{
    	NSString * path    = @"/Developer/About Xcode Tools.pdf";
    	NSURL * fileURL = [NSURL fileURLWithPath: path];
    	NSWorkspace * ws = [NSWorkspace sharedWorkspace];
    	[ws openFile:[fileURL path] withApplication:@"Safari"];
    }
    @end
    Is my code wrong?

    Thanks,
     
  6. Moderator emeritus

    kainjow

    #6
    It's not your code. Maybe if your project is tiny you could upload it here. If that's not the problem then somehow your Xcode installation is hosed, but that would be odd.
     
  7. macrumors member

    #7
    Well strangely I've just pasted the code into a new project and it works.. so not sure what the problem was. I'll go back and figure it out when I've got a spare week! Thanks for all your help.
     

Share This Page