-1

hi am trying to create a plist of my calculations but it turns out the list is empty....pls help here is my code.is there any othe way to write strings to a file other than this...

-(float)calculate
{
    if(!self.operatorLabel.text.length)
        return self.operand2;
    float result=0;
    switch([self.operatorLabel.text characterAtIndex:0])
    {
        case '+':result=self.self.operand1+self.operand2;
            break;
        case '-':result=self.operand1-self.operand2;
            break;
        case '*':result=self.operand1*self.operand2;
            break;
        case '/':if(self.operand2==0)
            divideByZeroFlag=true;
        else
            result=self.operand1/self.operand2;
            break;
        default:self.operand1=0;
            result=self.operand2;
            break;
    }
    if(!divideByZeroFlag&&self.operand1!=0)
    {

        NSString *data=[NSString stringWithFormat:@"%g%@%g=%g",self.operand1,self.operatorLabel.text,self.operand2,result];
        NSMutableDictionary *dat;
        [dat setObject:data forKey:@"nthing"];
        [dat writeToFile:@"memory.plist" atomically:YES];
    }
    return result;
}
Raon
  • 1,266
  • 3
  • 12
  • 25
  • 1
    Is debugging deprecated nowadays? Otherwise you should see quickly that `dat` is `nil` because you did not initialize it with a (mutable) dictionary ... – Martin R Aug 13 '13 at 05:17
  • And in addition to Martin's excellent point, you must pass a full pathname to the `writeToFile:` method, not just a filename. – rmaddy Aug 13 '13 at 05:18
  • You didn't allocate the dictionary `NSMutableDictionary *dat = [[NSMutableDictionary alloc] init]`. And also change `memory.plist` to full path `directoryPath/memory.plist`. – TheTiger Aug 13 '13 at 05:20
  • @MartinR Debugging has recently been deprecated as a part of the Stack Overflow Friendliness Movement. :P Apart from that, OP should really fix their indentation, that `else` needs to be one level deeper than the `default`... –  Aug 13 '13 at 05:48

2 Answers2

1

1 - You must initialize dat.

2 - [NSDictionary writeToFile] expects full path, not just the name.

EDIT:

To create a path, do this:

+ (NSString*) createFullFilePath:(NSString *)fileName
{
    //Look at documents for existing file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", fileName]];

    NSFileManager* fileManager = [NSFileManager defaultManager];

    if(![fileManager fileExistsAtPath:path])
    {
        NSError *error;
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
       if (error)
           NSLog (@"%@", [error localizedDescription]);
    }

    return path;
}

Once done, write to that path using following code (note that this initializes dictionary too (check NSLogs to see what you are doing yields results):

NSString * path = [self createFullFilePath:@"memory.plist"];
NSLog (@"@%", path);
NSMutableDictionary *dat = [NSMutableDictionary dictionary];
NSLog (@"@%", dat);
[dat setObject:data forKey:@"nthing"];
[dat writeToFile:path atomically:YES];
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • Note that documents DIR is just one example, you could use many other folders under your app's path - such as Application Support, Cache etc depending on your requirements. You should read Apple Manual for what to store in what location, to ensure successful app submission. – Nirav Bhatt Aug 13 '13 at 05:29
  • Also, as Chris suggests, there is no real need for storing in a plist unless you have dictionary requirements. But the createFullFilePath function will remain same no matter what you write using writeToFile. – Nirav Bhatt Aug 13 '13 at 05:33
0

Why a plist file? A plist is nice for arrays and dictionaries, but I see no reason to store a simple string in a plist.

How about...

NSString *data = ...
NSError *error = nil;
[data writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
Chris Wagner
  • 20,773
  • 8
  • 74
  • 95