如何识别显示为空括号的空 NSData 对象? [英] How do I identify an empty NSData Object that appears as empty brackets?
问题描述
我正在处理 Game Kit 的 GKTurnBasedMatch 类中的损坏问题(请参阅 此线程) 有时会导致无效的游戏状态,并且匹配数据已损坏.
I am dealing with a corruption issue in Game Kit's GKTurnBasedMatch class (see this thread) which sometimes results in an invalid game state, with corrupted matchData.
因此,作为一种解决方法,我正在创建一种方法来识别这些无效匹配项,以便我可以适当地处理它们.损坏的 matchData 似乎是这样做的好方法.但是,到目前为止,我一直无法识别它们.当我这样做时:
So as a workaround, I'm creating a way out a way to identify these invalid matches so I can deal with them appropriately. The corrupted matchData seems like a good way to do this. However, so far I've been unable to identify them. When I do this:
// "match" is an existing GKTurnBasedMatch object
NSLog(@"match data is: %@",[match matchData]);
NSLog(@"match data is nil? %@",[match matchData] == nil ? @"YES" : @"NO");
NSLog(@"match data equals empty nsdata? %@",[match matchData] == [NSData data] ? @"YES" : @"NO");
我得到以下信息:
match data is: <>
match data is nil? NO
match data equals empty nsdata? NO
因此匹配数据显示为一对空括号<>",我希望可以将其标识为 nil,但显然不是.
So the match data is showing up as a pair of empty brackets "<>", which I would hope could be identified as nil, but apparently not.
顺便说一下,我将此 matchData 存储在核心数据实体中,在 NSData 属性下.当我保存 NSManagedObjectContext,然后 NSLog NSManagedObject 以查看其中的内容时,有问题的 NSData 属性 仍然 显示为<>"!
Incidentally, I am storing this matchData in a core data entity, under an NSData attribute. And when I save the NSManagedObjectContext, then NSLog the NSManagedObject to see what's in it, the NSData attribute in question still shows up as "<>"!
但是,如果我然后创建一个 new NSManagedObjectContext,从中检索相同的 NSManagedObject,然后 NSLog 其值,NSData 属性现在显示为 无.
However, if I then create a new NSManagedObjectContext, retreive the same NSManagedObject out of it, then NSLog its values, the NSData attribute now shows up as nil.
因此,在某些时候,核心数据似乎正在清理"属性为其 poper nil 值.我的问题是,当我将它添加到核心数据存储时,我实际上需要在该点之前将这个值标识为 nil .
So it appears that at some point, core data is "cleansing" the attribute to its poper nil value. My problem is that I actually need to identify this value as being nil before that point, at the time when I add it to the core data store.
推荐答案
您正在对上一个案例中的对象实例进行比较.这两个实例都可能为空,结果不会为真.
You are doing a comparison of object instances in your last case. Both of those instances could be empty and the result would not be true.
试试这个:
NSLog(@"match data equals empty nsdata? %@",[[match matchData] length] == 0 ? @"YES" : @"NO");
这篇关于如何识别显示为空括号的空 NSData 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!