1

I have a singleton in place and want to store a UIImage in the singleton. Somehow this does not work. I get the Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto' Interestingly working with my NSMutableArray on the singleton works fine.

How can I store a UIImage on my singleton to access it later from another class?

Singleton.h

#import <Foundation/Foundation.h>

@interface SingletonClass : NSObject

@property (strong, nonatomic) NSMutableArray *myArray;
@property (strong, nonatomic) UIImage *photo;

+ (id)sharedInstance;
-(void)setPhoto:(UIImage *)photo    

@end

Singleton.m

#import "SingletonClass.h"

@implementation SingletonClass

static SingletonClass *sharedInstance = nil;

// Get the shared instance and create it if necessary.
+ (SingletonClass *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }

    return sharedInstance;
}

// We can still have a regular init method, that will get called the first time the Singleton is used.
- (id)init
{
    self = [super init];

    if (self) {
        // Work your initialising magic here as you normally would
        self.myArray = [[NSMutableArray alloc] init];
        self.photo = [[UIImage alloc] init];
    }

    return self;
}

// We don't want to allocate a new instance, so return the current one.
+ (id)allocWithZone:(NSZone*)zone {
    return [self sharedInstance];
}

// Equally, we don't want to generate multiple copies of the singleton.
- (id)copyWithZone:(NSZone *)zone {
    return self;
}

-(void)setPhoto:(UIImage *)photo {
    photo = _photo;
}

DetailView.m

-(void)sharePhoto:(id)sender {

    SingletonClass *sharedSingleton = [SingletonClass sharedInstance];

    [sharedSingleton.photo setPhoto:self.imageView.image];
    //Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto'

    [self.navigationController popViewControllerAnimated:YES];

}
jerik
  • 5,714
  • 8
  • 41
  • 80
  • 1
    change `[sharedSingleton.photo setPhoto:self.imageView.image]` to `[sharedSingleton setPhoto:self.imageView.image]` – Jonathan Cichon Jul 12 '13 at 08:16
  • Something that should be noted is that you're not using a singleton class properly. In Objective-C, the fastest way to use a singleton is through `dispatch_once`. – jakenberg Jul 12 '13 at 08:42
  • I tried `dispatch_once` like stated in the [following thread](http://stackoverflow.com/questions/9119042/why-does-apple-recommend-to-use-dispatch-once-for-implementing-the-singleton-pat) but it did not work. The app hung-up. – jerik Jul 12 '13 at 09:26

3 Answers3

1

By invoking [sharedSingleton.photo setPhoto:self.imageView.image]; you are basically doing this:

UIImage *theImage = sharedSingleton.photo;
[theImage setPhoto:self.imageView.image];

So you are not calling setPhoto: on your SingletonClass but on the returned UIImage. Seems wrong.

You probably want: [sharedSingleton setPhoto:self.imageView.image];.

And then, I'm a little confused about this method:

-(void)setPhoto:(UIImage *)photo { photo = _photo; }

First, you probably won't need it, since you have a @property. Second you set the argument (photo) to the variable (_photo). Wrong way around?

fguchelaar
  • 4,779
  • 23
  • 36
0

Change the method to following:

-(void)sharePhoto:(id)sender {
SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
[sharedSingleton setPhoto:self.imageView.image];
//Compiler Error: No visible @interface for 'UIImage' declares the selector 'setPhoto'
[self.navigationController popViewControllerAnimated:YES];

}

With this line [sharedSingleton.photo setPhoto:self.imageView.image]; you are actually try to find photo property in sharedSingleton.photo which is actually not declared in that and hence it gives error.

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
0

In DetailView.m

-(void)sharePhoto:(id)sender {

[[SingletonClass sharedInstance] setPhoto:self.imageView.image];
[self.navigationController popViewControllerAnimated:YES];

}

crz
  • 438
  • 5
  • 18