Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Etan Kissling
Got a strange issue here.

I have a small model class that stores a NSNumber.

@objc(Foo) final class Foo : NSObject {
dynamic var x = 0



override init() {
self.x = Int(arc4random())
}



init(x: Int) {
self.x = x
}
}


In my ViewController, there is a NSSet-based collection of such objects.

final class ViewController: NSViewController {
dynamic var foos: Set = [Foo(x: 0), Foo(x: 1), Foo(x: 2)]
}


Then, I wire this Set to the Content Set binding of an NSArrayController.

When I launch the app and print the Set in the debugger, I get an expected 
output.

(lldb) e/x foos
(Set) $R0 = {
  [0] = 0x60021080 {
ObjectiveC.NSObject = {
  NSObject = {
isa = 0x041d800101268d11 NSKVONotifying_Foo
  }
}
x = 0x0002
  }
  .
}


Now, I modify the Set via the NSArraycontroller by adding or removing an object.

When I now print the Set, strange things happen:

(lldb) e/x foos
(Set) $R1 = ([0] = , 
)

The set is still properly displayed, and strangely I can still print it from 
the app itself.

I can even create a copy of the set, that then displays like the first output 
again:

(lldb) e/x Set(foos.map { $0 })
(Set) $R2 = {
  [0] = 0x60021080 {
ObjectiveC.NSObject = {
  NSObject = {
isa = 0x051d800101268d11 NSKVONotifying_Foo
  }
}
x = 0x0002
  }
  
}



Unfortunately, stuff like querying the set (isSubsetOf), involving Foo's 
isEqual implementation is also broken now.


Can someone please explain what's going on here?



I've created a minimum sample project: https://scriptreactor.com/SetCompare.zip

Thanks

Etan




___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Quincey Morris
On Jan 8, 2016, at 07:14 , Etan Kissling  wrote:
> 
> In my ViewController, there is a NSSet-based collection of such objects.
> 
> final class ViewController: NSViewController {
>dynamic var foos: Set = [Foo(x: 0), Foo(x: 1), Foo(x: 2)]
> }

I don’t think it’s true that ‘foos’ is a NSSet. Rather, it’s a Swift Set that 
can be bridged to/from a NSSet, and it’s not clear (from any documentation I’ve 
read) that automatic bridging is going to occur when it’s accessed from a 
NSArrayController.

It may be necessary to declare ‘foos’ as a NSSet explicitly in this case.

___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Etan Kissling
As the comments in the sample project describe in the introductory comments,
modifying the code to force an NSSet does not solve the problem.



> On 08 Jan 2016, at 18:39, Quincey Morris 
>  wrote:
> 
> On Jan 8, 2016, at 07:14 , Etan Kissling  > wrote:
>> 
>> In my ViewController, there is a NSSet-based collection of such objects.
>> 
>> final class ViewController: NSViewController {
>>dynamic var foos: Set = [Foo(x: 0), Foo(x: 1), Foo(x: 2)]
>> }
> 
> I don’t think it’s true that ‘foos’ is a NSSet. Rather, it’s a Swift Set that 
> can be bridged to/from a NSSet, and it’s not clear (from any documentation 
> I’ve read) that automatic bridging is going to occur when it’s accessed from 
> a NSArrayController.
> 
> It may be necessary to declare ‘foos’ as a NSSet explicitly in this case.
> 



signature.asc
Description: Message signed with OpenPGP using GPGMail
___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Etan Kissling
In fact, it makes things even worse, and freezes up Xcode when expanding the 
set in the debugger.


> On 08 Jan 2016, at 18:59, Etan Kissling  wrote:
> 
> As the comments in the sample project describe in the introductory comments,
> modifying the code to force an NSSet does not solve the problem.
> 
> 
> 
>> On 08 Jan 2016, at 18:39, Quincey Morris 
>> > > wrote:
>> 
>> On Jan 8, 2016, at 07:14 , Etan Kissling > > wrote:
>>> 
>>> In my ViewController, there is a NSSet-based collection of such objects.
>>> 
>>> final class ViewController: NSViewController {
>>>dynamic var foos: Set = [Foo(x: 0), Foo(x: 1), Foo(x: 2)]
>>> }
>> 
>> I don’t think it’s true that ‘foos’ is a NSSet. Rather, it’s a Swift Set 
>> that can be bridged to/from a NSSet, and it’s not clear (from any 
>> documentation I’ve read) that automatic bridging is going to occur when it’s 
>> accessed from a NSArrayController.
>> 
>> It may be necessary to declare ‘foos’ as a NSSet explicitly in this case.
>> 
> 



signature.asc
Description: Message signed with OpenPGP using GPGMail
___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Etan Kissling
dynamic var foos: NSMutableSet = [Foo(x: 0), Foo(x: 1), Foo(x: 2)]

(lldb) e/x foos as! Set
Comparing 2 to 0
Comparing 1 to 0
(Set) $R0 = ([0] = , [1] 
= , [2] 
= )


On 08 Jan 2016, at 19:00, Etan Kissling 
mailto:kissl...@oberon.ch>> wrote:

In fact, it makes things even worse, and freezes up Xcode when expanding the 
set in the debugger.


On 08 Jan 2016, at 18:59, Etan Kissling 
mailto:kissl...@oberon.ch>> wrote:

As the comments in the sample project describe in the introductory comments,
modifying the code to force an NSSet does not solve the problem.



On 08 Jan 2016, at 18:39, Quincey Morris 
mailto:quinceymor...@rivergatesoftware.com>>
 wrote:

On Jan 8, 2016, at 07:14 , Etan Kissling 
mailto:kissl...@oberon.ch>> wrote:

In my ViewController, there is a NSSet-based collection of such objects.

final class ViewController: NSViewController {
   dynamic var foos: Set = [Foo(x: 0), Foo(x: 1), Foo(x: 2)]
}

I don’t think it’s true that ‘foos’ is a NSSet. Rather, it’s a Swift Set that 
can be bridged to/from a NSSet, and it’s not clear (from any documentation I’ve 
read) that automatic bridging is going to occur when it’s accessed from a 
NSArrayController.

It may be necessary to declare ‘foos’ as a NSSet explicitly in this case.




___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Quincey Morris
On Jan 8, 2016, at 09:59 , Etan Kissling  wrote:
> 
> As the comments in the sample project describe in the introductory comments,
> modifying the code to force an NSSet does not solve the problem.

a. I believe your debugger display problems are just that — problems in the 
debugger, not necessarily anything to do with your code (though certainly 
getting in the way). I’ve seen other cases where the debugger generates huge 
output (many megabytes of text) and ties Xcode up for 20 minutes or more while 
doing it.

b. With your original Set code, the set does indeed change from a Swift 
internal implementation to a _XXX_NSSet implementation after you add an entry 
via the array controller, though this is probably irrelevant.

c. With your Set code, you should probably be using ==, not isEqual, even 
though the Foos are NSObjects.

d. For NSObject equality, the hash getter method is called ‘hash’, not 
‘hashValue’.

I don’t know offhand if Swift tries to bridge between ‘hash’ and ‘hashValue’ 
under any circumstances. (It seems like it must, otherwise I don’t see how Set 
and Set<> could bridge properly, but I admit I haven’t taken the time to try to 
think this through.) However, I suspect that your immediate problems are cause 
by the lack of a ‘hash’ method. Certainly, when I changed the Set variable 
to NSMutableSet, your hashValue method wasn’t ever called.


___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Etan Kissling
Wow, thanks for the various hints.

a. Could be :-) However, since the Find feature of the minimal app stops 
working properly after modifying the collection, there's definitely something 
fishy.

b. Yep, the dynamic keyword can afaik only be applied to types that are 
representable in objc.

c. Hm. Not sure. == on NSObject subclasses is broken. See 
https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20151214/000515.html

d. THANKS ! <3 That's it!

Swift's hashValue probably forwards to ObjC's hash, but not vice versa. That's 
why it breaks in a strange way when I only override Swift's hashValue.
As soon as the set is bridged to ObjC, the default hash is used and not my 
customized one.

Etan


On 08 Jan 2016, at 20:24, Quincey Morris 
mailto:quinceymor...@rivergatesoftware.com>>
 wrote:

On Jan 8, 2016, at 09:59 , Etan Kissling 
mailto:kissl...@oberon.ch>> wrote:

As the comments in the sample project describe in the introductory comments,
modifying the code to force an NSSet does not solve the problem.

a. I believe your debugger display problems are just that — problems in the 
debugger, not necessarily anything to do with your code (though certainly 
getting in the way). I’ve seen other cases where the debugger generates huge 
output (many megabytes of text) and ties Xcode up for 20 minutes or more while 
doing it.

b. With your original Set code, the set does indeed change from a Swift 
internal implementation to a _XXX_NSSet implementation after you add an entry 
via the array controller, though this is probably irrelevant.

c. With your Set code, you should probably be using ==, not isEqual, even 
though the Foos are NSObjects.

d. For NSObject equality, the hash getter method is called ‘hash’, not 
‘hashValue’.

I don’t know offhand if Swift tries to bridge between ‘hash’ and ‘hashValue’ 
under any circumstances. (It seems like it must, otherwise I don’t see how Set 
and Set<> could bridge properly, but I admit I haven’t taken the time to try to 
think this through.) However, I suspect that your immediate problems are cause 
by the lack of a ‘hash’ method. Certainly, when I changed the Set variable 
to NSMutableSet, your hashValue method wasn’t ever called.



___

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

Obtaining a value from a hex representation

2016-01-08 Thread Carl Hoefs
A user enters the hexadecimal representation of a number like "0x31C8FD" into a 
text field. The intValue of such strings is 0. Is there a Cocoa method (like 
.hexValue) to convert the hex representation string into a value?

-Carl


___

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: Modifying Set via NSArrayController breaks Set in weird way

2016-01-08 Thread Quincey Morris
On Jan 8, 2016, at 13:44 , Etan Kissling  wrote:
> 
> a. Could be :-) However, since the Find feature of the minimal app stops 
> working properly after modifying the collection, there's definitely something 
> fishy.

I suspect the inability to display Set members comes from the fact that the 
debugger information says that the implementation is a Swift Set object, and 
the debugger isn’t capable of dealing with the situation where the actual 
object is a NSSet subclass**. Precisely where the bug is, is hard to say, but a 
bug report about the debugger seems like a good place to start.

The Xcode-hanging problem is probably a second bug. If the debugger thinks the 
object is a different class of object, then it may try to follow pointers, and 
that can take it very far afield. (After my bug report about this tendency, I 
was told that the debugger didn’t follow pointers for reference types by 
default. Apparently this meant that the 32 MB of output I attached to the bug 
report didn’t really exist.)

The debugger also seems to (still) have the bug where it confuses local 
variables that have the same name. In the method where you unwrap an optional 
‘object’ into a non-optional object, it told me that ‘object’ was a view 
controller, at the start of the method. It turned into a Foo a couple of lines 
later.

So, yes, multiple fishy things. Your sample project seems like excellent 
supporting evidence for bug reports.

> Swift's hashValue probably forwards to ObjC's hash, but not vice versa. 
> That's why it breaks in a strange way when I only override Swift's hashValue.
> As soon as the set is bridged to ObjC, the default hash is used and not my 
> customized one.

After I thought about this some more, I suspect there’s no direct built-in link 
between ‘hash’ and ‘hashValue’. My guess is that if you use ‘==‘ it uses 
‘hashValue’ for Swift objects, but that if ‘==' internally uses isEqual because 
the underlying objects are NSObjects, it’s going to use ‘hash’ automatically 
(modulo the misbehavior in the Swift bug report you mentioned).

___

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: Obtaining a value from a hex representation

2016-01-08 Thread Steve Mills

On Jan 08, 2016, at 03:58 PM, Carl Hoefs  wrote:

A user enters the hexadecimal representation of a number like "0x31C8FD" into a 
text field. The intValue of such strings is 0. Is there a Cocoa method (like .hexValue) 
to convert the hex representation string into a value?

Either use NSScanner or strtol if you have char* (or strtoll if it's longer 
than 8 hex chars).
Sent from iCloud's ridiculous UI, so, sorry about the formatting

 
___

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: Obtaining a value from a hex representation

2016-01-08 Thread Jens Alfke

> On Jan 8, 2016, at 1:58 PM, Carl Hoefs  wrote:
> 
> A user enters the hexadecimal representation of a number like "0x31C8FD" into 
> a text field. The intValue of such strings is 0. Is there a Cocoa method 
> (like .hexValue) to convert the hex representation string into a value?

-[NSScanner scanHexInt:].

—Jens
___

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: Obtaining a value from a hex representation

2016-01-08 Thread Quincey Morris
On Jan 8, 2016, at 13:58 , Carl Hoefs  wrote:
> 
> Is there a Cocoa method (like .hexValue) to convert the hex representation 
> string into a value?

There are the ‘scanHex…’ methods in NSScanner. It’s a bit annoying to set up, 
but if you packaged it as a function it wouldn’t be bad. OTOH, if you go to 
that trouble, you may as well just examine the characters directly, assuming 
you just need ASCII digits.

___

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: Obtaining a value from a hex representation

2016-01-08 Thread Carl Hoefs
On Jan 8, 2016, at 3:16 PM, Steve Mills  wrote:
> 
> Either use NSScanner or strtol if you have char* (or strtoll if it's longer 
> than 8 hex chars).
> Sent from iCloud's ridiculous UI, so, sorry about the formatting
> 
Hadn't thought to use NSScanner, though it appears a bit overkill. 

strtoll() works fine. (Note: Hex values greater than 7FFF require the use 
of strtoll(). strtol() returns -1 in such cases.)

Guess I'll create a NSString category so I can have a -hexValue method after 
all. 

Thanks Steve, Quincey, Jens!
-Carl


___

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