0

I want to write a function named remove in Swift which will accept an array or a string and remove the string from a dictionary if it is a string else it will remove all the strings from the dictionary which are present in the array. Also, this function that I wrote is disabling the styles in the editor of Xcode.

func remove(key: AnyObject){
    if key is Array{
        for (index, value) in enumerate(key){
            if -1 < self._getDataStoreKeyIndex(value){
               self._removeProperty(value)
            } else{
                self._removeItem(value)
            }
        }
    }else{
        if -1 < self._getDataStoreKeyIndex(key){
           self._removeProperty(key)
        }else{
            self._removeItem(key)
       }
    }
}

The other functions in the code are correct because if I comment this function my project is building successfully. What is wrong in my code?

I am getting a segmentation fault.

<unknown>:0: error: unable to execute command: Segmentation fault: 11
7
  • What's the question and/or the problem? Commented Aug 26, 2014 at 12:32
  • This function is not working. What is wrong with the code? Commented Aug 26, 2014 at 12:33
  • You need to give more details... what's not working? bad result? exception? Commented Aug 26, 2014 at 12:34
  • I am getting a segmentation fault. I edited the question. Commented Aug 26, 2014 at 12:38
  • 2
    Set a breakpoint at the beginning of the function then step through the code to see exactly where it crashes. Commented Aug 26, 2014 at 12:43

1 Answer 1

1

The heart of the problem is attempting to convert to "Array" which is a generic class. Instead, you need to convert to a specific instantiation of the generic, in your case "Array" or "[String]"

You also have a problem in that you can't enumerate AnyObject, combined, you need something like:

func remove(key: AnyObject) {
    if let array = key as? Array<String> {
        for (index, value) in enumerate(array) {
        }
    }
    else {
    }
}

Although I'm not sure why you're using enumerate to get at the indices which you don't use for anything, faster and more legible to just iterate the array directly:

func remove(key: AnyObject) {
    if let array = key as? Array<String> {
        for value in array {
        }
    }
    else {
    }
}

One further thought... these two operations really aren't similar and don't really share much code. Why not define to different functions that differ in signature:

func remove(key:String) {
}

func remove(array:[String]) {
    for string in array {
        remove[string]
    }
}

Which eliminates the possibility that somebody calls "remove(5)"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.