iOS Swift EXC_BAD_ACCESS code 2 SearchTextField (UITextField subclass)

916 Views Asked by At

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!

3

There are 3 best solutions below

0
karthikeyan On BEST ANSWER

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.

8
Daniel Williams On

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.

0
Ewurafua Plange On

Clicking the textField and then adding class name = SearchTextField.swift worked for me.

enter image description here