0

I am working on Swift 1.2 with Xcode 6 but now I've installed Xcode 7 with Swift 2.1. There are many errors in Swift 2.1 and many syntax changes, even though my code works well with Swift 1.2. The first problem is with this method:

func getSubstringUpToIndex(index: Int, fromString str: String) -> String
{
    var substring = ""

    for (i, letter) in enumerate(str) {
        substring.append(letter)

        if i == index - 1 {
            break
        }
    }

    return substring
}

Another problem occurs on this line, "extra argument 'error' in call":

let jsonResult: Dictionary = NSJSONSerialization.JSONObjectWithData(self.mutableData, options: NSJSONReadingOptions.MutableContainers, error: &error) as! Dictionary<String, AnyObject>
andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • 1
    Possible duplicate of [Swift: Extra argument 'error' in call](http://stackoverflow.com/questions/31073497/swift-extra-argument-error-in-call) and [enumerate is unavailable call the enumerate method on the sequence](http://stackoverflow.com/questions/31230761/enumerate-is-unavailable-call-the-enumerate-method-on-the-sequence/31230808#31230808) – Eric Aya Oct 13 '15 at 08:25

1 Answers1

0

In both cases it's Swift 1.2 code, not 2.0.

In the first snippet of code, the enumerate method does not exist anymore, you can update it this way:

func getSubstringUpToIndex(index: Int,
        fromString str: String) -> String
{
    var substring = ""

    for (i, letter) in str.characters.enumerate() {

        substring.append(letter)

        if i == index - 1 {
            break
        }
    }

    return substring
}

Or this way, using substringRithRange :

func getSubstringUpToIndex2(index: Int,
        fromString str: String) -> String {
    let range = str.startIndex.advancedBy(0)..<str.startIndex.advancedBy(index)
    return str.substringWithRange(range)
}

Try them out on swiftstub.

The second line you pasted does not work anymore because swift 2 now handles errors with try/do/catch, the last parameter NSErrorPointer does not exist anymore, learn more about swift2 error handling here.

uraimo
  • 19,081
  • 8
  • 48
  • 55