Chapter 2: 3D Models. Export asset

let asset = MDLAsset()
asset.add(mdlMesh)

/// 2
let fileExtension = "obj"
guard MDLAsset.canExportFileExtension(fileExtension) else {
    fatalError("Can't export a .\(fileExtension)")
}

/// 3
do {
    let url = playgroundSharedDataDirectory.appendingPathComponent("primitive.\(fileExtension)")
//    let url = URL(string: "/Users/thomascurley/Documents/primitive.\(fileExtension)")!
    print(url)
    try asset.export(to: url)
}
catch {
    fatalError("Error \(error.localizedDescription)")
}

Hello, I’ve copied this code from the book, however, when I try to execute this I get an exception thrown:

Fatal error: Error The operation couldn’t be completed. (Foundation._GenericObjCError error 0.): file Chapter1.playground, line 40

I have made a directory in
~/Documents/Shared Playground Data

However, when I print the URL the location isn’t the same:

file:///Users/thomascurley/Library/Developer/XCPGDevices/D9EB2BCE-ABB8-4571-8B6B-7ACCC0BE32EE/data/Containers/Data/Application/73982C04-BFA7-4898-A34F-9228D722B767/Documents/Shared%20Playground%20Data/primitive.obj

How do I fix this,

many thanks

I’m really enjoying this book!!!

1 Like

@tomus85 I’m happy to hear that you’re enjoying the book!

Did you create a macOS playground? Looking at your URL, it looks as if you may have created an iOS playground.

Try creating a blank macOS playground and copy/paste your code into that and try it

2 Likes

@caroline, ahh many thanks, that would explain it. :roll_eyes:

It was confused, but I also found the correct answer.

I enjoy reading books. Thank you so much for making a book!

I’m glad you found the correct answer and are enjoying the book :smiley:!