Reputation: 2246
Is it possible to return an array of tuples
as AnyObject?
or would I have to encapsulate the tuple
variable within a class
then return that class
?
Current structure
public typealias Changes = (id:Int!, cors:Bool!)
struct ClientReturn{
var error: NSError?
var json: JSON?
var mutableReturn: AnyObject?
var pageResults: PageResults?
}
class func Changes(api_key: String!, startDate: String?, endDate:String?,
completion: (ClientReturn) -> ()) -> (){
//content
}
Client.Changes(api_key, startDate: nil, endDate: nil){
apiReturn in
var aReturn = apiReturn;
var changesArray = [Changes]()
for(var i = 0; i < apiReturn.json!["results"].count; i++ ){
let json = apiReturn.json!["results"]
changesArray.append((id: json[i]["id"].int, cors: json[i]["cors"].bool))
}
//aReturn.mutableReturn = changesArray as! Changes
aReturn.mutableReturn = changesArray //ERROR (Cannot assign value of type '[Changes]' to type 'AnyObject?')
completionHandler(aReturn)
}
Upvotes: 1
Views: 203
Reputation: 534966
The only thing that can be cast up to AnyObject is a class (or a Swift struct that is bridged to some class).
A tuple is not a class. Thus, it cannot be cast up to an AnyObject.
The same thing applies to an array, though by a more roundabout route. An Array is not a class, but it is bridged to NSArray. But an NSArray can contain only classes, i.e. AnyObject elements. Thus, only an array of AnyObject-castables can be cast to AnyObject. A tuple is not a class... You take it from here.
Another way of looking at it is this: AnyObject is all about interchange with Objective-C. But tuples are a Swift-only feature: Objective-C knows nothing of them. Thus, an array of tuples cannot be cast to an AnyObject, because it cannot be handed to Objective-C.
Upvotes: 6