匹配Swift中对象的数据类型

时间:2020-12-14 16:26:45

How to match the data type of a object in Swift?

如何在Swift中匹配对象的数据类型?

Like:

如:

var xyz : Any
    xyz = 1;
    switch xyz
 {
    case let x where xyz as?AnyObject[]:
        println("\(x) is AnyObject Type")
    case let x where xyz as?String[]:
        println("\(x) is String Type")
    case let x where xyz as?Int[]:
        println("\(x) is Int Type")
    case let x where xyz as?Double[]:
        println("\(x) is Double Type")
    case let x where xyz as?Float[]:
        println("\(x) is Float Type")
    default:println("None")
    }

In this case switch case run default case

在这种情况下,switch case运行默认情况。

2 个解决方案

#1


11  

change var xyz : AnyObject to var xyz : Any and add it will match to this case

将var xyz: AnyObject更改为var xyz: Any,并添加它将与本例匹配

case let x as Int:

from REPL

从REPL

  1> var a : Any = 1
a: Int = <read memory from 0x7fec8ad8bed0 failed (0 of 8 bytes read)>
  2> switch a { case let x as Int: println("int"); default: println("default"); }
int

from The Swift Programming Language

从Swift编程语言。

You can use the is and as operators in a switch statement’s cases to discover the specific type of a constant or variable that is known only to be of type Any or AnyObject. The example below iterates over the items in the things array and queries the type of each item with a switch statement. Several of the switch statement’s cases bind their matched value to a constant of the specified type to enable its value to be printed:

您可以在switch语句的情况中使用is和as操作符来发现只知道类型为Any或AnyObject的常量或变量的特定类型。下面的示例遍历things数组中的项,并使用switch语句查询每个项的类型。switch语句的一些情况将它们匹配的值绑定到指定类型的常量,以便打印其值:

for thing in things {
    switch thing {
    case 0 as Int:
        println("zero as an Int")
    case 0 as Double:
        println("zero as a Double")
    case let someInt as Int:
        println("an integer value of \(someInt)")
    case let someDouble as Double where someDouble > 0:
        println("a positive double value of \(someDouble)")
    case is Double:
        println("some other double value that I don't want to print")
    case let someString as String:
        println("a string value of \"\(someString)\"")
    case let (x, y) as (Double, Double):
        println("an (x, y) point at \(x), \(y)")
    case let movie as Movie:
        println("a movie called '\(movie.name)', dir. \(movie.director)")
    default:
        println("something else")
    }
}

// zero as an Int
// zero as a Double
// an integer value of 42
// a positive double value of 3.14159
// a string value of "hello"
// an (x, y) point at 3.0, 5.0
// a movie called 'Ghostbusters', dir. Ivan Reitman

Note:

注意:

var xyz : AnyObject = 1

will give you NSNumber because Int is not object so it auto convert it to NSNumber which is object

会给你NSNumber因为Int不是object它会自动将它转换成NSNumber也就是object

#2


2  

Putting up an interesting usage of "case is" i.e., "case is Int, is String", "," behavior is like OR operator.

提出一个有趣的“案例”的用法。,“case is Int, is String”,“behavior is like OR operator”。

switch value{
case is Int, is String:
    if value is Int{
        print("Integer::\(value)")
    }else{
        print("String::\(value)")
    }
default:
    print("\(value)")
}

Similar Post with demo link

类似的文章与演示链接

#1


11  

change var xyz : AnyObject to var xyz : Any and add it will match to this case

将var xyz: AnyObject更改为var xyz: Any,并添加它将与本例匹配

case let x as Int:

from REPL

从REPL

  1> var a : Any = 1
a: Int = <read memory from 0x7fec8ad8bed0 failed (0 of 8 bytes read)>
  2> switch a { case let x as Int: println("int"); default: println("default"); }
int

from The Swift Programming Language

从Swift编程语言。

You can use the is and as operators in a switch statement’s cases to discover the specific type of a constant or variable that is known only to be of type Any or AnyObject. The example below iterates over the items in the things array and queries the type of each item with a switch statement. Several of the switch statement’s cases bind their matched value to a constant of the specified type to enable its value to be printed:

您可以在switch语句的情况中使用is和as操作符来发现只知道类型为Any或AnyObject的常量或变量的特定类型。下面的示例遍历things数组中的项,并使用switch语句查询每个项的类型。switch语句的一些情况将它们匹配的值绑定到指定类型的常量,以便打印其值:

for thing in things {
    switch thing {
    case 0 as Int:
        println("zero as an Int")
    case 0 as Double:
        println("zero as a Double")
    case let someInt as Int:
        println("an integer value of \(someInt)")
    case let someDouble as Double where someDouble > 0:
        println("a positive double value of \(someDouble)")
    case is Double:
        println("some other double value that I don't want to print")
    case let someString as String:
        println("a string value of \"\(someString)\"")
    case let (x, y) as (Double, Double):
        println("an (x, y) point at \(x), \(y)")
    case let movie as Movie:
        println("a movie called '\(movie.name)', dir. \(movie.director)")
    default:
        println("something else")
    }
}

// zero as an Int
// zero as a Double
// an integer value of 42
// a positive double value of 3.14159
// a string value of "hello"
// an (x, y) point at 3.0, 5.0
// a movie called 'Ghostbusters', dir. Ivan Reitman

Note:

注意:

var xyz : AnyObject = 1

will give you NSNumber because Int is not object so it auto convert it to NSNumber which is object

会给你NSNumber因为Int不是object它会自动将它转换成NSNumber也就是object

#2


2  

Putting up an interesting usage of "case is" i.e., "case is Int, is String", "," behavior is like OR operator.

提出一个有趣的“案例”的用法。,“case is Int, is String”,“behavior is like OR operator”。

switch value{
case is Int, is String:
    if value is Int{
        print("Integer::\(value)")
    }else{
        print("String::\(value)")
    }
default:
    print("\(value)")
}

Similar Post with demo link

类似的文章与演示链接