I have done lots of research, yet I am stilling having the same problem. I am trying to load a website onto a web view in swift, although every time I run the project, it is giving me the following error message:
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)
The following two lines of code are what I am attempting to use to load the url onto the web view. I want to let you know that these 2 lines of code came from the following video: https://www.youtube.com/watch?v=rcVv1N1hReQ. Here are the lines of code:
var URL = NSURL(string: "https://www.google.com")
petInfo.loadRequest(NSURLRequest(URL: URL!))
In case you are wondering what the outlet for petInfo looks like, it is displayed in the following block of code:
#IBOutlet weak var petInfo: UIWebView!
The outlet is getting initialized immediately before the viewDidLoad() function. The url is being loaded inside the viewDidLoad() function. Thank you in advance for any help.
I finally figured the answer out. The problem was that my WebView was equal to nil, although my function was requesting a value, so the app crashed. Instead of using the ! to unwrap the outlet, I changed it to the following:
if let pet = petInfo{
//do normal function stuff like loading the web views.
}
I want to thank you for the answer, although it was not what I was looking for. If anybody doesn't understand the code below, just type up a comment (make sure to add +Andrew_Wilson214) and then I will explain it to you further.
I suspect that you have not connected the WebView to the IBOutlet in your ViewController. In the screenshot below you will see that I have defined #IBOutlet weak var petInfo: UIWebView but the grey circle in the gutter next to that line is not filled. It is not connected to the UI element yet. Also check your console to see if get the same fatal error 'unexpectedly found nil while unwrapping an Optional value'.
Related
Ive downloaded Apple's PageControl Sample Code to try to learn how to create lazily loaded pages as the user scrolls. However right out of the box as I scroll the picutres disappear and I get a black screen. Is that supposed to happen is there an error in the code.
Did you edit the code before running the app?
I was able to reproduce your problem by commenting out the _ = setupInitialPages call in the viewDidLayoutSubviews method call. Uncommented, the app works fine.
The thing to remember with lazy loading vars is they are not populated until called, that's the whole point of lazy loading. If there's no reference to the lazy loaded variable, it won't be instantiated and will result in what you saw in your test.
The syntax for a lazy loaded var is:
lazy var someVarName: the variable type e.g. Int, UIIMage, String... whatever = {
// Code in here to populate the variable
return variable with the declared type
}()
You need to make certain the return of the call to the lazy var matches the type. Also keep in mind, a lazy var while called a variable, once set doesn't change and is more like a constant in that regard. There are some work arounds, but they've already been addressed here... Re-initialize a lazy initialized variable in Swift
I have a simple UILabel, nothing special about it, in a UIView inside a UIScrollView. I can link it up to my ViewController.swift file just fine, and it doesn't crash on opening, but whenever I try to use the outlet, it produces a nil. I've been looking around for a solution and it seems as though you can't access an outlet inside a subview from the superview... but nothing has been exactly my situation, and none of the provided solutions work.
Here is the full error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
The strange thing, though, is that I have another UILabel, seemingly the exact same, that works fine when I try to edit it. I do not know what is going on in the slightest here. How can I fix this?
"right click" no your label in storyboard and check if you don't have another outlet connected, maybe you deleted from code and connection is still there, other than that, you can delete the label from storyboard and recreate connections.
Try providing the #IBOutlet as not weak and try.
This is not the right way, but still let us see if it's working.
I'm trying to create a simple LineChart from the Charts framework
I have successfully (at least I think I did it properly) used Carthage to link the binary to my XCode project so that I can use Charts library.
I created a UIView in my storyboard and set the Class and Module to the proper values:
I created an outlet in the proper UIViewController class:
#IBOutlet weak var lineChartView: LineChartView!
I added the delegate to the class:
class LineChartViewController: UIViewController, ChartViewDelegate {
In the viewDidLoad() function I tried to set the delegate:
lineChartView.delegate = self
*** this is the line that creates the 'fatal error'
It's as if the outlet is not set properly, or the Class/Module is not set properly.
If I check Connection Inspector, the referencing outlet is showing as connected:
I'm stumped as to what the problem could be so any help would be greatly appreciated!
Thank you
I never found a reason as to why objects in the storyboard were not able to access the 'Charts' Module.
So, if anyone else is coming up with the same problem, what I did to rectify this was to build the LineChartView programmatically. I did not have any problems instantiating from the proper class and building a line chart.
lineChartView = LineChartView(frame: CGRect(x: 0, y: 0, width: (view?.frame.width)!, height: (view?.frame.height)!))
lineChartView?.delegate = self
self.view.addSubview(lineChartView!)
I would also recommend taking a look at the documentation and examples here:
https://github.com/danielgindi/Charts
Some of the examples are still written in objective-C, but solutions are generally easy enough to work out by converting to Swift.
I would also recommend taking a look at MPAndroidChart which is where Charts is based from. There is much more documentation and examples you can find online to point you in the right direction.
https://github.com/PhilJay/MPAndroidChart
I had the same issue... My solution was to create my outlet like this:
#IBOutlet var lineChartView: LineChartView! = LineChartView()
Probably not the best practice, but it works
You are setting delegate in incorrect manner. Please go through the below link, this will help you to understand and set the right delegate.
Examples of Delegates in Swift 3
I am relatively new to iOS development with Swift (I actually have 3 years of experience with Android development with Java, trying to learn a new technology). I am creating an app that requires the usage of a library known as SearchTextField:
https://github.com/apasccon/SearchTextField
In a shellnut, it's a UITextField subclass that has a dropdown suggestions/autocomplete functionality.
Below is the ViewController that uses it...
#IBOutlet var homeAddressTextField: SearchTextField!
#IBOutlet var workAddressTextField: SearchTextField!
override func viewDidLoad() {
super.viewDidLoad()
homeAddressTextField.delegate = self
workAddressTextField.delegate = self
homeAddressTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
workAddressTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
//vvvvvvvvv EXC_BAD_ACCESS CODE 2 THROWN BELOW vvvvvvvv
homeAddressTextField.filterStrings(["foo","bar"])
}
homeAddressTextField should be instantiated, otherwise any reference to it above should throw the same exception. When breakpointing into the problematic line, homeAddressTextField is NOT nil, and correctly shows that it is an instance of SearchTextField.
I have tried many things to fix or at least find the source of the error. As you can tell, I used a strong var instead of weak var for the Outlet.
I have tried using zombies to track any attempt to access a deallocated memory block, yet the zombie Instruments came up with no zombies accessed.
If it is worth noting, the error disappears as soon as the problematic line containing filterStrings() is removed. Any help is appreciated!
It seems bug in library, could you please check here
SearchTextField Issue
It is in still open issues at their repository.
Kindly watch issues in repository, if you try to use someone readymade code.
Are you sure you've attached your IBOutlet in interface builder?
Try putting a breakpoint on the line that's crashing. That will stop it right before executing that line. Then, in the console (command+shift+y) you'll see a line that says "lldb" - put your cursor there and type po homeAddressTextField and see if it returns a value, or nil. If nil, then the IBOutlet is not set properly, which would cause bad access.
Additionally, if it is indeed nil, you'll want to make sure that the subclass and module are both set within interface builder on the SearchTextField, as well as making sure to set the outlet itself. You can also try filtering these strings in the viewDidAppear() method just to see if it is indeed an issue with the reference to the SearchTextField.
Edit: I've looked through the code a bit of the repo. You might not want to set the datasource and delegate properties, as the SearchTextField has a datasource and delegate of its own. You simply need to set the filterable strings as you are on the last line. So try removing the calls to make the view controller the datasource/delegate.
Clicking the textField and then adding class name = SearchTextField.swift worked for me.
I have an image that I download from Parse and load into an image view. When I do so I get the following error (with included print of image to show existence):
<UIImage: 0x13707c950>, {2045, 2588}
fatal error: unexpectedly found nil while unwrapping an Optional value
It is definitely linked up. I have tried cleaning, restarting Xcode, and deleting the view and image and remaking it. I have a companion project that is similar to this, and uses the same code (in a separate method I convert the PFFile into the image). I also know this method works because at another part in the project I load the image in this exact way.
if let image = objectsKeyValues[indexNumber]["image"] {
print(image)
sALargeNoteImage.imageView.image = image as? UIImage
sALargeNoteImage.imageView.contentMode = UIViewContentMode.ScaleAspectFit
}
I can't for the life of me figure out what's happening. I have done plenty of reworking in the storyboard, even at one point refactoring. This could suggest that it is related (as I have read on SO) to Xcode getting "confused". Not really sure what I should try next, short of remaking the storyboard its on (that would be a lot of work). I suppose I could remake it and copy paste the code..
Anybody have any ideas? I don't use a simulator, instead I load it onto an iPad. I have tried deleting and reinstalling the app.
The part that gets highlighted from the error is:
sALargeNoteImage.imageView.image = image as? UIImage
update:
var sALargeNoteImage = SALargeNoteImage()
class SALargeNoteImage: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
sALargeNoteImage = self
}
At one point I was testing out if it would clear the image when I set:
lSLargeNoteImage.imageView = nil
vs
lSLargeNoteImage.imageView.image = nil
I had left it like that by accident, and the function that does that runs before this.
Since you are creating sALargeNoteImage using a simple init, it will not be connected to any of the outlets in the storyboard.
You need to use something like
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sALargeNoteImage=storyboard.instantiateViewControllerWithIdentifier("largeNoteViewController") as! SALargeNoteImage
in order to instantiate the view controller and link all of the IBOutlets
I would highly recommend that you avoid the use of var someVariable=SomeType() if all you are trying to do is suppress the error from the compiler that properties must be initialised and you intend to initialise the property properly later. It is much better to use var someVariable:SomeType! as this will throw an exception if you forget to assign a value to the property rather than resulting in obscure bugs where a default value is used.