Simple HTML Parsing in Swift

I needed to parse some HTML in a SwiftUI project so I could put the content into a Text view. I couldn’t use the HTML partly because of weird behaviour in WKWebView but that’s for another post. This is was a simple case, I didn’t need any structure information from the HTML, just the text with the tags removed. Parsing HTML is always a bit evil so I tend to look for built-in services that can do it for me. Here’s what I found. Using NSAttributedString to load HTML Turns out that NSAttributedString has a way to directly load HTML [Read on…]

Sharing Swift package with WatchOS extension

I was working on a simple iOS app as a small side project and thought it would be fun to have a watch extension for it. Since the bulk of the project involved retrieving and parsing some JSON via a REST call, I wanted to share a package with Swift code between the app and the extension. It turned out to be complicated to figure out how to do that. It’s simple to do in practice so I wanted to document the process for the future. Note: This is not the simplest way to share code. It is the simplest [Read on…]

Missing argument in SwiftUI ForEach

I’ve been experimenting a bit with SwiftUI and ran into an error that it took me an embarrassingly long time to diagnose. The error message is “Missing argument for parameter #1 in call” and it appeared on a ForEach statement in a View. Here’s the cause as well as some troubleshooting steps I wish I’d followed. The SwiftUI Code The change I was trying to make was to the return type of the property stuff.all. I wanted to return a more specialized type to handle some localization conditions. For example, a change like this: As soon as I made the [Read on…]

JSON decoding in Swift Failure

Just ran into this error when trying to decode some JSON in Swift and the error message had me stumped for too long despite being trivial so I thought I’d commit it to memory. The JSON looked like this: And here’s the Swift struct I was trying to use to decode it: The problem is in the declaration of bar in Foo, it is not an array of Bar, it is just a Bar. So the declaration should be: Then this test case will work:

Flutter Null Safety Unit Tests: Late for Tests

Dart added the late keyword as part of their null safety release and it is very useful, particularly for testing. As a quick review null safety means telling the compiler that some variables will never be null. Any variable declared without a question mark following the type declaration is null safe. So String name is null safe while String? name can be null. Given that the question mark is new syntax all code written before null safety was introduced now declares all variables as cannot be null. The new late keyword tells the compiler that although a variable is not [Read on…]

GO Obsolete Dependencies

This article refers to problems that would probably only be seen for those running older versions of GO. If you know all about module management and are running up to date versions of GO with modules then you probably won’t find anything useful here. Up until very recently I was running a pre-module GO version and that bit me. If like me, you haven’t made the switch to modules and some of your dependencies appear to be missing functionality that you’re sure is there, read on. Signs of Trouble It all started with a fairly innocuous error message from the [Read on…]

Null Safety firstWhere

The change to null safety in Dart is a good thing, however a common pattern I used firstWhere for got broken. The pattern is to search a collection and return null if the search returns nothing. The short answer is that you should use firstWhereOrNull instead in this case. Example of the Issue Before the null safe version of Dart the code would look like this: The naive conversion to null safety would be: But this conversion will show a warning that the test if ( null == val ) is not required because the value can’t be null. What’s [Read on…]

GOLANG unexpected directory layout error

I got this error trying to compile some GO code today and the usual searches didn’t immediately turn up the answer to my issue. The error output made it seem that something was wrong with the path setup in my GO environment which lead me into a dead end of searches and attempted resolutions. The actual problem was very simple. I was importing with a path relative to the project directory and GO no longer approves of that. All import paths should now be relative to the src directory within the directory given by the $GOPATH environment variable. Bad, don’t [Read on…]

Java Object Creation is Very Fast

I was recently doing a code review and suggested some refactoring to extract some behaviour into separate classes. A common question came up, “This code has to be fast so shouldn’t I avoid object creation?” My instinct is always no, ignore those considerations when coding, let the compiler handle it until proven that you have to step-in. This time I avoided my laziness and cross checked that instinct. The top result on a quick search said that object creation should be avoided due to expense, but mentioned a Pentium II so I was suspicious. I’m not going to link to [Read on…]

OAuth2 – Get a Token Via REST Google Sign In

Every time I want to get an access token for a REST service for an integration test or other non-interactive use I forget how to go about it using OAuth and then have to piece it back together. My use case is wanting to give some headless process access to a user level service like Google Calendar. This post is now my permanent memory of how to do it, I hope it helps someone else. The Steps Get an account on the serviceDefine the client for OAuth access – example using a Google APIGet the code using an interactive request [Read on…]