Trim Whitespace From UITextField in Swift

Let’s say you have a text field and you want to disable the Save button until the user enters something. But you don’t want to allow whitespaces. Here’s one way to handle that.

1. Hook up an outlet to your bar button item so you can set its isEnabled state later.

@IBOutlet weak var saveButton: UIBarButtonItem!

2. Hook up an outlet to your text field.

@IBOutlet weak var nameTextField: UITextField!

3. Set a target action on your text field. Put this in your viewDidLoad method.

nameTextField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)

4. Create the selector method.

@objc private func textFieldDidChange(_ textField: UITextField) {
    guard let text = textField.text else { return }

    if !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
        saveButton.isEnabled = true
    }
}

Symfony Cheatsheat

Here’s a bunch of random notes on Symfony. These are kind of a summary of the SymfonyCasts tutorial track here.

Creating a New Controller

  1. Right-click on the directory where you want your controller to to (probably src/Controller)
  2. Choose “New PHP Class”
    Because we installed the Symfony plugin, PhpStorm can pre-fill the namespace
Continue reading “Symfony Cheatsheat”

Find your SQLite Database for Xcode 10

To get the SQLite database for your app on all simulators on which it’s installed:

find ~/Library/Developer/CoreSimulator/Devices -name MyAppName.sqlite

(Xcode 10)

Open Finder and do Shift + Cmd + G  to bring up the “Go to folder” dialog. Paste in the file path and you should see it. It will be garbled if you try to open it with a plain text editor. DB Browser for SQLite seems to work for me.

Rounded Corner Button in Swift

It’s very simple to create buttons with rounded corners. Create a button like you normally would in Interface Builder, create an Outlet to it, and then in viewDidLoad, set the attributes like this:

button.layer.borderWidth = 3.0
button.layer.borderColor = UIColor.white.cgColor // Set this to the background color of your button
button.layer.cornerRadius = 8.0

Laravel New Project Steps

Here are my steps for creating a new laravel project. I know the docs are excellent but these are the steps specific to my setup, which is probably pretty common. For reference, I use the laravel installer to create new projects and I use Homestead. I already have Homestead set up so it’s just a matter of changing a few things and the inevitable updates, since this is something I don’t do frequently (thus the need for notes).

1. update laravel installer
composer global update "laravel/installer"

2. create the laravel project
laravel new projectname
cd projectname

3. Update Homestead
cd into your Homestead directory and edit the Homestead.yaml file
Add a new entry to the sites section

4. run
vagrant reload --provision

5. Update your hosts file to add projectname
open -a "Sublime Text" /etc/hosts
use .test extension (I’ve had issues with Chrome and .dev or .app extensions not working)

6. Launch the vagrant box
vagrant up

7. SSH into Homestead and cd into the projectname directory
run
composer install

9. Create a .env file in your project root
Copy everything from .env.example into .env

10. Generate the Application Key from Homestead (all php artisan commands have to be run from Homestead on my setup)
php artisan key:generate

11. Navigate to your projectname.test in the browser – you should get the Laravel welcome page

Custom key bindings in Xcode 8

I hate that Xcode doesn’t include a duplicate line shortcut. I have to keep adding it in every time a new version of Xcode comes out and every time, I have to Google it, so I’m pasting the SO post here that worked most recently. There must be a better way, but until I figure that out, here’s what has worked for me.

Open

/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist

And add:

<key>Custom Commands</key>
<dict>
    <key>Cut Current Line</key>
    <string>selectLine:, cut:</string>
    <key>Copy Current Line</key>
    <string>selectLine:, copy:</string>
    <key>Duplicate Current Line</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>
</dict>

Then restart Xcode and go to Preferences -> Key Bindings and assign your shortcuts.

This was taken from: https://stackoverflow.com/a/41579618