Buscar..
Observaciones
Creando objetos NSData
Usando un archivo
Rápido
let data = NSData(contentsOfFile: filePath) //assuming filePath is a valid path
C objetivo
NSData *data = [NSData dataWithContentsOfFile:filePath]; //assuming filePath is a valid path
Usando un objeto String
Rápido
let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding) //assuming string is a String object
C objetivo
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; //assuming string is a String object
Convertir NSData a otros tipos
Encadenar
Rápido
let string = String(NSString(data: data, encoding: NSUTF8StringEncoding)) //assuming data is a valid NSData object
C objetivo
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //assuming data is a valid NSData object
[string release];
A Array
Rápido
let array = data.bytes as! NSMutableArray //assuming data is a valid NSData object
C objetivo
NSMutableArray *array = (NSMutableArray *)[data bytes]; //assuming data is a valid NSData object
A la matriz de bytes
Rápido
let bytesArray = data.bytes as! UInt8 //assuming data is a valid NSData object
C objetivo
UInt8 *bytesArray = (UInt8 *)data.bytes; //assuming data is a valid NSData object
Convertir NSData a cadena HEX
NSData
se puede representar como una cadena hexadecimal, similar a lo que produce en su método de description
.
Rápido
extension NSData {
func hexString() -> String {
return UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(bytes), count: length)
.reduce("") { $0 + String(format: "%02x", $1) }
}
}
C objetivo
@implementation NSData (HexRepresentation)
- (NSString *)hexString {
const unsigned char *bytes = (const unsigned char *)self.bytes;
NSMutableString *hex = [NSMutableString new];
for (NSInteger i = 0; i < self.length; i++) {
[hex appendFormat:@"%02x", bytes[i]];
}
return [hex copy];
}
@end
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow