在swift中使用bridgeToObjectiveC() [英] Use of bridgeToObjectiveC() in swift
问题描述
bridgeToObjectiveC() 在swift中会做什么。这似乎是将 Swift
对象转换为 ObjectiveC
对象。这与类型转换相同吗?
What does bridgeToObjectiveC() will do in swift. It seems like converting a Swift
object into ObjectiveC
object. Is this same as type casting?
例如,我可以将swift String
转换为 NSString
喜欢
For example i can convert a swift String
into NSString
like
var swingString:String = "test string"
var correspondingNSString = swingString.bridgeToObjectiveC() // Using bridge
var correspondingNSString = swingString as NSString // type casting
我想知道两者是否相同?
I want to know both are same or not?
推荐答案
我的理解是提供了bridgeToObjectiveC()
,因此您不一定需要知道相应的objective-c类型是什么;编译器决定适当的类型。
My understanding is that bridgeToObjectiveC()
is provided so that you don't necessarily need to know what the corresponding objective-c type is; the compiler decides what the appropriate type is.
例如:如果你有一个的数组任何
对象,你可以调用 bridgeToObjectiveC( )
每个 map ,而不用担心是否有String,Int和Double值。
For example: if you have an array of Any
objects can you call bridgeToObjectiveC()
on each of them with map
without worrying about whether there are String, Int, and Double values.
使用作为
关键字,您可以明确选择要将值转换为的类型。
Using the as
keyword allows you to explicitly choose which type you want to cast the value as.
这篇关于在swift中使用bridgeToObjectiveC()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!