5

I'm new to swift/xcode and am trying to use the crc32 function that, as mentioned here, is provided in zlib from libz.dylib .

I've modified the code so that I am now trying:

let message1 = "some message".first?.value!.data(using: String.Encoding.utf8, allowLossyConversion: false) let crc = crc32(CLong(0), UnsafePointer<Bytef>(strcat!.bytes), UInt(message1!.length))

however I am encountering the error: use of unresolved identifier 'crc32'.

Could anyone help me to figure out what step I might be missing in trying to use the crc32 function?

Thanks!

Mikmac
  • 85
  • 2
  • 8
  • Did you do the import? – Mark Adler Mar 02 '18 at 18:20
  • Hey @MarkAdler ! Yes, here's what I did to import libz.dylib : Go to Build Phases > Link Binary with Libraries > + > Add other While in the file selection window press: "CMD"+Shift+G (i.e. Go to folder) and type /usr/lib/ From /user/lib find and add : libz.dylib – Mikmac Mar 05 '18 at 14:32
  • 2
    I meant, did you have "import zlib" in your code? – Mark Adler Mar 10 '18 at 07:24

1 Answers1

1

In Swift 5:

import zlib

let data = Data(base64Encoded: "SGF2ZSBhIG5pY2UgZGF5ISA6KQ==")!
let checksum = data.withUnsafeBytes { crc32(0, $0.bindMemory(to: Bytef.self).baseAddress, uInt(data.count)) }
print("crc32: 0x\(String(format:"%08X", checksum))")
Leszek Szary
  • 9,763
  • 4
  • 55
  • 62