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:
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
Ios9: Using Dynamic Framework with Simulator and Device
How to Rearrange Views in Swiftui Zstack by Dragging
How to Convert Nsset to [String] Array
Can't Able to Get Video Tracks from Avurlasset for Hls Videos(.M3U8 Format) for Avplayer
Using Key-Value Programming (Kvp) with Swift
Load a Pcm into a Avaudiopcmbuffer
Swift: Deallocate Gamescene After Transition to New Scene
How to Get the Yaw, Pitch, Roll of an Aranchor in Absolute Terms
What's the Best Approach to Prefill Core Data Store When Using Nspersistentcloudkitcontainer
Guarantees About the Lifetime of a Reference in a Local Variable
Swift 3D Touch iOS 10 Home Screen Quick Actions Share Item Missing
Why Is the ! in Swift Called an 'Implicitly' Rather Than 'Explicitly' Unwrapped Optional
Swift - Initialize View Controller from Storyboard by Overriding Init
Swift, Nsjsonserialization and Nserror
How to Make a Uiview Focusable Using the Focus Engine on Apple Tv