1

All the examples I've come across so far ( including Stanford podcasts ) reference a Model by declaring it as a property of a View Controller and using it from there:

#import "myClass.h"    // assume it carries a single NSString property

@interface
@property (nonatomic,strong) myClass *myobject;
@end

@implementation ViewController

-(void)viewDidLoad {
self.myObject = [[myClass alloc] init]
.
.   
.
-(void)someMethod{
displayLabel = self.myObject.myString; 

seems like that's more self.C-V than M-V-C.
After messin' about on my own this works:

#import "myClass.h"

@implementation ViewController {
MyClass *myObject;
}

-(void)viewDidLoad {
myObject = [[myClass alloc] init]
}
.
.
.
-(void)someMethod{
displayLabel = myObject.myString;

my question is; is there any danger in using second example? or to ask differently, does it give the compiler an easier task of keeping MODEL separate from VIEW and CONTROLLER?

aremvee
  • 179
  • 1
  • 13
  • If you're a student, are you as yet familiar with **singletons**? Almost always, on mobile, a singleton is the best way to approach your "model". (Since among other reasons singletons are the natural heart of mobile device engineering.) (And double ditto for game engineering.) Regarding your specific question, you should definitely use a property, and not a "ordinary variable". {As a rule, from now on, 2014, you should pretty much only ever use properties for anything - at all. Forget "ordinary variables".} – Fattie Jun 01 '14 at 08:05
  • Regarding the standford code, I'd pretty much just dismiss it - it's all hopeless. The example above is a bit like when text books ask things like "what is the combined weight of planet A and planet B?!" - ie, silly.) It's very unlikely (indeed, impossible/silly) that a view will create it's model. Other than in hello world programs the model is a thing that hangs around like, err, heaven. Hope it helps! – Fattie Jun 01 '14 at 08:06
  • I'll look into it - every time Singletons are mentioned, always is coming some dude who says one should never resort to them. So i didn't. – aremvee Jun 01 '14 at 08:33
  • 1
    "every time Singletons are mentioned, always is coming some dude who says one should never resort to them" Yeah, it's the sort of utter nonsense you see on the internet. In iOS almost everything is a singleton (notably .. "the app", also all the physical mobile stuff like "accelerometer" etc etc). **BY ALL MEANS** one can use singletons incorrectly (poor syntax, poor understanding). But then, the "else if" statement is used incorrectly extremely often. – Fattie Jun 01 '14 at 09:16
  • I clicked through your profile to a reply to [ this ](http://stackoverflow.com/questions/4142177/difference-between-self-ivar-and-ivar/4142614#4142614) and found it more closely resembled my mystery. Incidentally your last comment touches upon another question I may ask about the relationship between app delegate and 'main'. – aremvee Jun 01 '14 at 09:22
  • Yeah, the app delegate itself, is indeed a singleton. I'm not sure what you mean by "main". I'll officially put in an answer so you can close it out if you want to keep the site tidy! – Fattie Jun 01 '14 at 09:46
  • @JoeBlow Wow that escalated quickly - the OP was essentially querying the difference between direct access and using properties and you've jumped to Singletons. I'm half inclined to think this is an elaborate troll? – Paul.s Jun 01 '14 at 10:53
  • @aremvee make sure you do a good amount of research on singletons before you dive into using them everywhere. In my experience the apps my team creates have always benefitted from going the extra mile to remove singletons where possible (it's not always possible) – Paul.s Jun 01 '14 at 11:42
  • (Paul, if you're referring to the fact that "teenage beginner programmers sometimes use 'singletons' because they need a global to hold the score!!" .. well sure. Don't badly-use singletons, goto statements, raw memory access, PlayerPrefs, or any of the other common gotchyas. My personal pet-peeve is poor use of "else if".) – Fattie Jun 01 '14 at 12:45

2 Answers2

1

There are a couple of implementation details that are different between your two examples but they are essentially doing the exact same thing.

In both cases you are declaring a backing ivar. This line @property (nonatomic,strong) myClass *myobject; will implicitly @synthesize myObject = _myObject;, which is similar to you manually writing:

@interface ViewController : UIViewController {
  MyClass *_myObject;
}

// or

@implementation ViewController {
  MyClass *_myObject;
}

The only other difference is that @property (nonatomic,strong) myClass *myobject; will also create the accessor methods for you

- (void)setMyObject:(MyClass *)myObject;
- (MyObject *)myObject;

This is indeed still MVC but controllers that are subclasses of UIViewController always manage at least one view. The M component is your myObject instance. As in most diagrams the Controller sits there managing the communications between the V and M both of which the controller owns

Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • That's valuable to know. I've bought (paid) for a number of books including those that start at Java and they don't crystallise it quite so succinctly. Now to close this thread up which answer to give the Big Tick? – aremvee Jun 02 '14 at 08:47
0

Using singletons or holding the model as a property of the controller is also a question of lifetime. I prefer using singletons for helper classes (getting data from game center or fetching data from core data). Singletons are useful for things you need to access from different places within your app (where lifetime of your singleton is longer than your view controller's lifetime). But for view controller dependent models you can of course use them as properties of the view controller. Let's say you have ten view controllers in your app each displaying completely different content it does absolutely not make sense to keep all data for all possible view controllers in memory (in a singleton) just to have data ready in case the user wants to see any of the view controllers. In this case it's no shame to load your model's data from within your view controller implementation and hold it as a property. This guarantees that data is auto released when the view controller's lifetime ends and avoids conflicts. Holding data in singleton would make sense when you display data loaded from a server that does not need to be refreshed everytime you present your data to reduce the amount of traffic generated by loading the data. Using singletons might be dangerous regarding thread safety e.g. when data is mutated from a background thread while iterating over your datasource object to refresh a table view's content. Singletons can also lead to tigh coupling which should be avoided. Using an instance variable instead of a property is still a good choice if you want to hold a weak reference to an object as it is automatically set to nil if the referenced object gets auto released. A weak property would in this case lead to bad access.

RTasche
  • 2,614
  • 1
  • 15
  • 19
  • 1
    "What other sort of apps are there?" - Let's say you have a menu controller for in-app navigation that has its menu items stored in a plist file would you create a singleton for it? I would read the plist and store the items in an arry on init to have them displayed in a table view. – RTasche Jun 01 '14 at 11:48
  • What I talked about was storing the objects in a singleton, not fetching them from persistence / server. Let's say you have a game with ten leaderboards. The user gets a view controller displaying all leaderboards to select one to display the scores. In this case a singleton helper class that fetches the scores for the leaderboard and returns them in an arry makes sense. So you store this returned array as a vc property and display the scores in a table view. But imho it would not make sense to fetch all leaderboards scores and cache them in the singleton to have the ready to display... – RTasche Jun 01 '14 at 11:58
  • it's not necessary to have those singletons at all. I mean you could also create an instance of your helper class in your view controller base class and have the helper available in every view controller subclass. you act as if nothing would be possible without using singletons. – RTasche Jun 01 '14 at 12:58
  • What you're explaining to have everything available locally all time seems like waste of memory and calculation capacity. I often have to support older devices such as iPhone 3GS. On these devices the user notices every bit of "background processing" by delays. So I usually confine myself to load and process data that's actually needed and not data I (the programmer) think the user could ever need at some point of time. Imagine you have a slow mobile connection and you want to download data for all leaderboards or whatever at once instead of the one you actually need, it's a pain for the user! – RTasche Jun 01 '14 at 13:17
  • I've given this answer The Gong for the line ".. In this case it's no shame to load your model's data from within your view controller implementation and hold it as a property". Let me refine my original question. What are the ways my VC is aware of my M,? not - what form the M should take, be it a singleton of otherwise. There is the way using the property of the model in the VC and @JoeBlow reveals a magic Class Method in conjunction with some magic entries in the Prefix.pch. – aremvee Jun 02 '14 at 09:42
  • I'd nowadays say this answer is basically wrong. Views (and VCs) are nothing. If there are 1, 10 or 20 views on a screen - they were **put there** by the brains of the program, the model itself. It's that simple really. Completely setting aside the singleton issue, whatever your model is: it's the very thing that plops down or eliminates views. (Sure, in a simple app there may just be the one view already there at launch, but it's there "because of" and run by the model. The model is the "boss", that's really all there is to it. – Fattie Jan 02 '16 at 04:53
  • Hence, ***"Let's say you have ten view controllers in your app each displaying completely different content it does absolutely not make sense to keep all data for all possible view controllers in memory (in a singleton) just to have data ready in case the user wants to see any of the view controllers."*** the ten views were only brought up in to existence because the "boss" of the app (the model) decided they should be brought in to existence (perhaps because of viewer activity, news from the cloud, or whatever). – Fattie Jan 02 '16 at 04:54