Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift中,这两种类型的铸造方法有什么区别_Swift_Casting_Swift2 - Fatal编程技术网

在swift中,这两种类型的铸造方法有什么区别

在swift中,这两种类型的铸造方法有什么区别,swift,casting,swift2,Swift,Casting,Swift2,这两者的区别是什么。。(假设这是偶数类型转换) 及 我知道这似乎很基本,但我真的不知道我具体要求什么,因此无法找到合适的资源来帮助我理解这一点。在Swift中 当声明变量数据类型是可选部分时,如果您初始化它 e、 g 在上述情况下,它将存储整数值,其数据类型将为Int 若以后为变量赋值,则必须指定其数据类型。 e、 g 就你而言 let cell:MessageCell = tableView.cellForRowAtIndexPath(indexPath) 您正在指定单元格的数据类型,因此如

这两者的区别是什么。。(假设这是偶数类型转换)

我知道这似乎很基本,但我真的不知道我具体要求什么,因此无法找到合适的资源来帮助我理解这一点。

在Swift中

当声明变量数据类型是可选部分时,如果您初始化它

e、 g

在上述情况下,它将存储整数值,其数据类型将为Int

若以后为变量赋值,则必须指定其数据类型。 e、 g

就你而言

let cell:MessageCell = tableView.cellForRowAtIndexPath(indexPath)
您正在指定
单元格的数据类型
,因此如果
tableView.cellforrowatinedexpath(indexath)
将返回
MessageCell
类型,则无需再次对其进行分类

但是在
let cell=tableView.cellforrowatinexpath(indexPath)as!MessageCell

您没有为<代码>单元格< /代码>指定数据类型,因此在这种情况下,它将考虑其数据类型为<代码> UITababVIEWCELL < /COD>。因为返回类型<代码> CyfFurWaldRexXPosi:<代码>是<代码> UITababVIEW Cys< /C> > < /P> 但是您很清楚,它将返回类型为
MessageCell
的自定义单元格,因此要将其数据类型称为
MessageCell
,您必须强制将其展开。

第一个表达式是强制向下转换

它用于两种情况:

  • 返回类型为
    AnyObject
    ,必须转换为正确的类型
  • 返回类型是基类(
    UITableViewCell
    ),必须强制转换为子类(
    MessageCell

第二个表达式不是类型转换,而是类型注释

它被使用了

  • 如果无法从默认值推断类型,则声明该类型

    从文件中

    实际上,很少需要编写类型注释。如果你 在以下位置为常量或变量提供初始值: 根据定义,Swift几乎总能推断出要用于的类型 常数或变量


尝试在操场上查看下一个示例。正确理解什么是铸造以及它是如何工作的是知识的重要组成部分,如果你来自C或ObjectiveC世界,一开始可能会非常痛苦

class B {
    func fb() {
        print("function defined in B.Type")
    }
}
class C: B{
    func fc() {
        print("function defined in C.Type")
    }
}
class D {}

func foo(t: Bool)->B! {
    if t {
        return C()
    } else {
        return nil
    }
}

let c0 = foo(true)
print("casting c0 is C returns:", c0 is C, " ,BUT c0 dynamic type is", c0.dynamicType)
// casting c0 is C returns: true, BUT c0 dynamic type is ImplicitlyUnwrappedOptional<B>
c0.fb() // function defined in B.Type

// but next line doesn't compile!! (try to 'uncomment it...)
//c0.fc() // error: value of type 'B' has no member 'fc'


let c1 = foo(true) as! C
c1 is C // compiler warns you that 'is' test always true :-)
c1.fb() // function defined in B.Type
c1.fc() // function defined in C.Type

// next lines don't compile !! (try to 'uncomment it...)
//let d0: D = foo(true) // error: cannot convert value of type 'B!' to specified type 'D'
//let c2: C = foo(true)    // error: cannot convert value of type 'B!' to specified type 'C'

你的回答是正确的,没有讨论!与问题相关的一个注意事项是,问题中的第二个语句将不会编译,因为(正如您在回答中提到的)“返回类型是基类(UITableViewCell),必须强制转换为子类(MessageCell)”。一个区别是第一行编译,而第二行不编译。可能重复
var intValue = 5;
var doubleValue : Double;
doubleValue = 2.0;
let cell:MessageCell = tableView.cellForRowAtIndexPath(indexPath)
class B {
    func fb() {
        print("function defined in B.Type")
    }
}
class C: B{
    func fc() {
        print("function defined in C.Type")
    }
}
class D {}

func foo(t: Bool)->B! {
    if t {
        return C()
    } else {
        return nil
    }
}

let c0 = foo(true)
print("casting c0 is C returns:", c0 is C, " ,BUT c0 dynamic type is", c0.dynamicType)
// casting c0 is C returns: true, BUT c0 dynamic type is ImplicitlyUnwrappedOptional<B>
c0.fb() // function defined in B.Type

// but next line doesn't compile!! (try to 'uncomment it...)
//c0.fc() // error: value of type 'B' has no member 'fc'


let c1 = foo(true) as! C
c1 is C // compiler warns you that 'is' test always true :-)
c1.fb() // function defined in B.Type
c1.fc() // function defined in C.Type

// next lines don't compile !! (try to 'uncomment it...)
//let d0: D = foo(true) // error: cannot convert value of type 'B!' to specified type 'D'
//let c2: C = foo(true)    // error: cannot convert value of type 'B!' to specified type 'C'
// and finaly, next line compile with warning and (yes, unfortunately) crash :-)
let d = foo(true) as! D // crash you code