How to cast a swift array of tuples to NSMutableArray?

How to cast a swift array of tuples to NSMutableArray?

I have swift array of tuples [(String, String)] and would like to cast this array to NSMutableArray. I have tried this and it is not working: 
let myNSMUtableArray = swiftArrayOfTuples as! AnyObject as! NSMutableArray

Solutions/Answers:

Solution 1:

Since swift types like Tuple or Struct have no equivalent in Objective-C they can not be cast to or referenced as AnyObject which NSArray and NSMutableArray constrain their element types to.

The next best thing if you must return an NSMutableArray from a swift Array of tuples might be returning an Array of 2 element Arrays:

let itemsTuple = [("Pheonix Down", "Potion"), ("Elixer", "Turbo Ether")]
let itemsArray = itemsTuple.map { [$0.0, $0.1] }
let mutableItems = NSMutableArray(array: itemsArray)

Solution 2:

There are two problems with what you are trying to do:

  • Swift array can be cast to NSArray, but it cannot be cast to NSMutableArray without constructing a copy
  • Swift tuples have no Cocoa counterpart, so you cannot cast them or Swift collections containing them to Cocoa types.

Here is how you construct NSMutableArray from a Swift array of String objects:

var arr = ["a"]
arr.append("b")
let mutable = (arr as AnyObject as! NSArray).mutableCopy()
mutable.addObject("c")
print(mutable)

References

Using the guard condition in the body of statement

Using the guard condition in the body of statement

I'm making a url request in Swift and want to print a meaningful error including the response code should their be one. I'm attempting to do this in as few lines of code as possible. The error I'm getting in XCode is the following : Variable declared in 'guard' condition not usable in its body. 
How can I perform the following without bloating the code to more lines, is it possible?
 //check to see if we got a valid response code
    guard let resCode = (response as? NSHTTPURLResponse)?.statusCode where resCode == 200 else {
        return NSError(domain: "Error with request", code: 1, userInfo: [NSLocalizedDescriptionKey: "Recieved the following status code: \(resCode)"])
    }

The error occurs with my attempting to use the variable resCode within the body of the guard statement. 

Solutions/Answers:

Solution 1:

As the error states, you can’t use a variable that you bound in the guard statement inside of the guard statement’s body. The variable only gets bound in the case where the guard body is not entered. You also aren’t differentiating between the cases where your response is nil and where your status code isn’t 200.

You should break the statements into two different checks:

guard let httpResponse = response as? NSHTTPURLResponse else {
    return NSError(domain: "Error with request", code: 1, userInfo: [NSLocalizedDescriptionKey: "Invalid response: \(response)"])
}

guard httpResponse.statusCode == 200 else {
    return NSError(domain: "Error with request", code: 1, userInfo: [NSLocalizedDescriptionKey: "Recieved the following status code: \(httpResponse.statusCode)"])
}

Don’t try to minimize the number of lines of code at the expense of readability or correctness.

References