Nil cannot be assigned to type AVCaptureDeviceInput
It makes total sense to me. If you'd like to set it to nil. Use optionals. that way you can set it to nil. It's a good mechanism to avoid crashes and a ton of if statements for null checks. What I recommend (even for testing) is using optionals. This is how it would look like for you:
var deviceInput : AVCaptureDeviceInput?
Now deviceInput does NOT have to hold any valid value. so you could write the following (although it's default to nil)
deviceInput = nil
Now, say you want to use it elsewhere. You could use if let like this:
if let myNonNilValue = deviceInput {
// myNonNilValue is for sure not nil
}
Xcode 12.5 compiler bug, 'nil' cannot be used in context expecting type 'someType'
Splitting code into two parts helped
var mappedArray: [SomeStruct] = ["123123", "2", "332", "124r132q", "123"].compactMap { $0.count == 3 ? .init(str: $0) : nil }
arr1.append(contentsOf: mappedArray)
AVCaptureSession Runtime crash. fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
Looking at your code, you should really try to get out of the habit of force-unwrapping optionals using !
at any opportunity, especially just to “make it compile". Generally speaking, if you ever find yourself writing if something != nil
, there’s probably a better way to write what you want. Try looking at the examples in this answer for some idioms to copy. You might also find this answer useful for a high-level explanation of why optionals are useful.
AVCaptureDeviceInput.deviceInputWithDevice
returns an AnyObject
, and you are force-casting it to a AVCaptureInput
with this line:
var deviceInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &err) as! AVCaptureInput
(you don’t need to state the type of deviceInput
by the way, Swift can deduce it from the value on the right-hand side)
When you write as!
, you are telling the compiler “don’t argue with me, force the result to be of type AVCaptureInput
, no questions asked”. If it turns out what is returned is something of a different type, your app will crash with an error.
But then on the next line, you write:
if deviceInput == nil! {
I’m actually quite astonished this compiles at all! But it turns out it does, and it’s not surprising it crashes. Force-unwrapping a value that is nil
will crash, and you are doing this in it’s purest form, force-unwrapping a nil
literal :)
The problem is, you’ve already stated that deviceInput
is a non-optional type AVCaptureInput
. Force-casting the result is probably not the right thing to do. As the docs for state,
If the device cannot be opened because it is no longer available or because it is in use, for example, this method returns
nil
, and the optional outError parameter points to anNSError
describing the problem.
The right way to handle this is to check is the result is nil
, and act appropriately. So you want to do something like:
if let deviceInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &err) as? AVCaptureInput
// use deviceInput
}
else {
println("error: \(err?.localizedDescription)")
}
Variables show up as nil - swift 4 IOS
First thing is never use forcefully unwrap until you are sure about value. In your case VNCoreModelRequest can fail and your both variable will be un assigned so it will defiantly crash your app.
One more thing make sure you use proper naming convention to your label.
Your issue is you are not setting label value from result you are getting.
To fix this
var stringy:String? {
didSet {
DispatchQueue.main.async {
self.Labele.text = self.stringy
}
}
}
OR
self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence
DispatchQueue.main.async {
self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"
}
Bewildered: Non-nil value unwrapped nil, if-let clause executes block on nil value
stillImageData
and detailItem
are not nil
, but imageView
is nil
.
In prepare(for
at the moment you execute controller.detailItem = stillImageData
the view of the destination controller is not loaded yet, therefore all outlets are not connected. When the outlet is accessed in configureView()
the crash occurs.
In this case call configureView()
in viewDidLoad
var detailItem: Data?
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
On the other hand if detailItem
is going to be updated multiple times check the outlet
var detailItem: Data? {
didSet {
if imageView != nil { configureView() }
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
Swift Dictionary is nil when adding output settings
You aren't setting imageCaptured
to anything. It's nil.
For example how to initialize it, see How To Use AVCaptureStillImageOutput To Take Picture.
Related Topics
Swift Override Static Method Compile Error
Flipping The Positions of Items in a UIstackview
How to Handle Error with Realm During Writing
Override UIgesturerecognizer Touchesbegan
Enum of String Type Vs Struct with Static Constant
How to Hide The Status Bar Programmatically in iOS 8
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift
Check If Class Has a Value for a Key
Swift Selector with Default Argument
How to Decode a Utf16 String into a Unicode Character
How to Update UIviewrepresentable with Observableobject
Variable with Getter/Setter Cannot Have Initial Value, on Overridden Stored Property
Count Number of Instances of a Class Swift
How to Put View on Top of All Other Views in Swiftui
Dependency Injection in View Controller