Swiftͷas Any / AnyObjectͷ
҉తͳܕมʹ͍ͭͯ
iPhone Dev SapporoษڧձΦϯϥΠϯ Mar, 2022
1
Slide 2
Slide 2 text
ੁݪ ༞
@yusuga_
2
Slide 3
Slide 3 text
1
let int: Int = 0
print(type(of: int)) // Int
print(type(of: int as AnyObject)) // ԿʹͳΔʁ
3
Slide 4
Slide 4 text
͑1
let int: Int = 0
print(type(of: int)) // Int
print(type(of: int as AnyObject)) // __NSCFNumber
__NSCFNumber
≒ NSNumber
4
Slide 5
Slide 5 text
2
let string = ""
print(type(of: string)) // String
print(type(of: string as AnyObject)) // ԿʹͳΔʁ
5
Slide 6
Slide 6 text
͑2
let string = ""
print(type(of: string)) // String
print(type(of: string as AnyObject)) // __NSCFConstantString
__NSCFConstantString
≒ NSString
6
Slide 7
Slide 7 text
ͳ͔ͥObjectice-Cܥͷܕʹ
ม͞Ε͍ͯΔͧʂ
!
7
Slide 8
Slide 8 text
͢͜ͱՄೳ
let int: Int = 0
type(of: (int as AnyObject) as! Int) // Int
8
Slide 9
Slide 9 text
3
struct StructModel {}
let structModel = StructModel()
type(of: structModel) // StructModel
type(of: structModel as AnyObject) // ԿʹͳΔʁ
9
Slide 10
Slide 10 text
͑3
struct StructModel {}
let structModel = StructModel()
type(of: structModel) // StructModel
type(of: structModel as AnyObject) // __SwiftValue
__SwiftValue
10
Slide 11
Slide 11 text
__SwiftValue
!
11
Slide 12
Slide 12 text
ղઆ
12
Slide 13
Slide 13 text
SwiftͱObjective-Cͷܕͷ૬ޓม
13
Slide 14
Slide 14 text
SwiftͱObjective-Cͷܕͷ૬ޓม
• Objective-C and Swift interoperabilityʢ૬ޓӡ༻ੑʣʹΑͬͯɺ
SwiftͱObjective-CͷܕΛ૬ޓʹม͕ߦ͑Δ༷ʹͳ͍ͬͯ
Δɻ
13
Slide 15
Slide 15 text
SwiftͱObjective-Cͷܕͷ૬ޓม
• Objective-C and Swift interoperabilityʢ૬ޓӡ༻ੑʣʹΑͬͯɺ
SwiftͱObjective-CͷܕΛ૬ޓʹม͕ߦ͑Δ༷ʹͳ͍ͬͯ
Δɻ
• มfunc _bridgeAnythingToObjectiveC(_ x: T) ->
AnyObjectͰߦΘΕ͍ͯΔɻ
13
Slide 16
Slide 16 text
public func _bridgeAnythingToObjectiveC(_ x: T) -> AnyObject {
var done = false
var result: AnyObject!
let source: Any = x
if let dynamicSource = _extractDynamicValue(x) {
result = dynamicSource as AnyObjectɹ// ← ʂʂʂ
done = true
}
14
Slide 17
Slide 17 text
if !done, let wrapper = source as? _Unwrappable {
if let value = wrapper._unwrap() {
result = value as AnyObjectɹ// ← ʂʂʂ
} else {
result = _nullPlaceholder
}
done = true
}
15
Slide 18
Slide 18 text
if !done {
if type(of: source) as? AnyClass != nil {
result = unsafeBitCast(x, to: AnyObject.self) // ↓ʂʂʂ
} else if let object = _bridgeToObjectiveCUsingProtocolIfPossible(source) {
result = object
} else {
result = _makeSwiftValue(source) // ← ʂʂʂ
}
}
return result
}
16
__SwiftValueͱ
// __SwiftValue is an Objective-C class, but we shouldn't interface with it
// directly as such. Keep the type opaque.
#if __OBJC__
@class __SwiftValue;
#else
typedef struct __SwiftValue __SwiftValue;
#endif
swift/stdlib/public/runtime/SwiftValue.h
18
Slide 22
Slide 22 text
__SwiftValueͱ
// __SwiftValue is an Objective-C class, but we shouldn't interface with it
// directly as such. Keep the type opaque.
#if __OBJC__
@class __SwiftValue;
#else
typedef struct __SwiftValue __SwiftValue;
#endif
swift/stdlib/public/runtime/SwiftValue.h
• Objective-Cͷclass
18
SwiftͰͷas Anyͷڍಈ
let structModel = StructModel()
type(of: structModel as Any) // StructModel
24
Slide 29
Slide 29 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
NSArray * [AnyObject] [Any]
NSDictionary * [NSObject: AnyObject] [AnyHashable: Any]
NSSet * Set Set
Ҿ༻ݩ: Objective-C id as Swift Any 25
Slide 30
Slide 30 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
26
Slide 31
Slide 31 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
• Swift 2Ͱ idܕAnyObjectܕͱͯ͠ѻΘΕ͍ͯͨɻ
26
Slide 32
Slide 32 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
• Swift 2Ͱ idܕAnyObjectܕͱͯ͠ѻΘΕ͍ͯͨɻ
• ͨͩ͠ɺAnyObjectͩͱSwiftͷstructenum͕ͤͳ͍ෆ߹
͕͋ͬͨͨΊɺSwift 3͔ΒAnyܕʹมΘͬͨɻ
26
Slide 33
Slide 33 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
27
Slide 34
Slide 34 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
• ͭ·ΓObjective-CͰݩʑAnyObjectʢࢀরܕʣ͔͠ఆͯ͠
͍ͳ͔͕ͬͨɺSwiftͷܕʢstruct, enumʣΛͤΔΑ͏ʹ͢
ΔͨΊʹAnyܕʹมߋ͞Εɺ෦ͰରԠ͢Δclassܕʹม·ͨ
ϥοϓ͢Δ͜ͱʹͳͬͨɻ
27
Slide 35
Slide 35 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
28
Slide 36
Slide 36 text
SwiftͰͷidܕͷѻ͍
Objective-C Swift 2 Swift 3
id AnyObject Any
• Objective-CͷAnyܕʹΛ͢ͱ as AnyObject ͱಉ͘͡ func
_bridgeAnythingToObjectiveC(_ x: T) -> AnyObjectʹΑΔܕม
͕ߦΘΕΔ͜ͱʹͳΔɻ
28
ObjectIdentifierɺղ์͞ΕͨաڈͷΠϯελϯεͱಉ
͡IDΛฦ͢͜ͱ͕͋Δ
• ͜Εͨ·ͨ·ಉ͡ϝϞϦྖҬ͕͍·Θ͞Εͨͱਪ͞Ε
Δɻ
• ObjectIdentifierͷ༷తʹੜଘதͷΠϯελϯεͷൺֱ
ʢcomparisons during the lifetime of the instance.ʣΛఆͯ͠
͍ΔIDͳͷͰɺ͢Ͱʹղ์͞ΕͨΠϯελϯεͱಉ͡IDʹͳͬ
ͯ͠·͏Մೳੑ͋ΔͱࢥΘΕΔɻ
36