Is it possible to set an attribute to nil
using NSBatchUpdateRequest
? Passing NSNull()
to propertiesToUpdate
isn't working:
是否可以使用NSBatchUpdateRequest将属性设置为nil?将NSNull()传递给PropertiesToUpdate不起作用:
let unlockRequest = NSBatchUpdateRequest(entityName: "MyEntity")
unlockRequest.predicate = NSPredicate(format: "self in %@", myObjectIDs)
unlockRequest.propertiesToUpdate = ["lockDate": NSNull()]
var error: NSError?
myContext.executeRequest(unlockRequest, error: &error)
if let error = error {
log.error("Failed to unlock: \(error)")
}
I don't get any errors, but it doesn't clear the date.
我没有收到任何错误,但它没有清除日期。
I've also tried setting it to NSExpression(forConstantValue: NSNull())
, but that doesn't work either (forConstantValue
argument does not accept an optional value, so I can't pass it nil
.).
我还尝试将其设置为NSExpression(forConstantValue:NSNull()),但也不起作用(forConstantValue参数不接受可选值,因此无法传递nil)。
更多回答
Did you try NSExpression(constantValue: nil) ?
您尝试过NSExpression(constantValue:nil)吗?
@Willeke Sadly it doesn't accept an optional value, so I can't pass it nil.
@Willeke遗憾的是,它不接受可选值,因此我不能将其传递为零。
@Sencha did you find a solution for this?
@Sencha你找到解决这个问题的办法了吗?
@Serluca No I never did, in the end I had to set my lock date to a date in the past to effectively invalidate the lock (acceptable for me but I'm sure not for other situations).
@SerLuca不,我从来没有这样做过,最后我不得不将我的锁定日期设置为过去的某个日期,以有效地使锁定无效(我可以接受,但我确定其他情况下不能)。
优秀答案推荐
Current API allows to pass nil
as a constant value.
当前API允许将nil作为常量值传递。
unlockRequest.propertiesToUpdate = ["lockDate": NSExpression(forConstantValue: nil)]
This seems to work correctly in Objective-C:
这似乎在Objective-C中正常工作:
NSBatchUpdateRequest *batch = [[NSBatchUpdateRequest alloc] initWithEntityName:entityName];
NSExpression *value = [NSExpression expressionForConstantValue: nil];
batch.propertiesToUpdate = @{@"lockDate": value};
batch.resultType = NSUpdatedObjectsCountResultType;
NSError *error;
NSBatchUpdateResult *results = [self.privateContext executeRequest:batch error:&error];
Old question but here is the way you do it in Swift, verified myself without any crash.
老问题,但这是你在SWIFT中做到这一点的方式,验证了我自己,没有任何崩溃。
// Or Optional<Date>.none as Any
unlockRequest.propertiesToUpdate = ["lockDate": Date?.none as Any]
更多回答
I can see that the method signature now accepts Swift optionals. If this is verified to work then that would be the problem solved! NSExpression.init(forConstantValue obj: Any?)
我可以看到方法签名现在接受SWIFT选项。如果这被证实是有效的,那么问题就解决了!NSExpression.init(forConstantValue obj:有吗?)
The compiler doesn't complain, but when I execute the request it throws an exception: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid string key (null) passed to propertiesToUpdate:'
编译器没有抱怨,但当我执行请求时,它抛出了一个异常:由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因是:‘传递给PropertiesToUpdate的字符串键(NULL)无效:’
The header for NSBatchUpdateRequest.propertiesToUpdate
says: "The expressions can be any NSExpression
that evaluates to a scalar value."
NSBatchUpdateRequest.PropertiesToUpdate的标头表示:“表达式可以是计算结果为标量值的任何NSExpression。”
我是一名优秀的程序员,十分优秀!