Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Closures in Swift 2 First Class Functions Higher Order Functions

Kieran Tross
Kieran Tross
8,266 Points

In an extension to the String type, add a function named modify. The modify function should accept as an argument a func

This is what I have so far.

extension String {

func modify(argument: String) -> String {

    return self.modify("argument here")
}

}

Does anyone know the answer?

functions.swift
// Enter your code below
extension String {

    func modify(argument: String) -> String {

        return self.modify("argument here")
    }

}

3 Answers

Alexander Smith
Alexander Smith
10,476 Points

This is the first part of challenge. Forgot how confusing closures are

// Enter your code below
extension String {
  func modify(argument: String -> String) -> String {
    return argument(self)
  }
}
Alexander Smith
Alexander Smith
10,476 Points

Heres the rest if you need it.

// Enter your code below
extension String {
  func modify(argument: String -> String) -> String {
    return argument(self)
  }
}

func firstLetter(input: String) -> String {
  return String(input.characters.first!)
}

let aString = "Swift"
let value = aString.modify(firstLetter)
Christopher Mayfield
Christopher Mayfield
19,928 Points

Are you unwrapping it because its an optional? Did you know it was optional because you looked at the documentation?

Abdoulaye Diallo
Abdoulaye Diallo
15,863 Points

try to use this.

extension String { func transform(_ argc: (String) -> String ) -> String { return argc(self) }

}