Subclassing UIDocument

Discussion in 'iOS Programming' started by nashyo, Jul 31, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I'm running through a RayWenderlich tutorial and I'm a little confused.

    Code:
    #import "PTKDocument.h"
    #import "PTKData.h"
    #import "PTKMetadata.h"
    #import "UIImageExtras.h"
     
    #define METADATA_FILENAME   @"photo.metadata"
    #define DATA_FILENAME       @"photo.data"
     
    @interface PTKDocument ()
    @property (nonatomic, strong) PTKData * data;
    @property (nonatomic, strong) NSFileWrapper * fileWrapper;
    @end
     
    @implementation PTKDocument 
    @synthesize data = _data;
    @synthesize fileWrapper = _fileWrapper;
    @synthesize metadata = _metadata;
    
    - (void)encodeObject:(id<NSCoding>)object toWrappers:(NSMutableDictionary *)wrappers preferredFilename:(NSString *)preferredFilename {
        @autoreleasepool {            
            NSMutableData * data = [NSMutableData data];
            NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            [archiver encodeObject:object forKey:@"data"];
            [archiver finishEncoding];
            NSFileWrapper * wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:data];
            [wrappers setObject:wrapper forKey:preferredFilename];
        }
    }
     
    - (id)contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
     
        if (self.metadata == nil || self.data == nil) {        
            return nil;    
        }
     
        NSMutableDictionary * wrappers = [NSMutableDictionary dictionary];
        [self encodeObject:self.metadata toWrappers:wrappers preferredFilename:METADATA_FILENAME];
        [self encodeObject:self.data toWrappers:wrappers preferredFilename:DATA_FILENAME];   
        NSFileWrapper * fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];
     
        return fileWrapper;
     
    }
    Firstly, @autoreleasepool shouldn't be used in iOS5.1 SDK right?

    Secondly, Ray goes on to say this

    "Take a look at contentsForType first. To create a directory NSFileWrapper, you call initDirectoryWithFileWrapper and pass in a dictionary that contains file NSFileWrappers as the objects, and the names of the files as the key. We use a helper method encodeObject:toWrappers:preferredFilename to create a file NSFileWrapper for the data and metadata."

    initDIrectoryWithFileWrapper is not coded anywhere, so what is he reffering to here?

    Thanks

    Source: http://www.raywenderlich.com/12779/icloud-and-uidocument-beyond-the-basics-part-1
     
  2. ConCat macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #2
    Do you mean "initDirectoryWithFileWrappers:"?
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    No.

    Check the source link. About half way down the page.
     
  4. ConCat macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #4
    well, your code has "NSFileWrapper * fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:wrappers];"

    I am pretty sure he meant initDirectoryWithFileWrappers. As for @autoreleasepool, every new application has one, but it's in your main.m file in the "supporting files" folder. Don't worry about it.
     
  5. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    Ah right! I thought Ray could never be wrong :p

    As for autorelease pool...I can't just let it go. If it is in main.m, then why mentioned it in this method if I'm using ARC?
     
  6. ConCat macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #6
    Okay, I don't know why this is, but it no longer seems to be in main.m. It's supposed to be, and it was in previous versions of Xcode, but in the latest version (4.4) on mountain lion it isn't there.

    I suppose it must just be implied now, since ARC is working fine... It is incorrect to specify it within a method like that though. I don't think the author completely understands ARC.
     

Share This Page