Another Objective-C problem.

I have an IBAction that’s not doing it’s job. I looked online everywhere to find out how to see if a file exists! Please look at the handler. It’s giving me errors and I don’t know how to solve them. The DWImageView is an NSView handling an image.

- (void)fieldDidSendName:(id)sender { NSString *bundlePath = [[NSBundle mainBundle] resourcePath]; NSLog(@"%@", bundlePath); NSFileManager *fm = [NSFileManager defaultManager]; NSString *thefile = @"%@%@.png", bundlePath, [thefield stringValue]; BOOL *exists = [fm fileExistsAtPath:thefile]; NSLog(@"%@", thefile); if (exists == NO) { NSString *previoustext = [thefield stringValue]; [thefield setStringValue:@"That image is not availible."]; [thefield setTextColor:[NSColor redColor]]; sleep(3); [thefield setStringValue:previoustext]; [thefield setTextColor:[NSColor blackColor]]; } else { DWImageView *theinit = [DWImageView alloc]; theimage = [NSImage imageNamed:[thefield stringValue]]; [theinit setImage]; } }

Hi,

the method resourcePath returns a path without a trailing slash, so you must write

NSString *thefile = @"%@/%@.png", bundlePath, [thefield stringValue];

or the “waterproof” version

NSString *thefile = [[[bundlePath stringByAppendingPathComponent:[thefield stringValue]] stringByAppendingPathExtension:@"png"];

But a much easier way is

NSString *thefile = [[NSBundle mainBundle] pathForResource:[thefield stringValue] ofType:@"png"]; if (theFile) { // file exists } else { // file does not exist }
the DWImage should be defined with an IBOutlet, or you must use [[ alloc] init] and a balancing [ release] somewhere to avoid memory leaks

OK, I replaced my old stuff with “But a much easier way is” way. I added “[theinit release];” to the end of the else block. I did add the init: “alloc] init];”.

It still doesn’t work! :frowning:

- (void)fieldDidSendName:(id)sender { NSString *theFile = [[NSBundle mainBundle] pathForResource:[thefield stringValue] ofType:@"png"]; if (theFile) { NSBeep(); NSString *previoustext = [thefield stringValue]; [thefield setStringValue:@"That image is not availible."]; [thefield setTextColor:[NSColor redColor]]; sleep(3); [thefield setStringValue:previoustext]; [thefield setTextColor:[NSColor blackColor]]; } else { DWImageView *theinit = [[DWImageView alloc] init]; theimage = [NSImage imageNamed:[thefield stringValue]]; [theinit setImage]; [theinit release]; } }
Yes, the applicationDidFinishLaunching: does work. I tested it by adding “NSBeep();”

can you explain in which way the custom view is used?
Releasing (throwing away) the object after setting the image is certainly not intended.

In the “easier” way the if and else branches are swapped.
the condition if (theFile) is true if the file is not nil i.e. the file exists.

I have this. It beeps when it starts and when I send the action, but it doesn’t do ANYTHING! Nothing to “thefield,” nothing to the DWImageView.

[code]- (void) applicationDidFinishLaunching:(NSNotification *)notification {
NSBeep();
}

  • (void)fieldDidSendName:(id)sender {
    NSBeep();
    NSString *theFile = [[NSBundle mainBundle] pathForResource:[thefield stringValue] ofType:@“png”];
    if (theFile) {
    NSBeep();
    NSString *previoustext = [thefield stringValue];
    [thefield setStringValue:@“That image is not availible.”];
    [thefield setTextColor:[NSColor redColor]];
    sleep(3);
    [thefield setStringValue:previoustext];
    [thefield setTextColor:[NSColor blackColor]];
    }
    else {
    DWImageView *theinit = [[DWImageView alloc] init];
    theimage = [NSImage imageNamed:[thefield stringValue]];
    [theinit setImage];
    }
    }
  • (void) dealloc {
    DWImageView *theinit;
    [theinit release];
    [super dealloc];
    }[/code]
    DWImageView is a small NSView that set’s itself to the image in the message sent to it’s “setImage:” action.

have you connected the UI elements in Interface Builder to the respective IBOutlets?

The DWImageView must also be assigned to a UI element like window or panel or menu item.
The syntax you use cannot work, both variables theInit are not identical. The scope issue is the same as in AppleScript