1

Hi everyone I have a really strange problem. I create a new subclass of NSObject, but I just cannot use "self.someproperty" in the implementation. Normally when I type the word "self", Xcode will guess what I'm typing and give me the property name after dot. But in this case, it doesn't and give me an red error. I have check my code for a night and give up now. So, wish some one give me a advise please let me know this might be a small problem somewhere?

Here's my code:

#import <Foundation/Foundation.h>

@interface FlickrPhotoCache : NSObject

+(BOOL)isInCache:(NSDictionary *)photo;

@end



#import "FlickrPhotoCache.h"

@interface FlickrPhotoCache()

@property (nonatomic, strong) NSMutableArray *photos;

@end

@implementation FlickrPhotoCache
@synthesize photos = _photos;

+(BOOL)isInCache:(NSDictionary *)photo
{
    self.photos // here I get error
}

@end

Thanks in advance! WHT

WHT
  • 63
  • 1
  • 7
  • Actually when I typing "self", the real time help bar below shows it's a "const class", which should be "FlickrPhotoCache" in this case. And the auto correction wants to make it to "self->photos"... – WHT Apr 20 '12 at 13:29
  • thank luke self._photos not work – WHT Apr 20 '12 at 13:31

1 Answers1

1

You're using + when declaring the method which is for static (class) methods not instance methods self only exists in the instance methods.

Use

 -(BOOL)isInCache:(NSDictionary *)photo;

When declaring to use the instance variable self for properties.

And then

-(BOOL)isInCache:(NSDictionary *)photo
{
    self.photos;
}

Works just fine.

lukecampbell
  • 14,728
  • 4
  • 34
  • 32
  • yeah, this is the reason up! thank you. then I have to create a class instance when I call it? What i was thinking is to call it like [FlickrPhotoCache isInCache:aPhoto] in another class. I just think unnecessary to create a class instance. Any recommendation? – WHT Apr 20 '12 at 13:44
  • sorry Luke I just cannot vote up right now. I'll do it as soon as I have 15 or more reputations. – WHT Apr 20 '12 at 13:45
  • Yeah make a class variable to hold whatever you're trying to use like a `static struct` in C. Like `@interface FlickrPhotoCache : NSObject + NSArray *knownPhotos; @end` or something. See for http://stackoverflow.com/questions/6035824/objective-c-static-class-variables for a better explanation. – lukecampbell Apr 20 '12 at 14:18