Re: What type category should I use for my model in Swift?
Re: What type category should I use for my model in Swift?
- Subject: Re: What type category should I use for my model in Swift?
- From: Quincey Morris <email@hidden>
- Date: Thu, 02 Jun 2016 09:46:52 -0700
- Feedback-id: 167118m:167118agrif8a:167118soTtUcg2SC:SMTPCORP
On Jun 2, 2016, at 08:55 , Daryle Walker <email@hidden> wrote:
>
> My model in mind is dumb data, so a struct seems appropriate.
The thing is, though, that Swift value objects are no dumber than reference objects (that is, structs can have behavior just like classes). Conversely, in Swift, the lack of behavior doesn’t really make structs a better choice for dumb data representation.
> But your various Cocoa subclass would need to reference the model, and using a struct means it’s by value instead of reference, so coordinating changes would get harder.
I think the question you need to ask yourself is whether the data represents the value of something, or whether it represents (a unique) something.
Consider a document-based application where the document files where (for simplicity) the contents of each file is a simple string. It’s true that a string is a value, so that would seem to suggest that the data model should simply be a String (value).
However, the String represents the contents of its *file*, which is a unique thing — a copy of a file is a different file, even if the contents are the same value. That means the correct data model in this case is a class (reference) with a String property (value).
Note that you can “simulate” the latter with the former, if you do some combination of (a) passing the String variable around by reference [!], or (b) mutating the string via accessor functions that implicitly reference [!] the string. But, generally, if you approach it this way, you may as well have used a reference object to begin with.
It seems to me there are cases where you it’s hard to decide whether something is a unique thing within your app design or not. In those circumstances, the correct answer is probably to use the representation that is most convenient to code for.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden