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

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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
 
No.

Check the source link. About half way down the page.

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.
 
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?
 
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?

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