Check type of class an NSData store?

2020-05-06 11:39发布

问题:

I have a library that process a string and converts it to an unknown format and insert it into a NSData. In a part of the code that library check if the NSData is empty, if not it sends to the server:

if (!dataToSend) {
return;
}

I thought the stored value would be a string or base64, for that tested the following codes:

NSLog(@"To string -> %@",[NSString stringWithUTF8String:[dataToSend bytes]]);

NSString *decodedString = [[NSString alloc] initWithData:dataToSend encoding:NSUTF8StringEncoding];
    NSLog(@"Decode String Value: %@", decodedString);

In console I continue receiving (null), but the data is not null once I insert that code below from return;. How I can check the type of that NSData stores?

回答1:

  • Check the type of NSData
  • if([yourData isKindOfClass: [NSData class]])