PDA

View Full Version : (NSArray)writeToFile: issue




mdeh
Jul 15, 2012, 06:02 PM
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.




#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



chown33
Jul 15, 2012, 06:21 PM
See the reference doc for NSArray's writeToXXX methods. What types of objects (i.e. what classes) are supported? Is your object one of the supported types?

Property List Programming Guide (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html%23//apple_ref/doc/uid/10000048i-CH3-SW2). Read the "What is a Property List" heading under "About Property Lists".

lee1210
Jul 15, 2012, 06:29 PM
The docs for that method state that:
NSData
NSString
NSDictionary
NSArray

Are property list objects. TESTClass is not. My immediate reaction is to serialize to NSData or NSString first, then make an NSArray and write it to the plist. You'd also need to write code to deserialize on the other side. So what's the recommended solution? It seems like NSData serialization is the suggested method. Check the link below.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/SerializePlist/SerializePlist.html

-Lee

chown33 beat me to it. I typed most of this then mobile safari reloaded the page after I researched a link.

mdeh
Jul 15, 2012, 06:38 PM
The docs for that method state that:
NSData
NSString
NSDictionary
NSArray

Are property list objects. TESTClass is not. My immediate reaction is to serialize to NSData or NSString first, then make an NSArray and write it to the plist. You'd also need to write code to deserialize on the other side. So what's the recommended solution? It seems like NSData serialization is the suggested method. Check the link below.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/SerializePlist/SerializePlist.html

-Lee

chown33 beat me to it. I typed most of this then mobile safari reloaded the page after I researched a link.


Thank you Lee and Chown. I knew that but, of course, conveniently overlooked the fact that "Test class" is not one of the allowed data types!!!
Thanks