Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
I'm trying to read a file with a relative pathname, but it's not working.

Can somebody help me out?

Thanks.

Code:
- (void) drawRect:(NSRect)dirtyRect {
	NSString *directory = @".";
	directory = [directory stringByStandardizingPath];
	NSString *fileName = @"/Logo.png";
	NSString *string = [NSString stringWithFormat:@"%@%@%@", @"file://localhost/", directory, fileName];
	NSURL *url = [[NSURL alloc] initWithString:string];
	NSData *data = [NSData dataWithContentsOfURL:url]; <<< data is nil <<<
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
Display the resulting string, before you make it into a URL. Confirm the file actually exists at that location. Post the result.

Since you're referring to a file on localhost, you can avoid the URL entirely and use one of the NSData methods that returns the contents of a file given by a pathname string.

There are also NSData methods that return an NSError by reference. You might use one of those, and use the NSError or its sub-components to give you more detail on exactly what the problem is.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
Thanks, That Works Better

Thanks, that works better. Here is my new code:

Code:
- (void) drawRect:(NSRect)dirtyRect {
	NSString *path = @"./Preferences/Logo.png";
	NSData *data = [NSData dataWithContentsOfFile:path];
	NSImage *image = [[NSImage alloc] initWithData:data];
	[self setImage:image];
	[super drawRect:dirtyRect];
}

@end

The curious thing about the above code is that it works in debug mode, but not in release mode (I don't get it!).

-- Kaydell :):confused:
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
In debug mode the cwd may be were you think it is. In release mode I'm pretty sure it isn't. Where are you trying to read from? Within the application? If so use NSBundle to find the path. That's the correct way to do it.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
Where are you trying to read from? Within the application? If so use NSBundle to find the path. That's the correct way to do it.

No, I'm trying to read from the Preferences folder which is within my applications folder. The reason that I'm reading from my applications Preferences folder is because the user should be able to drop in their own logo which I don't know at compile-time, so I don't want to keep the logo within my application's bundle.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Well, you're not putting that in the correct places: it should be in Application Support. Which you can get the path to programatically using the usual function.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
Thanks Everyone

Thanks for all of your help.

It's working now. I just had to put the logo in two places. Once in the Debug Folder and once again in the Release Folder.

Thanks again.

-- Kaydell :):):)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Please see my advice above: you should not rely on a folder in /Applications. Any well written application could be moved anywhere on the filesystem. Or run from a location that the user can't write to. Apple provide clear guidelines on this: use ~/Application Support/. A really well written app would look in the user, then system, then network domains (in Application Support) in that order.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
...use NSBundle to find the path. That's the correct way to do it.

OK. Here is my code to read the logo from the application bundle. It perplexes me that the code works in debug but not for the release target.

Code:
	NSBundle *mainBundle = [NSBundle mainBundle];  // <<<<< works in debug mode, but returns nil in release mode

:confused:
 

Thomas Harte

macrumors 6502
Nov 30, 2005
400
4
When you say it returns nil, is that from an NSLog or from checking with the debugger? The debugger is likely to show 'nil' if the optimiser has cut the variable.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
OK. Here is my code to read the logo from the application bundle. It perplexes me that the code works in debug but not for the release target.

Code:
	NSBundle *mainBundle = [NSBundle mainBundle];  // <<<<< works in debug mode, but returns nil in release mode

:confused:

Post all your code. Or at least more than one line. An isolated line of code is meaningless, without knowing what comes before and after.

Also, are you building a command-line tool, or a bundled app? It's not clear from any prior post which one it is.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
...When you say it returns nil, is that from an NSLog or from checking with the debugger? The debugger is likely to show 'nil' if the optimiser has cut the variable.

OK. That's was it. I was seeing nil in the debugger, but in NSLog() it look OK.

-- Kaydell :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.