0

enter image description here

I have the following code at the beginning of the program, when it starts I need to see if there is any copied text.

But if I start the program and no text has been copied at the moment, I get the following error.

How can I solve the problem?

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
init() {        let paste = NSPasteboard.general.string(forType: .string)!...}
askedJul 9, 2021 at 10:24
Paul's user avatar
2
  • If you want to find out whether or not you can return a value, why are you force-unwrapping it?CommentedJul 9, 2021 at 11:12
  • Besides, that's probably not the right way to do it. Try using the data property.CommentedJul 9, 2021 at 11:21

2 Answers2

2

you could try this:

init() {    if let paste = NSPasteboard.general.string(forType: .string) {        // do something with paste    } else {        // do something when paste is nil    }    ....}
answeredJul 9, 2021 at 11:26
workingdog support Ukraine's user avatar
Sign up to request clarification or add additional context in comments.

Comments

1
if let _ = NSPasteboard.general.data(forType: .string) {    print("You have data")} else {    print("Oops, you don't.")}
answeredJul 9, 2021 at 11:27
El Tomato's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.