Search code examples
iosswiftstringnsdata

Converting Data object to String in swift returning nil


I'm using the following code to convert Data to String in swift. The returned String is nil. Could you please help me identify what is going wrong. Thank you.

let dataObject = try RSAUtils.encryptWithRSAPublicKey(str: textToEncrypt!, pubkeyBase64: pubKeyB64)


let dataToString = String(data: dataObject!, encoding: String.Encoding.utf8) as String!

Solution

  • this base 64 encryptedData by RSAUtils lib so use this

    if ( dataObject != nil ) {
        let encryptedDataText = dataObject!.base64EncodedString(options: NSData.Base64EncodingOptions())
        NSLog("Encrypted with pubkey: %@", encryptedDataText)
    }