Hi all,
I have been struggling with this for a few days and need help. My (NSArray) writeToFile: fails. I have written some test code that closely emulates the code I am trying to write. It basically takes an instance of a class, places it in an NSArray, then writes it to an NSURL. For good measure, the file at the given NSURL is first read.
Here is the code. (Xcode(4.3.3), command line tool. A file, "H.txt" with the words "This is a test" exists in a folder called "testPLIST" on the desktop.
output:
2012-07-15 15:35:33.138 TestPlistMaker[996:403] url:file://localhost/Users/m/Desktop/testPLIST/H.txt
2012-07-15 15:35:33.140 TestPlistMaker[996:403] File contents: This is a test.
2012-07-15 15:35:33.140 TestPlistMaker[996:403] Failed
Would appreciate any input. thanks
I have been struggling with this for a few days and need help. My (NSArray) writeToFile: fails. I have written some test code that closely emulates the code I am trying to write. It basically takes an instance of a class, places it in an NSArray, then writes it to an NSURL. For good measure, the file at the given NSURL is first read.
Here is the code. (Xcode(4.3.3), command line tool. A file, "H.txt" with the words "This is a test" exists in a folder called "testPLIST" on the desktop.
Code:
#import <Foundation/Foundation.h>
@interface TESTClass : NSObject
@property(strong)NSString * code;
@property(strong)NSString * desc;
@end
@implementation TESTClass
@synthesize code, desc;
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString * path = [NSString stringWithString:@"/Users/m/Desktop/testPLIST/H.txt"];
TESTClass * one = [[TESTClass alloc]init];
[one setCode:@"123"];
[one setDesc:@"Foo Bar"];
NSArray *arrWithClass = [NSArray arrayWithObject:one];
NSURL * url = [NSURL fileURLWithPath:path];
NSLog(@"url:%@",url);
NSString *fileContents = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"File contents: %@", fileContents);
BOOL success = [arrWithClass writeToURL:url
atomically:YES];
if (success == YES)
NSLog(@"Suceeded");
else {
NSLog(@"Failed");
}
}
return 0;
}
output:
2012-07-15 15:35:33.138 TestPlistMaker[996:403] url:file://localhost/Users/m/Desktop/testPLIST/H.txt
2012-07-15 15:35:33.140 TestPlistMaker[996:403] File contents: This is a test.
2012-07-15 15:35:33.140 TestPlistMaker[996:403] Failed
Would appreciate any input. thanks