(NSArray)writeToFile: issue

Discussion in 'Mac Programming' started by mdeh, Jul 15, 2012.

  1. macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    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
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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. Read the "What is a Property List" heading under "About Property Lists".
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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.
     
  4. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #4

    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
     

Share This Page