Stories That Should be Banned from Hacker News: Immortality

Some stories on HN are a complete waste of time.  Little real knowledge is ever included in the threads.  No one learns anything. We just bullshit for a few hours.  And this happens a few times a year for each topic.

My first suggestion is stories related to immortality. Most of the comments immediately turn  philosophical.

“Death gives life meaning”

“It’ll only be for rich people”

“The money should be spent on something better.”

On HN, most people don’t  want to learn anything about aging.  No one discusses the basic science that we might gain by doing the research.  The knowledge gained could have benefits in other areas of medicine like heart disease and cancer, for example.  You aren’t going to want to live forever and have Alzheimer’s.

There are 7 billion people on the planet.  It’s not going to hurt if several thousand people work on basic research to understand aging.  From reading the current news headlines, robots and AI, will  soon leave many people without jobs. Perhaps basic research in all areas of science should be expanded to create jobs?

Finally, let’s not delude ourselves into thinking that death is going to be any easy disease to cure.  Maybe we’ll get a few breakthroughs over the next couple of decades and we’ll figure out how to add a few extra healthy years to the average person.  We definitely need to move beyond long term studies and peer deeper into our bodies.  By the way, the latest study says we need to take 15,000 steps a day to live a longer life.

 

Using AutoLayout with UIView Subclasses in Playgrounds

myview1x

Playgrounds on the Mac and iPad are a great way to develop quick prototypes for new classes. Recently I had the need to create a UIView subclass that contained a few labels. Unfortunately, I couldn’t get my labels to display in the playground. When I used a UIViewController instead of a UIView, everything displayed properly. However, when I changed the parent class to UIView and made the few appropriate changes, my background color changed but my labels no longer appeared. So, instead of seeing this:

view_goal

I was seeing this:

view_problem

My initial thought was to configure the label in layoutSubviews() but the result was still the same.

It turns out that the labels will not autolayout inline within the playground.  You must use the PlaygroundSupport module and the liveView window:

import UIKit
import PlaygroundSupport

class MyView: UIView {

  ...

}

let v = MyView(frame: CGRect(x: 0, y: 0, width: 200, height: 150))
PlaygroundPage.current.liveView = v

Here’s a complete example:

import UIKit
import PlaygroundSupport

class MyView: UIView {
    
    let label = UILabel()
    
    private func setup() {
        self.backgroundColor = UIColor.green

        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "Hello world"
        label.textAlignment = .center
        self.addSubview(label)
        
        let margins = self.layoutMarginsGuide
        
        label.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0).isActive = true
        label.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0).isActive = true
        label.topAnchor.constraint(equalTo: margins.topAnchor, constant: 0).isActive = true
        label.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: 0).isActive = true
        
    }
    
//    override func layoutSubviews() {
//    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let v = MyView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
PlaygroundPage.current.liveView = v
v

Smartwatch Sales Are Tanking

Well, at least if you believe the latest click-bait headline from TechCrunch, the smartwatch revolution could be over before it even started.

The article points out that the new Watch was only available for the last 2 weeks of the quarter, and everyone was waiting for it, so it depressed sales. Fitbit sales are skyrocketing because people want a device to help improve their health:


(source)

Apple is trying to entice that market with its new release, so we should expect quite a bit of growth.

Anyway, as I pointed out last year, health and safety will drive sales.

Personally, I’ve used my Watch to help me lose some weight:

Now I’m using my Watch to help train for my first 10k:

10k_training

macOS Sierra 10.12.1 Keyboard Issue

When I upgraded to the latest version of macOS, a few of the keys on my external mechanical keyboards stopped working. Specifically, the ‘h’ and ‘i’ keys were reversed. In other words, pressing i registered an h and pressing h registered an i. In addition, the ‘a’ key didn’t register any key press.

Fortunately, the fix only takes a minute. First, open your System Preferences then choose Keyboard.   Next, click the “Modifier Keys…” button:

 

keyboard

Finally, with your external keyboard selected, click “Restore Defaults”:

reset

Swift Cookbook Updated to Swift 3.0 Syntax

I updated my online Swift Cookbook to the 3.0 syntax.

Here are the current topics:

I also added a Github repo where all the sections are in one playground. In the near future, I hope to add sections for web, regular expressions, and files. Let me know if there are any other topics that interest you.

Functional Swift: Using Array filter and forEach functions

Recently, I started learning a couple of functional languages through online MOOCs:

Swift isn’t classified as a functional language. However, that doesn’t mean we can’t use lessons learned from functional programming to improve our code. In this article, I illustrate a few ways my programming has changed after a couple of weeks of functional programming.

[].filter

There are many places in my code that looked something like this snippet:

self.retestQuestions = []

for q in questionList {
    if !q.isAnsweredCorrectly {
        self.retestQuestions.append(q)
    }
}

This translates to one line of Swift code:

let retestQuestions = questionList.filter({!$0.isAnsweredCorrectly})

In the past, I’ve used filter() to replace NSPredicate statements, which I used extensively in Objective C. In my first conversion to Swift, I directly translated my Objective C to Swift then cast my Swift array back to an NSArray to get the filter to work. Roughly my code looked like this:

let predicate = NSPredicate(format: "numWordsInGroup >= \(minWordCount) and  numPicturesInGroup >= \(minPictureCount)")

let answer = (a1 as NSArray).filtered(using: predicate)

Embracing the Array filter() function makes a lot more sense:

let answer = a1.filter({$0.numWordsInGroup >= minWordCount && $0.numPicturesInGroup >= minPictureCount})

After only a couple of weeks of functional programming, I’ve noticed many places in my code where this example, and other

[].forEach

The forEach() function isn’t a functional concept. However, without going into detail, my functional programming has led me to prefer using the Array forEach function more often. Here are several snippets from my code:

Add subviews to parent

[pronounView, answerWellView, answerConjugationView].forEach {self.mainContainerView.addSubview($0)}

Update: Someone pointed out on Reddit that this can be simplified:

// Simply pass func to apply to each element
[pronounView, answerWellView, answerConjugationView].forEach (mainContainerView.addSubview)

Active constraints

[c1, c2].forEach {NSLayoutConstraint.activate($0)}

Applying the same concept of passing the function as an argument to forEach(), activating constraints is even simpler.

[c1, c2].forEach(NSLayoutConstraint.activate) 

translatesAutoresizingMaskIntoConstraints and addSubviews

[answer1Btn, answer2Btn, answer3Btn, answer4Btn].forEach {
    $0.translatesAutoresizingMaskIntoConstraints = false
    self.cardFrontSubView.addSubview($0)
}

Disable and hide buttons

[answer1Btn,answer1Btn,answer3Btn,answer4Btn].forEach {
    $0.isEnabled = false
    $0.isHidden = false
}

Conclusion

I’ve just finished the second week, of a six week, Haskell MOOC. Hopefully, soon I’ll be using more functional concepts and my code will be “cleaner”, and more importantly contain fewer bugs.

Disabling Activity Tracing in Xcode 8

In Xcode 8, every time you run your app, there is additional logging information in the debugging window that looks similar to this:

2016-09-29 12:37:04.826400 LosVerbos[24208:17542891] subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-29 12:37:04.833232 LosVerbos[24208:17542891] subsystem: com.apple.UIKit, category: HIDEventIncoming, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0
2016-09-29 12:37:04.844944 LosVerbos[24208:17542886] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0

xcode

While this may be useful at some point, I find that it makes it harder to see my own debugging messages. These messages can be disabled by setting an environment variable in your schema: OS_ACTIVITY_MODE disable

 

schema
More on Apple’s new logging: WWDC 2016 – Session 721 – iOS, macOS – Unified Logging and Activity Tracing