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

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.