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 trialRandell Pur
4,934 PointsWhat is the difference between these 2?
both of them do the same thing in Xcode so I am a little confused about the difference, or if there is one.
the first one let result = removeVowels(from:"Hello World!")
the second one "Hello, World!".transform(removeVowels)
// Enter your code below
extension String {
func transform(_ argument : (String) -> String) -> String{
return argument(self)
}
}
func removeVowels(from string: String) -> String {
var newString = ""
for character in string.characters {
if ["a","e","i","o","u"].contains(character) {
continue
}
newString.append(character)
}
return newString
}
let result = removeVowels(from:"Hello World!")
1 Answer
Brandon Mahoney
iOS Development with Swift Techdegree Graduate 30,149 PointsSo your goal is to use the extension to call the function removeVowels. I think this is more about teaching you to use extensions. Both examples you give are actually using removeVowels() to do the job. Transform is calling the function on itself and could be reused with any function that takes a String and returns a String. For example:
func removeWhiteSpace(from string: String) -> String {
let newString = string.lowercased()
return newString
}
let result2 = "Hello, World!".transform(removeWhiteSpace)
Randell Pur
4,934 PointsRandell Pur
4,934 Pointson the second one, I know I have to add the let result for the code to work, I am mainly looking for the difference between the 2 code snippets and why they do the same thing but are written differently