This is my first post, and I am very happy to join in this community. I am learning Swift with Ray Wenderlich's video tutorial. The challenge I got for lesson 3 is remove a element in an array without hard-coding the index. I understand the correct answer that Ray provided, but just don't understand why my answer can not work. Please see following for Ray's answer as well as my answer. If anyone can explain it for me, that would be great!! thanks:]
Correct Answer:
// Make an array with "C", "C++", and "Objective-C"
var programmingLanguages = ["C", "C++", "Objective-C"]
// Append "Swift" to the array
programmingLanguages += "Swift"
// Insert "Javascript" at Index 2
programmingLanguages.insert("Javscript", atIndex: 2)
// Remove "Objective-C" (without hard-coding the index)
let optIndex = find(programmingLanguages, "Objective-C")
if let defIndex = optIndex {
programmingLanguages.removeAtIndex(defIndex)
}
programmingLanguages
My answer1:
// Make an array with "C", "C++", and "Objective-C"
var programmingLanguages = ["C", "C++", "Objective-C"]
// Append "Swift" to the array
programmingLanguages += "Swift"
// Insert "Javascript" at Index 2
programmingLanguages.insert("Javscript", atIndex: 2)
// Remove "Objective-C" (without hard-coding the index)
programmingLanguages.removeAtIndex(find(programmingLanguages,"Objective-C")
programmingLanguages
My answer2:
// Make an array with "C", "C++", and "Objective-C"
var programmingLanguages = ["C", "C++", "Objective-C"]
// Append "Swift" to the array
programmingLanguages += "Swift"
// Insert "Javascript" at Index 2
programmingLanguages.insert("Javscript", atIndex: 2)
// Remove "Objective-C" (without hard-coding the index)
let optIndex = find(programmingLanguages, "Objective-C")
programmingLanguages.removeAtIndex(optIndex)
programmingLanguages