我试图对 NSDate 进行扩展。我想要的是一个标志,指示是否必须稍后删除 NSDate
所以我在操场上试过这个
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
extension NSDate {
private struct RemovalInformation {
static var removed: Bool = false
}
var removed: Bool {
get {
return (objc_getAssociatedObject(self, &RemovalInformation.removed) as! Bool)
}
set(newValue) {
objc_setAssociatedObject(self, &RemovalInformation.removed, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
}
let date = NSDate()
print( "The Date is: \(date)")
if(date.removed == true) {
print ("removed")
}
但是行if(date.removed == true)
产生一个错误,我不知道如何处理:“EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”