0

Here's my model class LevelInformationModel.

     @interface LevelInformationModel : NSObject

     @property NSInteger levelCompleted;
     +(id)sharedModel;

     @end


     #import "LevelInformationModel.h"

     @implementation LevelInformationModel

     @synthesize levelCompleted;

     /* Return singleton model */
     + (id)sharedModel {
         static LevelInformationModel *sharedModel = nil;
         static dispatch_once_t onceToken;
         dispatch_once(&onceToken, ^{
             sharedModel = [[self alloc] init];
         });
         return sharedModel;
     }

     - (id)init {
        self = [super init];
        if (self) {
            self.levelCompleted = 0;
        }
        return self;
     }

     @end

And here's how I'm using it all (in GameViewController class). I have imported LevelInformationModel.h already.

     NSInteger currentLevel = [LevelInformationModel sharedModel].levelCompleted;

But above the levelCompleted property is the error Property 'levelCompleted not found on type 'id'`. Any thoughts would be great.

Frank
  • 107
  • 9

2 Answers2

2

That's because your method is returning an id.

Change it to this

+(LevelInformationModel)sharedModel 
{
.
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • 2
    You should get into the habit of using `instanceType`, rather than id or the actual class name. – Mike Jun 18 '16 at 21:58
  • @Frank If you feel this answer helped solved your issue, please mark it as 'accepted' by clicking the green check mark. This will help the community to keep the focus on unanswered questions. – Lahiru Jayaratne Jun 22 '16 at 03:43
1

You should use +(instancetype)sharedModel;

"There definitely is a benefit. When you use 'id', you get essentially no type checking at all. With instancetype, the compiler and IDE know what type of thing is being returned, and can check your code better and autocomplete better."

Would it be beneficial to begin using instancetype instead of id?

https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html

Community
  • 1
  • 1
Proton
  • 1,335
  • 1
  • 10
  • 16