美文网首页
NSData dataWithBytes: 在swift3中的写

NSData dataWithBytes: 在swift3中的写

作者: 秦枫桀 | 来源:发表于2017-09-06 12:15 被阅读0次

    Objective-C用Byte数组来构造一个NSData实例:

        Byte signkey[] = {0x1e,0xc3,0xf8,0x5c,0xb2,0xf2,0x13,0x70,0x26,0x4e,0xb3,0x71,0xc8,0xc6,0x5c,0xa3,0x7f,0xa3,0x3b,0x9d,0xef,0xef,0x2a,0x85,0xe0,0xc8,0x99,0xae,0x82,0xc0,0xf6,0xf8};
        NSData *appSign = [NSData dataWithBytes:signkey length:32];
    

    在swift3中,应该这么写:

    let signkey: [UInt8] = [0x1e,0xc3,0xf8,0x5c,0xb2,0xf2,0x13,0x70,0x26,0x4e,0xb3,0x71,0xc8,0xc6,0x5c,0xa3,0x7f,0xa3,0x3b,0x9d,0xef,0xef,0x2a,0x85,0xe0,0xc8,0x99,0xae,0x82,0xc0,0xf6,0xf8]
    let appSign: NSData = NSData.init(bytes: signkey, length: 32)
    
    注意,Byte数组在swift中应该声明为[UInt8],类型如果不显式指定,编译器并不会报错,但是得到的NSData就不对了!

    相关文章

      网友评论

          本文标题:NSData dataWithBytes: 在swift3中的写

          本文链接:https://www.haomeiwen.com/subject/mswmjxtx.html