Category Archives: Programming

Accessing an SMB share in iOS using Swift

As you might guess from my Books page, I read/listen to a lot of books. It became apparent that I needed a way to track all of these books, basically just for my own sanity. I’m not sure if you are like me but when you read this many books, I really don’t remember all of them (most of them?). There are a lot of series in there that span years, so I often forget which ones I’ve read.

To alleviate this problem, I wrote a book database app for iOS/iPhone, using Swift and SwiftUI. It’s super simple, only has a couple of screens but as important as tracking my books, it allows me to search them. This is invaluable. Here are a couple of screenshots:

Since friends and family will ask what I’m reading, I decided to add the ability to post my list of books read to this website. It’s just a json file that lives on my server and is read by a script. The catch was when I first wrote the app, my website was hosted and the only way to push files to the server was via FTP. iOS supported FTP upload a long time ago and while depreciated, it still worked. And once you have something working, you tend to forget about it. When I moved to self hosting my website, FTP was quick and dirty to get going and I only had to change the login data inside the app.

But all the depreciated warnings bugged me, so I set out to move to SMB, as my server has a bunch of shares on it for various other things. iOS added native support for SMB way back in iOS 13 but it was pretty low level and I could never find any good example code on the web to work from. Fast forward to this week and I finally have tackled the problem thanks to a super easy SMB client library I found on GitHub.

The readme has very clear instructions, although I think he changed the fileName property to name in all instances and just forgot to update it. It really was as simple as adding the library via the Swift Package Manager (SPM), importing the library into my helper swift file that deals with saving the data, and then following the code examples under “Usage”. The author doesn’t use any do {...} catch {...} blocks to deal with errors in the example code but they are easy enough to implement. If you incorporate this functionality using a function, you’ll need to mark it as asynchronous and use a Task to call it, again pretty straight-forward. I commented out the old FTP code, tested, and everything worked. Sweet!

I’m not sure it’s worth providing more code here, given the extent of code in the readme on the GitHub page but if you have any questions, feel free to contact me or leave a comment.

Updated:
Uploading a file using this client will not overwrite an existing file but will cause a name collision error. You can however, delete the file prior to uploading, which worked find.

100 Days of Swift

Paul Hudson (@twostraws) is hosting a 100 Days of Swift in which he provides short videos, quizzes and assignments to walk you through an introduction to Swift. He actually started some time ago, I think on Feb 1, 2019, but I’m just getting started now and have completed Day 4!

In Day 1, we covered the following topics:

  1. Variables – test
  2. Strings and integers – test
  3. Multi-line strings – test
  4. Doubles and booleans – test
  5. String interpolation – test
  6. Constants – test
  7. Type annotations – test
  8. Simple types: Summary – test

In Day 2, we covered the following topics:

  1. Arrays – test
  2. Sets – test
  3. Tuples – test
  4. Arrays vs sets vs tuples – test
  5. Dictionaries – test
  6. Dictionary default values – test
  7. Creating empty collections – test
  8. Enumerations – test
  9. Enum associated values – test
  10. Enum raw values – test
  11. Complex types: Summary – test

In Day 3, we covered:

  1. Arithmetic Operators – test
  2. Operator overloading – test
  3. Compound assignment operators – test
  4. Comparison operators – test
  5. Conditions – test
  6. Combining conditions – test
  7. The ternary operator – test
  8. Switch statements – test
  9. Range operators – test
  10. Operators and conditions summary – test

In Day 4, we covered:

  1. For loops – test
  2. While loops – test
  3. Repeat loops – test
  4. Exiting loops – test
  5. Exiting multiple loops – test
  6. Skipping items – test
  7. Infinite loops – test
  8. Looping summary – test

Exec-php WordPress Plugin and PHP 7 Fix

I recently upgraded the version of PHP used on our hosting service to 7.1, as the Elementor plugin requires at least version 7 to run now. This had the side effect of breaking the Exec-php plugin (version 4.9) I use to allow PHP code to run in posts and pages. It manifested itself with the following error message:

Parse error: syntax error, unexpected ‘new’ (T_NEW) in

It appears you can no longer assign a class in PHP version 7 using the ‘&’ symbol and Exec-php makes extensive use of this. Here’s an example:

$GLOBALS['g_execphp_manager'] =& new ExecPhp_Manager();

I went through all of the instances and removed the & symbol using BBEdit and it now runs correctly.

Thanks to the CodeCave blog for this insight!

Using Python to Calculate Transmission Probabilities for Multilayer Structures

I’m currently taking the second Quantum Mechanics for Scientists and Engineers course online from Stanford. They are both great courses taught by David Miller. The second course is looking more at applications and the third week’s topic was optical absorption by semiconductors.

Once we worked through all the physics and algebra, it was presented that these types of calculations are very easy to code and Professor Miller provided a program to calculate the transfer matrix and transmission probabilities for multilayer structures. The code he provided was for Matlab (and also for Mathcad). I have Octave, basically an open source version of Matlab, running on my Mac, so I was able to run the code provided. Mathcad is only available on Windows and installing Octave on a Mac can be a bit cumbersome, so I wanted to create a solution that could easily be run on any platform and work on my Python coding skills, which are pretty dismal.

It took a couple of days to port the Matlab code to Python but I got it running, the challenge being complex numbers in Numpy. Then a little more time to figure out Jupyter Notebooks, as my original code ran straight Python on my desktop. Here’s a link to my notebook running on Google’s Colaboratory:

https://colab.research.google.com/drive/1aMV8ZYSfhraMMVnF_KiVhHFJXnMzWZnN

This link is view only, but you can easily download the code and run it in your own notebook, which will allow you to change the parameters, like the potentials, effective masses, etc. If you do run this one, you should get a graph that looks like this:

I did not attempt to make the code as efficient as possible, the goal was simply to get it to work. Comments and feedback are welcome.

pydotplus on macOS

I’ve been playing around with SciKit-Learn’s decision tree regression function. In one of the examples I was working on, they provided a demo script to print out a jpg of the decision tree. Cool! Unfortunately, my system didn’t have pydotplus installed. I’m using Anaconda, so issuing the following command took care of that issue:

conda install -c conda-forge pydotplus

However, when attempting to run again, this produced and error stating that the GraphViz executable could not be found. Ugh. Luckily, a quick trip to Homebrew took care of that issue:

brew install graphviz

Voila!

Sending email with PHPMailer

It appears most of the examples out there relate to version 5.2. The current version is 6 and I had to make the following change in order to get it to work. Everything else from the examples worked.

Change:

$mail = new PHPMailer();

To:

$mail = new PHPMailer\PHPMailer\PHPMailer();

I’ll post (or edit this post) a full example shortly.

Installing GCC-7.3.0 on macOS High Sierra

I talked about getting GCC-5.3.0 running in a previous post. As part of some benchmark testing I’m doing for a new computer build, which I hope to write about in a future post, I ventured back into the world of parallel programming. I used the excellent instructions from Solarian Programmer found here to get GCC-7.3.0 running on macOS High Sierra 10.13.3.

Strictly speaking, the link above points to instructions for compiling GCC-7.1.0. While it’s not a big deal to modify these instructions for 7.3.0, here’s a text document with just the command line instructions to get 7.3.0 up.

Note: I did have to make one small deviation from the instructions provided in the link (as noted in the text document attached). I tried to “make” GCC7.3.0 with “make -j 4” but it failed, stating it couldn’t fine “<ctime>”. I retried the make using “make -j 1”, which took a LONG time but worked. I would try 4 first and only if it doesn’t work, try 1.

My previous post also talked about getting OpenMP/Clang running in Xcode and the link provided there works. I did try the steps but couldn’t get it to work. Maybe I’ll try again later but for now, I’m using the command line to compile the OpenMP code.

Allowing different orientations by device (iPad vs iPhone)

I’ve been watching the Developing iOS 11 Apps with Swift podcast (yes, iTunes U content is now available as podcasts, which is cool because you can view them on your Mac) and playing around the Concentration app. This app really wants to be Portrait on an iPhone but Portrait or Landscape on an iPad, given the whole master/detail thing.

I searched for ways to do this and while apparently you can do this in code, this method is super simple. You can add another Custom iOS Target Property to your Info.plist under the Info tab of your project’s target, specifically for iPad. The main one is:

Supported interface orientations

The iPad specific one is:

Supported interface orientations (iPad)

Here’s a screen shot to show it in action.

Hope this helps!

Installing Octave 4.2.1 on macOS Sierra

I’ve been using version 4.0.3 of Octave for some time, installed via the binary available from SourceForge. It works reasonably well, with the exception of having to return to Terminal to enter commands for paged output. I wanted to move up to the current version and managed to get version 4.2.1 installed using HomeBrew. In case you are interested, here are the steps I followed:

  1. Open Terminal
  2. Enter the command: /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
  3. Press RETURN
  4. Install homebrew/science using the following command: brew tap homebrew/science
  5. Octave requires XQuartz, install using: brew cask install XQuartz
  6. Install Octave using the following command: brew install octave
  7. Wait for a bit, then installation finishes

At this point, I “cd’ed” to the bin directory and attempted to run Octave with the following command:

cd /usr/local/Cellar/octave/4.2.1_2/bin/
./octave

Sadly, this produced the following error:

dyld: Library not loaded: /usr/local/opt/hdf5/lib/libhdf5.100.dylib
  Referenced from: /usr/local/Cellar/octave/4.2.1_2/libexec/octave/4.2.1/exec/x86_64-apple-darwin16.5.0/octave-gui
  Reason: image not found
octave exited with signal 6

After some poking around Brew’s install of Octave, I noticed the library in question (located at: /usr/local/opt/hdf5/lib/) had been updated to version 101 (libhdf5.101.dylib), instead of the expected 100 version. I also noticed there was a symlink from libhdf5.dylib to libhdf5.101.dylib. So I just duplicated that symlink, renamed it libhdf5.100.dylib and reran the ./octave command. Everything works!

 

P.S. I made a small text file that contains the following:

#!/bin/bash
# Runs Octave 4.2.1

/usr/local/bin/octave

and saved it to the Desktop with the filename Octave.command. Then in Terminal, I made it executable by running the following: chmod +x Octave.command

You can now double-click this file to run Octave!

Setting a UIButton’s Image in Swift

yourButton.ImageView?.image is a READ-ONLY property!!!
yourButton.ImageView?.image is a READ-ONLY property!!!
yourButton.ImageView?.image is a READ-ONLY property!!!
yourButton.ImageView?.image is a READ-ONLY property!!!
yourButton.ImageView?.image is a READ-ONLY property!!!

yourButton.setImage(UIImage(named: “name_of_image”), forState: UIControlState.Normal)