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 valueinit() { let paste = NSPasteboard.general.string(forType: .string)!...}- If you want to find out whether or not you can return a value, why are you force-unwrapping it?El Tomato– El Tomato2021-07-09 11:12:04 +00:00CommentedJul 9, 2021 at 11:12
- Besides, that's probably not the right way to do it. Try using the data property.El Tomato– El Tomato2021-07-09 11:21:55 +00:00CommentedJul 9, 2021 at 11:21
2 Answers2
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
38.9k5 gold badges47 silver badges52 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
if let _ = NSPasteboard.general.data(forType: .string) { print("You have data")} else { print("Oops, you don't.")}Comments
Explore related questions
See similar questions with these tags.
