Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Thinking

Swift Thinking

I talk about operator overloading and easy type safe JSON parsing in Swift.

Keith Smiley

June 26, 2014
Tweet

More Decks by Keith Smiley

Other Decks in Technology

Transcript

  1. KEITH SMILEY

    View full-size slide

  2. SWIFT THINKING

    View full-size slide

  3. ==(LHS: YOLO)

    View full-size slide

  4. view1.al_left == view2.al_right

    View full-size slide

  5. NSLayoutConstraint(item: view1,
    attribute: NSLayoutAttribute.Left,
    relatedBy: NSLayoutRelation.Equal,
    toItem: view2,
    attribute: NSLayoutAttribute.Right
    ...

    View full-size slide

  6. operator infix .... {}
    @infix func .... (lhs: Int, rhs: Int) -> Range {
    return Range(start: Int(arc4random_uniform(UInt32(lhs))),
    end: Int(arc4random_uniform(UInt32(rhs))))
    }

    View full-size slide

  7. for i in 1....5 {
    println(i)
    }
    // Expected: 1 2 3 4 5
    // Actual: 1 2 3

    View full-size slide

  8. NSRegularExpression *regex = [NSRegularExpression
    regularExpressionWithPattern:@"\\w+"
    options:0
    error:nil];
    NSRange range = NSMakeRange(0, string.length)
    NSUInteger numberOfMatches = [regex
    numberOfMatchesInString:string
    options:0
    range:range];
    if (numberOfMatches > 0) { ... }

    View full-size slide

  9. let regex = NSRegularExpression(pattern: "\\w+",
    options: nil,
    error: nil)
    let range = NSMakeRange(0, countElements(string))
    let numberOfMatches =
    regex.numberOfMatchesInString(string,
    options: nil,
    range: range)
    if numberOfMatches > 0 { ... }

    View full-size slide

  10. if string =~ "\\w+" { ... }

    View full-size slide

  11. -- Haskell
    matches :: String -> Bool
    matches x = x =~ "\\w+"
    # Ruby
    if string =~ /\w+/
    ...
    end
    # Shell script...
    if [[ $string =~ "\w+" ]]; then
    ...
    fi

    View full-size slide

  12. // This ~= operator ensures that indexPath.row
    // is found in the range on the right.
    if 1...list.count ~= indexPath.row { ... }
    // Check to see if the key is contained in the array.
    if ["foo", "bar"] ~= key { ... }

    View full-size slide

  13. date - 2.days() - 1.hour() + 5.minutes()

    View full-size slide

  14. isEqualToString:

    View full-size slide

  15. [thing1 isEqual:thing2];

    View full-size slide

  16. thing1 == thing2

    View full-size slide

  17. {
    "user": {
    "username": "Keith",
    "id": 1
    }
    }

    View full-size slide

  18. id JSON = [NSJSONSerialization
    JSONObjectWithData:data
    options:0
    error:&error];
    NSDictionary *userDict = JSON[@"user"];

    View full-size slide

  19. for (NSDictionary *userDict in JSON) {
    NSLog(@"%@", userDict[@"username"]);
    }

    View full-size slide

  20. let JSON: AnyObject = NSJSONSerialization
    .JSONObjectWithData(data,
    options: nil,
    error: &error)

    View full-size slide

  21. if let userDict = JSON as? NSDictionary {
    if let username = userDict.objectForKey("username") as? String {
    println(username)
    }
    }

    View full-size slide

  22. if let users = JSON as? NSDictionary[] {
    for userDict in users {
    if let username = userDict.objectForKey("username") as? String {
    println(username)
    }
    }
    }

    View full-size slide

  23. if let repos = githubJSON as? NSDictionary[] {
    for repoDict in repos {
    if let permissions = repoDict.objectForKey("permissions") as? NSDictionary {
    if let isAdmin = permissions.objectForKey("admin") as? Bool {
    if isAdmin {
    println("They are an admin!")
    }
    }
    }
    }
    }

    View full-size slide

  24. if let isAdmin = repoDict["permissions"]?["admin"]?.bool! {
    if isAdmin {
    println("They are an admin!")
    }
    }

    View full-size slide

  25. if let isAdmin = repoDict["permissions"]["admin"].bool {
    if isAdmin {
    println("They are an admin!")
    }
    }

    View full-size slide

  26. type User struct {
    Username string `json:"username"`
    }
    var user User
    err = json.Unmarshal(JSON, &user)

    View full-size slide

  27. data User = User { username :: String
    , userID :: Integer
    } deriving (Show)
    instance FromJSON User where
    parseJSON (Object j) =
    User <$>
    (j .: "username") <*>
    (j .:? "id")
    case decode JSON :: Maybe User of
    Just user -> print user
    Nothing -> error "Invalid JSON"

    View full-size slide