Swift Set Delegate to Self Gives Exc_Bad_Access

Swift set delegate to self gives EXC_BAD_ACCESS

Currently you have to explicitly mark your protocols with @objc if the delegate should be an Object of a Objective-C class (Like the UITableViewController):

@objc protocol SwiftProtocol

This will enable interoperating with Objective-C

Setting delegate for CustomUiView getting EXC_BAD_ACCESS

My best guess is that you forgot to set the class of the custom view in the Interface Builder. Check the Identity Inspector for your object in the IB, it should look like this:

How it should look

If it isn't set, then that's the problem.

Avoiding EXC_BAD_ACCESS when using the delegate pattern

No, you can't (usefully) "test if an address contains a valid object". Even if you were able to grub around inside the internals of the memory allocation system and determine that your address points to a valid object, that would not necessarily mean that it was the same object that you were previously referring to: the object could have been deallocated and another object created at the same memory address.

Retaining the delegate is the usual way to solve this. Your option (b) breaks object encapsulation, and might have thread-safety issues.

Passing self as a parameter to be set as delegate

func getBanerView(in view : UIView, delegate: GADBannerViewDelegate)-> GADBannerView {

view.addSubview(bannerView)
bannerView.adUnitID = "ca-app-pub-3940256099942544/6300978111"
bannerView.translatesAutoresizingMaskIntoConstraints = false;
bannerView.delegate = delegate

return bannerView
}

Then call

getBanerView(*someView*, delegate: self)

Hope it help. :)

EXC_BAD_ACCESS in protocol?

EXC_BAD_ACCESS usually means you're trying to access a released object. In this case, delegate is probably released before you call respondsToSelector: on it.

Swift calling delegate from didSet

If the problem is not related to the use of ! then lets look for the issue somewhere else.

Maybe infinite recursion?

More specifically what happens inside gameStateUpdated? Infact if this method is changing the playing property then the didSet is triggered again and again...

You can check this easily, just add a breakpoint to didSet or the following line of code.

print("didSet called")

If didSet gets called multiple times then this is the problem.

AFNetworkReachabilityManager EXC_BAD_ACCESS in swift when referencing self

I had the exact same problem trying to use the AFNetworking reachability code from within a RestKit-based project. Apparently, as the OP notes, self is no longer available for reference when the block is executed. I had already set up my class that contains the reachability code as a singleton so instead of referencing self I referenced the singleton shared instance and it works.

Here is the singleton setup code:

class var sharedInstance: MyClass {
struct Static {
static let instance = MyClass()
}
return Static.instance
}

I can now reference the shared instance as:

MyClass.sharedInstance

So using that pattern you could update your block to be the following and it should work:

AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock{(status: AFNetworkReachabilityStatus?)          in

switch status!.hashValue{
case AFNetworkReachabilityStatus.NotReachable.hashValue:
println("Not reachable")
case AFNetworkReachabilityStatus.ReachableViaWiFi.hashValue , AFNetworkReachabilityStatus.ReachableViaWWAN.hashValue :
println("Reachable")
println(MyClass.sharedInstance.description) // Seems to cause error
default:
println("Unknown status")
}
}


Related Topics



Leave a reply



Submit