Re: Best way to have KVC-compliant enum property in Swift 2?

2015-07-29 Thread Rick Mann

> On Jul 28, 2015, at 19:38 , Quincey Morris 
>  wrote:
> 
> I suspect that downloadState will never need to be set via KVC**, or from 
> Obj-C code, in which case there’s a slightly simpler solution than Charles’ 
> suggestion. You can have 2 properties, one of which is an enum, and other is 
> an immutable Int that’s observable, whose value is maintained via a simple 
> didSet accessor on the enum property.

Thanks, Quincey, that's pretty much what I want. My view observes these 
properties to know when to update.

Thanks to Charles, too.

-- 
Rick Mann
rm...@latencyzero.com



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Best way to have KVC-compliant enum property in Swift 2?

2015-07-28 Thread Quincey Morris
On Jul 28, 2015, at 18:07 , Rick Mann  wrote:
> 
>dynamic var thumbnailURL:   NSURL?
>dynamic var numFiles:   NSNumber?
>dynamic var filesDownloaded :   NSNumber?
>dynamic var downloadState   :   NSNumber?   =   
> DownloadState.notDownloaded.rawValue

I think you really should start from this:

>dynamic var thumbnailURL:   NSURL?
>dynamic var numFiles:   Int
>dynamic var filesDownloaded :   Int
>dynamic var downloadState   :   Int   =   
> DownloadState.notDownloaded.rawValue


KVO already has the ability to transform properties with a simple numeric value 
into objects as necessary. You don’t need to reinvent this wheel.

However, the problem with the enum is not that it’s a scalar value, but that 
it’s not representable in Obj-C. 

I suspect that downloadState will never need to be set via KVC**, or from Obj-C 
code, in which case there’s a slightly simpler solution than Charles’ 
suggestion. You can have 2 properties, one of which is an enum, and other is an 
immutable Int that’s observable, whose value is maintained via a simple didSet 
accessor on the enum property.

An alternative solution is to simply make downloadState an Int, not an enum 
(with class static Int vars for the allowable values). Yes, it’s less type-safe 
than an enum, but the KVO compatibility requirement may be paramount here. I’ve 
noticed that Swift tends to over-inspire us with tendencies towards 
over-generality and over-correctness. Sometimes the pragmatic solution is 
better.


** If it is, you can’t get the type safety of a Swift enum anyway, so I’d 
definitely use the Int-only solution of the last paragraph, given the 
relationship to KVC/KVO.



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Best way to have KVC-compliant enum property in Swift 2?

2015-07-28 Thread Charles Srstka
> On Jul 28, 2015, at 8:57 PM, Charles Srstka  wrote:
> 
>   func valueForKey(key: String) -> AnyObject? {
> 
>   func setValue(value: AnyObject?, forKey key: String) {

D’oh — I forgot to put “override” in front of these. Old Objective-C habits die 
hard. Sorry about that.

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Best way to have KVC-compliant enum property in Swift 2?

2015-07-28 Thread Charles Srstka
> On Jul 28, 2015, at 8:07 PM, Rick Mann  wrote:
> 
> I'd like to have a Swift enum that lays out a set of states, and a property 
> of that type on an object that is KVObservable (i.e. dynamic). I don't think 
> this is possible. What I settled on was this:
> 
> class
> Model : MPObject
> {
>enum
>DownloadState : NSNumber
>{
>case notDownloaded  =   0
>case downloadStarted=   1
>case downloadComplete   =   2
>case downloadError  =   3
>}
> 
>dynamic var thumbnailURL:   NSURL?
>dynamic var numFiles:   NSNumber?
>dynamic var filesDownloaded :   NSNumber?
>dynamic var downloadState   :   NSNumber?   =   
> DownloadState.notDownloaded.rawValue
> }
> 
> But that's kinda gross. Any better approaches? Thanks!

It’s a lot of boilerplate, but you could do something like this:

(disclaimer: written in Mail)

class Model: MPObject {
enum DownloadState : Int {
… cases ...
}

var downloadState: DownloadState = .notDownloaded {
willSet {
self.willChangeValueForKey("downloadState")
}
didSet {
self.didChangeValueForKey("downloadState")
}
}

func valueForKey(key: String) -> AnyObject? {
if key == "downloadState" {
return NSNumber(integer: self.downloadState.rawValue)
} else {
return super.valueForKey(key)
}
}

func setValue(value: AnyObject?, forKey key: String) {
if key == "downloadState" {
guard let rawValue = value as? Int, state = 
DownloadState(rawValue: rawValue) else {
… handle this condition somehow …
}

self.downloadState = state
} else {
super.setValue(value, forKey: key)
}
}
}

I think this should allow a native Swift enum to be KVC-compliant.

Alternative way to do it, with a little less boilerplate but an extra property:

class Model: MPObject {
enum DownloadState : Int {
… cases ...
}

var downloadState: DownloadState = .notDownloaded {
willSet {
self.willChangeValueForKey("kvcDownloadState")
}
didSet {
self.didChangeValueForKey("kvcDownloadState")
}
}

dynamic var kvcDownloadState: Int {
get {
return self.downloadState.rawValue
}
set(rawValue) {
guard let state = DownloadState(rawValue:rawValue) else 
{
… handle this condition somehow ...
}

self.downloadState = state
}
}
}

Charles


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Best way to have KVC-compliant enum property in Swift 2?

2015-07-28 Thread Rick Mann
I'd like to have a Swift enum that lays out a set of states, and a property of 
that type on an object that is KVObservable (i.e. dynamic). I don't think this 
is possible. What I settled on was this:

class
Model : MPObject
{
enum
DownloadState : NSNumber
{
case notDownloaded  =   0
case downloadStarted=   1
case downloadComplete   =   2
case downloadError  =   3
}

dynamic var thumbnailURL:   NSURL?
dynamic var numFiles:   NSNumber?
dynamic var filesDownloaded :   NSNumber?
dynamic var downloadState   :   NSNumber?   =   
DownloadState.notDownloaded.rawValue
}

But that's kinda gross. Any better approaches? Thanks!

-- 
Rick Mann
rm...@latencyzero.com



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com