Marc Andreessen & Brendan Baker on Traction

Tonight I happened to stumble upon a great blog post from Venture Hacks titled What should I send investors? Part 3: Business Plans, NDAs, and Traction. The post cites two other blog posts, one from Marc Andreessen and another from Brendan Baker. I really wanted to read the aforementioned posts but unfortunately the links had long ago rotted away. Luckily I was able to find the posts elsewhere and just wanted to bookmark them here. Enjoy!
Part 4: The only thing that matters by Marc Andreessen
Startups: How to Communicate Traction to Investors by Brendan Baker

 

Taking a Second Look at Creating JSON Friendly Dictionaries in Swift

Earlier today I published a bunch of blog posts on how to create dictionaries in Swift that can easily be turned into JSON using the Alamofire networking library/wrapper for iOS. While I was trying to figure all of this stuff out earlier today I was primarily using the web based repl.it environment. However, after taking a second look at things it looks like using repl.it to do all of my testing may have been one of the big reasons why my solution ended up being much more complex than I would have imagined necessary.
After taking a step back and approaching the problem with a fresh perspective, it appears Alamofire has a built in class called “Parameters” which makes it easy to create JSON friendly dictionaries in Swift. Apparently however, I didn’t attempt to use this convenient class because I was doing all my hacking in the web browser and the actual Alamofire library and Parameters class was not available to me. So now I know: just use the Parameters class and creating JSON objects in Swift shouldn’t be all that complicated (maybe a little complicated, but not too bad).

 

Dynamically Generating JSON Friendly Dictionaries in Swift

As I’ve mentioned in my previous posts over the past couple days, I’m currently working on a new app that involves transmitting a fairly large amount of data from an iPhone app (WingsuitGP) to a backend web server. This is sort of backwards from what people are usually doing with JSON. Typically, JSON data is created by backend web services and then consumed by mobile apps. But in my case, my mobile app is the one creating the JSON data, and my backend web service is the consumer of the JSON data.
To transmit my JSON data over the network, I’m currently using the Alamofire http networking library/wrapper for iOS. With Alamofire, JSON data needs to be created first in the form of a Swift Dictionary which is then later converted to JSON format by Alamofire before being sent over the network via http.
One speed bump which I’ve run into so far is generating JSON friendly dictionaries in Swift dynamically. For example, lets say I have a large amount of data stored in the form of objects in my client-side Realm Database, how do I go about programmatically iterating over all of my data to dynamically create a Swift Dictionary that is also ready for conversion to JSON format by Alamofire? I’ve found some fairly straightforward examples on how to properly create JSON friendly swift dictionaries that are hard-coded, but I haven’t really been able to find any examples on how to dynamically create these dictionaries using an undetermined amount of data. So… here I present the solution I’ve hacked together today on repl.it (the solution shows the hard-coded method along with the dynamically generated method):

import Foundation
// REFERENCE: https://dzone.com/articles/all-you-need-to-know-about-json-with-swift
// Create a Hard-Coded Dictionary (JSON Friendly)
var myDictionary = [
    "location": [
        [
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        ],
        [
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        ]
    ],
    "altitude": [
        [
            "pressure": 90.00000001,
            "timestamp": 2342342345
        ],
        [
            "pressure": 91.00000029,
            "timestamp": 2342342347
        ]
    ]
] as [String:Any]
print("Hard-Coded Dictionary (JSON FRIENDLY)")
print(myDictionary)
print("")
// Create a Dynamically Generated Dictionary (ALSO JSON FRIENDLY)
var myNewDictionary = [:] as [String:Any]
var myLocationArray: [Int:Any] = [:]
myLocationArray[0] = [
    "latitude": 30.00000001,
    "longitude": 30.0000005,
    "timestamp": 2342342342
]
myLocationArray[1] = [
    "latitude": 31.00000001,
    "longitude": 31.0000005,
    "timestamp": 2542342342
]
var myAltitudeArray: [Int:Any] = [:]
myAltitudeArray[0] = [
    "pressure": 90.00000001,
    "timestamp": 2342342342
]
myAltitudeArray[1] = [
    "pressure": 91.00000001,
    "timestamp": 2542342342
]
myNewDictionary["location"] = myLocationArray
myNewDictionary["altitude"] = myAltitudeArray
print("Dynamically Generated Dictionary (ALSO JSON FRIENDLY)")
print(myNewDictionary)

 

Representing JSON Data in the Form of Swift Dictionaries

The project which I’m currently working on involves working with a lot of JSON data in an iOS mobile app which I plan on sending to a database using the Alamofire http networking library/wrapper. When working with JSON in Swift using Alamofire, you need to create your JSON objects in the form of Swift Dictionaries. This has been pretty confusing for me, so I wanted to whip up this quick blog post for my own personal reference demonstrating how to represent JSON data in the form of a Swift Dictionary so that it can later be sent over the internet to a backend server using Alamofire:
exampledata.json

{
    "location": [
        {
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        },
        {
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        }
    ],
    "altitude": [
        {
            "pressure": 90.00000001,
            "timestamp": 2342342345
        },
        {
            "pressure": 91.00000029,
            "timestamp": 2342342347
        }
    ]
}

 
exampledictionary.swift

import Foundation
// REFERENCE: https://dzone.com/articles/all-you-need-to-know-about-json-with-swift
var myDictionary = [
    "location": [
        [
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        ],
        [
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        ]
    ],
    "altitude": [
        [
            "pressure": 90.00000001,
            "timestamp": 2342342345
        ],
        [
            "pressure": 91.00000029,
            "timestamp": 2342342347
        ]
    ]
] as [String:Any]
print(myDictionary)

 
REFERENCE: All You Need to Know About JSON With Swift

 

Working with JSON in PHP: Accessing Array Items

This afternoon I spent a little time playing around with JSON & PHP using repl.it so that I can figure out how to build the next part of my WingsuitGP app. For this app I’m sending a fairly large amount of JSON data from an iPhone app back to a server running PHP, and need to parse all of the JSON data so that I can stick it in a MySQL database for further analysis. Not sure if this will be of any use to anyone other than myself, but here is what I have so far:

$rawJSON = '
{
    "location": [
        {
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        },
        {
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        }
    ],
    "altitude": [
        {
            "pressure": 90.00000001,
            "timestamp": 2342342345
        },
        {
            "pressure": 91.00000029,
            "timestamp": 2342342347
        }
    ]
}
';
$decodedJSON = json_decode($rawJSON);
$latitude[0] = $decodedJSON->{'location'}[0]->{'latitude'};
$longitude[0] = $decodedJSON->{'location'}[0]->{'longitude'};
$locationTimestamp[0] = $decodedJSON->{'location'}[0]->{'timestamp'};
$latitude[1] = $decodedJSON->{'location'}[1]->{'latitude'};
$longitude[1] = $decodedJSON->{'location'}[1]->{'longitude'};
$locationTimestamp[1] = $decodedJSON->{'location'}[1]->{'timestamp'};
$altitude[0] = $decodedJSON->{'altitude'}[0]->{'pressure'};
$altitudeTimestamp[0] = $decodedJSON->{'altitude'}[0]->{'timestamp'};
$altitude[1] = $decodedJSON->{'altitude'}[1]->{'pressure'};
$altitudeTimestamp[1] = $decodedJSON->{'altitude'}[1]->{'timestamp'};
echo "\n";
echo "Latitude Point 0: {$latitude[0]}" . "\n";
echo "Longitude Point 0: {$longitude[0]}" . "\n";
echo "Location Timestamp 0: {$locationTimestamp[0]}" . "\n";
echo "\n";
echo "Latitude Point 1: {$latitude[1]}" . "\n";
echo "Longitude Point 1: {$longitude[1]}" . "\n";
echo "Location Timestamp 1: {$locationTimestamp[1]}" . "\n";
echo "\n";
echo "Altitude Reading 0: {$altitude[0]}" . "\n";
echo "Altitude Timestamp 0: {$altitudeTimestamp[0]}" . "\n";
echo "\n";
echo "Altitude Reading 1: {$altitude[1]}" . "\n";
echo "Altitude Timestamp 1: {$altitudeTimestamp[1]}" . "\n";
echo "\n";

 

Swift Objects, JSON, & Making HTTP POST Requests with Alamofire

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Another little note to self tonight, this time regarding how to convert swift objects to JSON in order to make HTTP POST requests using the Alamofire networking library/wrapper: When making HTTP POST requests using the JSON data format with Alamofire, you actually need to turn your swift objects into dictionaries, then you can pass the dictionary to Alamofire and it will handle converting your dictionary into JSON. I was under the assumption that you could just pass Alamofire an object since JSON stands for JavaScript Object Notation and not JavaScript Dictionary Notation, but Alamofire apparently wants a dictionary. This didn’t really seem obvious to me so… that’s why I’m making note of this here on my dev-blog. And here is a link to the original StackOverflow where I found the answer to this question: [Send] Object as Parameter using Alamofire
Note, while the code example below does work, there is a small bug involving the forced unwrapping of an optional that I should not have unwrapped. However, for demonstration/reference purposes, I think it’s a pretty good example (Also, you will need to make sure to add a little XML to your Info.plist if you would like to use HTTP instead of HTTPS):

//
//  ViewController.swift
//  NetworkingWithJSON
//
//  Created by Christopher Pedersen on 3/1/19.
//  Copyright © 2019 Christopher Pedersen. All rights reserved.
//
import UIKit
import Alamofire
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func onPostJsonButtonClicked(_ sender: Any) {
        let exampleObject = ExampleClass()
        let exampleDictionary: [String:String] = [
            "id_column": "\(exampleObject.firstExampleProperty)",
            "mediumtext_column": "\(exampleObject.secondExampleProperty)"
        ]
        let parameters = exampleDictionary
        Alamofire.request("http://wingsuitgp.com/networkingwithjson.php", method: .post, parameters: parameters, encoding: JSONEncoding.default).response { response in
            print("Request: \(response.request!)")
            print("Request \(response.response!)")
            print("Error: \(response.error!)")
            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                print("Data: \(utf8Text)")
            }
        }
    }
}


//
//  ExampleClass.swift
//  NetworkingWithJSON
//
//  Created by Christopher Pedersen on 3/1/19.
//  Copyright © 2019 Christopher Pedersen. All rights reserved.
//
import Foundation
class ExampleClass {
    var firstExampleProperty: String = "ObjectConvertedToDictConvertedToJSON"
    var secondExampleProperty: String = "AlamofireSaysHello"
}

 

POST JSON Data with AJAX to a Server Running PHP

Over the past couple days I’ve been messing around with sending JSON data back and forth between a web/mobile client and a server running PHP. Here are two little scripts demonstrating how to POST JSON data to a server running PHP with AJAX:
1) sendjson.html
2) receivejson.php

 

Receiving JSON Data via HTTP POST with PHP

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

If you happen to follow my blog you know that I’ve been working on this new app called WingsuitGP (Air Races Powered by iPhone). Yesterday while working on the app I found myself a little stumped having to do something I’ve never done before… how do I receive JSON data via HTTP POST with PHP?

In the past I’ve used PHP many times to spit out JSON data back to a web or mobile app. But with WingsuitGP I’m having to do the opposite. The mobile app is the one spitting out the JSON, and the backend PHP script is on the receiving end of these data. So I just wanted to sort of bookmark this cool blog post I found from thisinterestsme.com on how to go about doing this: Receiving JSON POST data via PHP

UPDATE
I found another excellent post on StackOverflow.com: Receive JSON POST with PHP

Also, here is an example demonstrating how to send JSON data via HTTP POST using client-side JavaScript: What is the way to send a JSON object via a POST request in JavaScript (not jQuery or Nodejs)?

 

How to Dismiss the Keyboard in iOS

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

First, drag the ‘Tap Gesture Recognizer’ from Object Library onto Background. Then control-drag from ‘Tap Gesture Recognizer’ to ViewController.swift to add the dismissKeyboard() @IBAction. And last, call the resignFirstResponder() method from within the dismissKeyboard function to then dismiss the keyboard.

@IBAction func dismissKeyboard(_ sender: UITapGestureRecognizer) {
mySuperSweetTextField.resignFirstResponder()
}
 

No such module 'Alamofire'

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Ran into another annoying error message tonight while messing around with the Alamofire networking library for iOS. Actually, it was two annoying error messages:
No such module ‘Alamofire’
and…
Module ‘Alamofire’ has no member ‘request’
It looks like the documentation for Alamofire is out of sync with the latest version of Alamofire. So simply open up your podfile and change the Alamofire version back to 4.8.1 instead of version 5.0.0. Then go ahead and run $ pod update and all will be well!