问题
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.
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");
I get the following:
match data is: <>
match data is nil? NO
match data equals empty nsdata? NO
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.
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 "<>"!
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.
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.
回答1:
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.
Try this:
NSLog(@"match data equals empty nsdata? %@",[[match matchData] length] == 0 ? @"YES" : @"NO");
回答2:
[NSData data]
returns a new NSData
object, which will never == an existing object. To check if it is an empty NSData:
[[match matchData] isKindOfClass:[NSData class]] && match.length == 0
回答3:
CoreData does not alter the values you set on the NSManagedObject
instance. You must be setting an empty NSData
value.
To check if it is an empty data, check its length
property. It should be 0 when empty.
来源:https://stackoverflow.com/questions/15773440/how-do-i-identify-an-empty-nsdata-object-that-appears-as-empty-brackets