Questions tagged [method-swizzling]

Method swizzling is the process of changing the implementation of an existing method.

Method swizzling is the process of changing the implementation of an existing method, swapping its implementation with another's that have the same signature.

The technique is possible in languages where the method implementation is allowed to be changed at runtime. One example is Objective-C, in which is used by Apple’s Foundation Framework to implement Key-Value Observing.

108 questions
246
votes
8 answers

What are the Dangers of Method Swizzling in Objective-C?

I have heard people state that method swizzling is a dangerous practice. Even the name swizzling suggests that it is a bit of a cheat. Method Swizzling is modifying the mapping so that calling selector A will actually invoke implementation B. One…
Robert
  • 37,670
  • 37
  • 171
  • 213
13
votes
3 answers

Debugging Core Data __NSCFSet addObject nil exception

I'm getting exceptions thrown during my unit tests, on a Core Data thread with this message: CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of…
Dov
  • 15,530
  • 13
  • 76
  • 177
13
votes
3 answers

How to mock NSDate in Swift?

I have to test some date calculation but to do so I need to mock NSDate() in Swift. Whole app is written in Swift and I'd like to write test in it as well. I've tried method swizzling but it doesn't work (or I'm doing something wrong which is more…
Marcin Zbijowski
  • 820
  • 1
  • 8
  • 23
12
votes
1 answer

Is there any way to list all swizzled methods in an iOS app?

I'm essentially looking for a way to detect when/what third party libraries swizzle. I recently ran into a situation where an ad library used an oddball fork of AFNetworking. AFNetworking swizzles NSURLSessionTask, and the two swizzles didn't play…
BTK
  • 123
  • 5
12
votes
5 answers

Method swizzling in Swift

There is a little problem with UINavigationBar which I'm trying to overcome. When you hide the status bar using prefersStatusBarHidden() method in a view controller (I don't want to disable the status bar for the entire app), the navigation bar…
Isuru
  • 30,617
  • 60
  • 187
  • 303
11
votes
2 answers

Calling original function from swizzled function

I am messing around with method swizzling and would like to call the original function after performing a method_exchangeImplementations. I have two projects I have setup for this. The first project is the main project for the application. This…
Corey Gagnon
  • 113
  • 1
  • 4
10
votes
1 answer

Are method swizzling and isa swizzling the same thing?

Are method swizzling and isa swizzling the same thing? If not, then what is isa swizzling?
Mayank
  • 643
  • 8
  • 19
10
votes
2 answers

Method swizzling for property in swift

While it is possible to replace setMyProperty: method in obj-c, I'm wondering how to do it in swift? For example I want to replace UIScrollView::setContentOffset:: let originalSelector: Selector = #selector(UIScrollView.setContentOffset) let…
brigadir
  • 6,874
  • 6
  • 46
  • 81
8
votes
1 answer

Method Swizzling does not work

I would like to make use of method swizzling, but am unable to get even simple examples to work for me. It is possible that I am misunderstanding what the concept is, but as far as I know it allows for method implementations to be swapped. Given two…
Kevin
  • 1,626
  • 16
  • 30
8
votes
1 answer

Objective-C method swizzling performance

What performance penalties arise while using method swizzling in Objective-C? Which, if any, compiler optimisations are defeated with message swizzling?
Ilea Cristian
  • 5,741
  • 1
  • 23
  • 37
7
votes
1 answer

Method swizzling in swift 4

Swizzling in Swift 4 no longer works. Method 'initialize()' defines Objective-C class method 'initialize', which is not permitted by Swift This is something I have found a solution to so wanted to leave the questions and answer for others.
Christopher Rex
  • 392
  • 3
  • 10
7
votes
3 answers

How to call original implementation when overwriting a method with a category?

I try to figure out how things really work. So I thought when I would overwrite certain methods using categories, I would get interesting NSLogs. @implementation UIView(Learning) - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { …
Proud Member
  • 40,078
  • 47
  • 146
  • 231
7
votes
2 answers

Proper way of method swizzling in objective-C

Currently experimenting with method swizzling in Objective-C and I have a question. I am trying to understand the proper way to method swizzle and after researching online I stumbled upon this NSHipster…
AyBayBay
  • 1,726
  • 4
  • 18
  • 37
7
votes
2 answers

Using dispatch_once in method swizzling

In NSHipter's article on method swizzling, it says "Swizzling should always be done in a dispatch_once." Why is this necessary since +load happens only once per class?
Boon
  • 40,656
  • 60
  • 209
  • 315
6
votes
1 answer

Prevent Method Swizzling Objective-c

I am trying to search to prevent the Method Swizzle in my current library but more or less every documentation or blog post that i found are about how to implement Swizzling. There are couple of question that i have regarding Method swizzling that i…
Akhilesh Sharma
  • 1,580
  • 1
  • 17
  • 29
1
2 3 4 5 6 7 8