Unable to write text file - Odd Warning

Discussion in 'Mac Programming' started by Mordicaii, May 2, 2009.

  1. Mordicaii macrumors newbie

    Joined:
    May 2, 2009
    #1
    I have been trying to learn to write files in Obj-C on OS X (not iPhone), and I have been getting a Warning (Picture Below)
    [​IMG]
    What does this Warning mean, and how can I fix the issue?

    Code - AppGromit.m
    Code:
    #import "AppGromit.h"
    
    @implementation AppGromit
    - (IBAction)writeFile:(id)sender {
        NSString *fileTest;
    	fileTest = @"Hello, I am writing a file";
    	[statLabel setIntValue:0];
    	if (![fileTest writeToFile:"text.txt" atomically:YES encoding:NSUTF8StringEncoding]) {
    		[statLabel setValue:@"Written"];
    	}else{
    		[statLabel setValue:@"Fail"];
    	}
    }
    @end
    
    
    Code - AppGromit.h
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface AppGromit : NSObject {
    	IBOutlet NSTextField *statLabel;
    }
    - (IBAction)writeFile:(id)sender;
    @end
    
    
    Thank You!
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    The method is writeToFile:atomically:encoding:error:. Note the missing error argument. You can just pass NULL if you don't want the error returned.
     
  3. Mordicaii thread starter macrumors newbie

    Joined:
    May 2, 2009
    #3
    ---It tells me the same thing---
    EDIT: It worked, thanks, man.
     
  4. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    OK yeah, the other thing though is for the file path you should be sending an NSString (using @) there instead of a C string. That's why the other warning you probably got about "incompatible pointer type". So the line should be:

    Code:
    if (![fileTest writeToFile:@"text.txt" atomically:YES encoding:NSUTF8StringEncoding error:NULL]) {
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Additionally you should use an absolute path, such as @"/Users/me/Desktop/test.txt"
     

Share This Page