Re: More elegance than a long if/else

2017-03-10 Thread Pascal Bourguignon

> On 10 Mar 2017, at 23:32, Quincey Morris 
>  wrote:
> 
> On Mar 10, 2017, at 08:24 , Bryan Vines  wrote:
>> 
>> Would integer division work better than the modulus operator?
> 
> It would certainly work better in the sense that division is the right 
> operator and modulus is the wrong one!
> 
> Regarding the original question, I would add that there’s a decent argument 
> to be made that keeping a long series of cases is clearer in intent and 
> methodology than a concise but somewhat obscure calculation.


Of course.  

switch(x){
case 0: return y;
case 1: y++; return y;
case 2: y++; y++; return y;
case 3: y++; y++; y++ ; return y;
…
}

this is much clearer in intent than return x+y.  So much clearer…



-- 
__Pascal J. Bourguignon__



___

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: More elegance than a long if/else

2017-03-10 Thread Quincey Morris
On Mar 10, 2017, at 08:24 , Bryan Vines  wrote:
> 
> Would integer division work better than the modulus operator?

It would certainly work better in the sense that division is the right operator 
and modulus is the wrong one!

Regarding the original question, I would add that there’s a decent argument to 
be made that keeping a long series of cases is clearer in intent and 
methodology than a concise but somewhat obscure calculation. In that case, 
though, I would just set an imageName variable inside the “if” statement, and 
retrieve the UIImage afterwards. (That keeps the contents of the “if” to simple 
statements that the compiler can easily optimize, whereas creating the image in 
each case is a method call that blocks some optimizations.)

Also, a “switch” statement would be cleaner than an “if”, since it has less 
boilerplate, and this kind of range testing can be done in Swift, though the 
mechanism isn’t obvious till you see it:

> switch (batteryLevel)
> {
> case _ where batteryLevel >= 90: imageName = "10"
> case _ where batteryLevel >= 80: imageName = “9"
> …
> default: imageName = "0”
> }
> let image = UIImage (named: imageName)

My guess is that the Swift compiler can optimize code like this very nicely.

Finally, I’d point out that anyone who cared about numerical correctness might 
say that the entire solution is “wrong” because the cutoff points are in the 
wrong place. If you’re trying to indicate battery level in 10% increments, it 
seems more correct to represent 89 and 90 values both as 90%, rather than as 
90% and 100% respectively, as the code seems to suggest. But that’s a different 
issue.

___

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: More elegance than a long if/else

2017-03-10 Thread Bryan Vines
Would integer division work better than the modulus operator?

batteryIcon.image = UIImage(named:"\(min(10, (Int(_myBatteryLevel) / 10) + 1))")

--
Bryan Vines

> On Mar 10, 2017, at 9:54 AM, Jeff Kelley  wrote:
> 
> I realized after sending that 100 won’t be correct, so you’ll need
> something like this:
> 
> batteryIcon.image = UIImage(named: "\(min(10, (_myBatteryLevel % 10) + 1))")
> 
> 
> Jeff Kelley
> 
> slauncha...@gmail.com | @SlaunchaMan  |
> jeffkelley.org


___

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

struts and springs layout

2017-03-10 Thread Jonathan Mitchell
In Xcode 8 we can use struts and springs style layout until we add an explicit 
constraint.

The idea seems to be that the strut + spring definition get turned into 
constraints.

However, querying NSView -constraints returns nothing in this case, nor does 
the debug view hierarchy.

Is it just that the constraints are private in this case?

And what relevance does NSView -translatesAutoresizingMaskIntoConstraints add 
to the mix now now that we seem to have an auto detection of when we add 
constraints? 
Is it just a legacy behaviour now?

I don’t use the strut and spring method, normally adding whatever constraints I 
need.
But sometimes I think I am missing trick by not using the simpler s + s route 
in some cases.

However a lot of my UI features wrapping text and this is just a pain to 
implement pre 10.11.

Even on 10.12 s + s and text wrapping and textfields don’t seem to get on well.

Thanks

J



___

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: More elegance than a long if/else

2017-03-10 Thread Jeff Kelley
I realized after sending that 100 won’t be correct, so you’ll need
something like this:

batteryIcon.image = UIImage(named: "\(min(10, (_myBatteryLevel % 10) + 1))")


Jeff Kelley

slauncha...@gmail.com | @SlaunchaMan  |
jeffkelley.org

On Fri, Mar 10, 2017 at 10:50 AM, Eric E. Dolecki 
wrote:

> Thank you!
>
> On Fri, Mar 10, 2017 at 10:48 AM Jeff Kelley 
> wrote:
>
>> Something like this should work:
>>
>> batteryIcon.image = UIImage(named: "\((_myBatteryLevel % 10) + 1)")
>>
>>
>> Jeff Kelley
>>
>> slauncha...@gmail.com | @SlaunchaMan  |
>> jeffkelley.org
>>
>> On Fri, Mar 10, 2017 at 10:41 AM, Eric E. Dolecki 
>> wrote:
>>
>> I have this super simple code, but I'd like to whittle it down to
>> something
>> a lot smaller - basically looking for multiples of 10 (100-0) for a value.
>> I need coffee, what's a great way to do this in Swift 3?
>>
>> if _myBatteryLevel >= 90 {
>> batteryIcon.image = UIImage(named: "10")
>> } else if _myBatteryLevel >= 80 {
>> batteryIcon.image = UIImage(named: "9")
>> } else if _myBatteryLevel >= 70 {
>> batteryIcon.image = UIImage(named: "8")
>> } else if _myBatteryLevel >= 60 {
>> batteryIcon.image = UIImage(named: "7")
>> } else if _myBatteryLevel >= 50 {
>> batteryIcon.image = UIImage(named: "6")
>> } else if _myBatteryLevel >= 40 {
>> batteryIcon.image = UIImage(named: "5")
>> } else if _myBatteryLevel >= 30 {
>> batteryIcon.image = UIImage(named: "4")
>> } else if _myBatteryLevel >= 20 {
>> batteryIcon.image = UIImage(named: "3")
>> } else if _myBatteryLevel >= 10 {
>> batteryIcon.image = UIImage(named: "2")
>> } else if _myBatteryLevel >= 0 {
>> batteryIcon.image = UIImage(named: "1")
>> }
>>
>> Thanks for thinking about my lame code.
>>
>> ___
>>
>> 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/slaunchaman%40gmail.com
>>
>> This email sent to slauncha...@gmail.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: More elegance than a long if/else

2017-03-10 Thread Eric E. Dolecki
Thank you!

On Fri, Mar 10, 2017 at 10:48 AM Jeff Kelley  wrote:

> Something like this should work:
>
> batteryIcon.image = UIImage(named: "\((_myBatteryLevel % 10) + 1)")
>
>
> Jeff Kelley
>
> slauncha...@gmail.com | @SlaunchaMan  |
> jeffkelley.org
>
> On Fri, Mar 10, 2017 at 10:41 AM, Eric E. Dolecki 
> wrote:
>
> I have this super simple code, but I'd like to whittle it down to something
> a lot smaller - basically looking for multiples of 10 (100-0) for a value.
> I need coffee, what's a great way to do this in Swift 3?
>
> if _myBatteryLevel >= 90 {
> batteryIcon.image = UIImage(named: "10")
> } else if _myBatteryLevel >= 80 {
> batteryIcon.image = UIImage(named: "9")
> } else if _myBatteryLevel >= 70 {
> batteryIcon.image = UIImage(named: "8")
> } else if _myBatteryLevel >= 60 {
> batteryIcon.image = UIImage(named: "7")
> } else if _myBatteryLevel >= 50 {
> batteryIcon.image = UIImage(named: "6")
> } else if _myBatteryLevel >= 40 {
> batteryIcon.image = UIImage(named: "5")
> } else if _myBatteryLevel >= 30 {
> batteryIcon.image = UIImage(named: "4")
> } else if _myBatteryLevel >= 20 {
> batteryIcon.image = UIImage(named: "3")
> } else if _myBatteryLevel >= 10 {
> batteryIcon.image = UIImage(named: "2")
> } else if _myBatteryLevel >= 0 {
> batteryIcon.image = UIImage(named: "1")
> }
>
> Thanks for thinking about my lame code.
>
> ___
>
> 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/slaunchaman%40gmail.com
>
> This email sent to slauncha...@gmail.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: More elegance than a long if/else

2017-03-10 Thread Jeff Kelley
Something like this should work:

batteryIcon.image = UIImage(named: "\((_myBatteryLevel % 10) + 1)")


Jeff Kelley

slauncha...@gmail.com | @SlaunchaMan  |
jeffkelley.org

On Fri, Mar 10, 2017 at 10:41 AM, Eric E. Dolecki 
wrote:

> I have this super simple code, but I'd like to whittle it down to something
> a lot smaller - basically looking for multiples of 10 (100-0) for a value.
> I need coffee, what's a great way to do this in Swift 3?
>
> if _myBatteryLevel >= 90 {
> batteryIcon.image = UIImage(named: "10")
> } else if _myBatteryLevel >= 80 {
> batteryIcon.image = UIImage(named: "9")
> } else if _myBatteryLevel >= 70 {
> batteryIcon.image = UIImage(named: "8")
> } else if _myBatteryLevel >= 60 {
> batteryIcon.image = UIImage(named: "7")
> } else if _myBatteryLevel >= 50 {
> batteryIcon.image = UIImage(named: "6")
> } else if _myBatteryLevel >= 40 {
> batteryIcon.image = UIImage(named: "5")
> } else if _myBatteryLevel >= 30 {
> batteryIcon.image = UIImage(named: "4")
> } else if _myBatteryLevel >= 20 {
> batteryIcon.image = UIImage(named: "3")
> } else if _myBatteryLevel >= 10 {
> batteryIcon.image = UIImage(named: "2")
> } else if _myBatteryLevel >= 0 {
> batteryIcon.image = UIImage(named: "1")
> }
>
> Thanks for thinking about my lame code.
> ___
>
> 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/slaunchaman%40gmail.com
>
> This email sent to slauncha...@gmail.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


More elegance than a long if/else

2017-03-10 Thread Eric E. Dolecki
I have this super simple code, but I'd like to whittle it down to something
a lot smaller - basically looking for multiples of 10 (100-0) for a value.
I need coffee, what's a great way to do this in Swift 3?

if _myBatteryLevel >= 90 {
batteryIcon.image = UIImage(named: "10")
} else if _myBatteryLevel >= 80 {
batteryIcon.image = UIImage(named: "9")
} else if _myBatteryLevel >= 70 {
batteryIcon.image = UIImage(named: "8")
} else if _myBatteryLevel >= 60 {
batteryIcon.image = UIImage(named: "7")
} else if _myBatteryLevel >= 50 {
batteryIcon.image = UIImage(named: "6")
} else if _myBatteryLevel >= 40 {
batteryIcon.image = UIImage(named: "5")
} else if _myBatteryLevel >= 30 {
batteryIcon.image = UIImage(named: "4")
} else if _myBatteryLevel >= 20 {
batteryIcon.image = UIImage(named: "3")
} else if _myBatteryLevel >= 10 {
batteryIcon.image = UIImage(named: "2")
} else if _myBatteryLevel >= 0 {
batteryIcon.image = UIImage(named: "1")
}

Thanks for thinking about my lame code.
___

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: needsDisplay and subviews

2017-03-10 Thread Jeremy Hughes
> On 9 Mar 2017, at 18:32, corbin dunn  wrote:
> 
>> 
>> On Mar 8, 2017, at 8:46 AM, Jeremy Hughes  
>> wrote:
>> 
>> If needsDisplay is set to true for an NSView, does that also cause subviews 
>> to be redrawn?
>> 
>> I’ve seen conflicting statements about this, but haven’t found anything in 
>> Apple’s documentation.
> 
> Just to be clear: if you want a view to be redrawn you should call 
> setNeedsDisplay on that view. That’s really the bottom line. There are some 
> cases where AppKit will redraw subviews when a parent view is invalidated, 
> but you should not depend on this!

Thanks for being clear!

> I also don’t recommend things like this:
> 
>> override var needsDisplay: Bool
>> {
>>  willSet
>>  {
>>  for view in subviews
>>  {
>>  view.needsDisplay = newValue
>>  }
>>  }
> 
> 
> Instead, it is better if your subviews invalidate themselves when their state 
> changes.

In this case I have a view that is broken up into subviews but should really be 
treated as a single view as far as redraws are concerned. When the model object 
for the view changes, the entire view needs to be redrawn. The controller for 
this view is not aware of each individual subview or what kind of view it is 
dealing with - it’s actually dealing with a more general view that is a 
superclass of different kinds of specific views. Also, the views aren’t aware 
of model states, so they can’t invalidate themselves.

The willSet override isn’t a general override that will cause all views to 
redraw whenever a superview is redrawn. It’s a specific override for a 
particular view that is really a cluster of subviews and which needs all of its 
subviews to be redrawn whenever it is invalidated.

Jeremy
___

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: How do I use a NSTextBlock in an attributed string?

2017-03-10 Thread Daryle Walker

> On Mar 9, 2017, at 7:05 AM, Daryle Walker  wrote:
> 
>> 
>> On Mar 8, 2017, at 4:17 PM, Daryle Walker  wrote:
>> 
>> I tried:
>> 
>>>   // Set the paragraph formatting for the body...
>>>   let bodyAdvancement = bodyFont.maximumAdvancement
>>>   let bodyIndent = max(bodyAdvancement.width, 
>>> bodyAdvancement.height) / 2.0
>>>   let bodyParagraphStyle = NSParagraphStyle.default().mutableCopy() 
>>> as! NSMutableParagraphStyle
>>>   bodyParagraphStyle.headIndent = bodyIndent
>>>   bodyParagraphStyle.lineBreakMode = .byWordWrapping
>>> 
>>>   // ...and separator
>>>   let separatorParagraphStyle = bodyParagraphStyle.mutableCopy() 
>>> as! NSMutableParagraphStyle
>>>   let separatorTextBlock = NSTextBlock()
>>>   separatorParagraphStyle.textBlocks.append(separatorTextBlock)
>>> 
>>>   // Set the body, but add a line for the initial separator
>>>   let richSeparator = NSMutableAttributedString(string: " \n", 
>>> attributes: [NSFontAttributeName: bodyFont, NSParagraphStyleAttributeName: 
>>> separatorParagraphStyle])
>>>   let richBody = NSMutableAttributedString(string: body, 
>>> attributes: [NSFontAttributeName: bodyFont, NSParagraphStyleAttributeName: 
>>> bodyParagraphStyle])
>>>   result.append(richSeparator)
>>>   result.append(richBody)
>> 
>> What I wanted: the separator line surrounded by a block.
>> What I got: the separator and the body all surrounded by a block.
>> 
>> I originally had the separator and body in a single string, and used a range 
>> to influence just the separator. This got the same result. I thought 
>> treating the separator and body in separate strings first wouldn’t infect 
>> the body with the text block, but it (illogically) does!
>> 
>> How do I end the influence of a block? Put -1 entries in the text-block 
>> array?
> 
> From tinkering around it seem like there is splatter dynamics when going 
> across ranges of text with different “textBlocks” settings, UNLESS the second 
> range uses no blocks. In just that case the second range copies the block 
> policy of the first range. This makes no sense.

I read the paragraph styles of my paragraphs, and their block settings are as 
expected. The separator has a single block, and the body has no blocks. This is 
a straight-up bug, but in Apple’s code (during rendering). That’s the problem 
with rarely-used/mentioned classes: not only does barely anyone knows how to 
use them, sometimes the creators didn’t do enough testing.

Since the bug is on Apple’s side (# 30968328), I can’t fix it and I’m stuck. 
The only idea in mind, besides giving up, is to create a 1x1 table. Now for 
more research….

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT 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