Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.


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
 
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...pertyLists/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.
 
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...pertyLists/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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.