Warm tip: This article is reproduced from serverfault.com, please click

Any ways of creating QR code for CoreData UUID object?

发布于 2020-12-09 15:12:01

I'm trying to create qr code for UUID, but the main thing that stops me is that ways that I saw on creating qr codes require using String instead of UUID, so that's what I went for.

var uuid: UUID
var uuidString: {uuid} //cannot convert return expression of type 'UUID' to return type 'String'

Another option was

var uuid = UUID().uuidString

But It gave error later, when I was trying to use qr generating algorithm on practice (full qr gen code below)

import SwiftUI
import CoreImage.CIFilterBuiltins

struct QrCodeGen : View {
    let context = CIContext()
    let filter = CIFilter.qrCodeGenerator()
    var uuid = UUID().uuidString
    var body : some View {
        Image(uiImage: createQrCodeImage(uuid))
    }
    
    func createQrCodeImage(_ uuid: String) -> UIImage{
        let data = Data(uuid.utf8)
        filter.setValue(data, forKey: "inputMessage")
        if let qrCodeImage = filter.outputImage{
            if let qrCodeCGImage = context.createCGImage(qrCodeImage, from: qrCodeImage.extent){
                return UIImage(cgImage:  qrCodeCGImage)
            }
        }
            return UIImage(systemName: "xmark") ?? UIImage()
    }
    
}

Usage of generating qr:

NavigationLink(destination: QrCodeGen(uuid: Item.itemid)) //error: Value of type "NSManagedObject" has no member itemid

But later when showing text of Item.itemid everything is fine and recognised Reeboting my machine and relaunching Xcode dint solve the issue also.

Questioner
Max Sat1l
Viewed
0
Duncan C 2020-12-10 01:04:53

This code works to return the UUIDString for a UUID:

struct TestUUID {
    var uuid: UUID
    var uuidString: String {return uuid.uuidString}
}

let aTestUUID = TestUUID(uuid: UUID())

print(aTestUUID.uuidString)

It isn't clear to me what you're asking in the second part of your question. What error are you getting?

("It gave me error" isn't very helpful)