syntax - How do I check if a var is a Tuple in Swift? -
reading type casting section of swift guide see use is
keyword type check variables.
func isstring(test: anyobject?) -> bool { return test string }
it seems when try similar check tuple containing 3 nsnumber objects, receive 'tuple not conform protocol anyobject
'. there way check if variable contains tuple?
func istuple(test: anyobject?) -> bool { return test (nsnumber, nsnumber, nsnumber) // error }
you can't use anyobject
here because tuple not instance of class type.
anyobject
can represent instance of class type.any
can represent instance of type @ all, including function types.
from the swift programming guide - type casting
instead, try using more general any
type:
func istuple(test: any?) -> bool { return test (nsnumber, nsnumber, nsnumber) } istuple("test") // false let tuple: (nsnumber, nsnumber, nsnumber) = (int(), int(), int()) istuple(tuple) // true
Comments
Post a Comment