initWithCoder: not visible in NSObject?
Asked Answered
U

1

6

I have an interface:

#import <Foundation/Foundation.h>

@interface Picture : NSObject;

@property (readonly) NSString *filepath;
- (UIImage *)image;

@end

and implementation:

#import "Picture.h"

#define kFilepath @"filepath"

@interface Picture () <NSCoding> {
    NSString *filepath;
}

@end


@implementation Picture
@synthesize filepath;

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    return self;

}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:filepath forKey:kFilepath];
}

- (UIImage *)image {
    return [UIImage imageWithContentsOfFile:filepath];
}

@end

I get error: ARC issue - No visible @interface for 'NSObject' declares the selector' initWithCoder:'

Is there something different about NSCoding when using ARC? Thanks

Unutterable answered 28/4, 2012 at 11:16 Comment(0)
B
14

There's nothing different between ARC and manual reference counting. NSObject doesn't conform to NSCoding and therefore doesn't supply -initWithCoder: or -encodeWithCoder:. Just don't call through to the superclass implementations of those methods.

- (id)initWithCoder: (NSCoder *)aCoder {
    self = [super init];
    if (self) {
        [aCoder decodeObject: filepath forKey: kFilepath];
    }
    return self;
}
Brame answered 28/4, 2012 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.