你好,我有一个NSMutableArray,我尝试了这个:
var ma = NSMutableArray()
let number:Int64 = 8345834344
ma.addObject(number)// Error "Type Int64 does not conform to protocol AnyObject"
如何将Int64变量添加到NSMutableArray()中?
你好,我有一个NSMutableArray,我尝试了这个:
var ma = NSMutableArray()
let number:Int64 = 8345834344
ma.addObject(number)// Error "Type Int64 does not conform to protocol AnyObject"
ma.addObject(NSNumber(longLong:number))
你也可以使用本地的 Swift 数组:
var ma = [Int64]()
ma.append(number)
就像 Swift 中的许多内容一样,这是用 Swift 实现的。
因此,您可以执行以下操作(或者对于您想要的类型执行等效操作),这将使能够使用 Int64 替代语言预期的 AnyObject 变得神奇起来:
extension Int64 : _ObjectiveCBridgeable
{
public init(_ number: NSNumber)
{
self.init(number.longLongValue)
}
public func _bridgeToObjectiveC() -> NSNumber
{
return NSNumber(longLong: self)
}
public static func _getObjectiveCType() -> Any.Type
{
return NSNumber.self
}
public static func _isBridgedToObjectiveC() -> Bool
{
return true
}
public static func _forceBridgeFromObjectiveC(source: NSNumber, inout result: Int64?)
{
result = source.longLongValue
}
public static func _conditionallyBridgeFromObjectiveC(source: NSNumber, inout result: Int64?) -> Bool
{
result = source.longLongValue
return true
}
}