Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

iOS development tutorial

Recently I was troubleshooting multithreading issues in an iOS app for IIN. Here is some code that can identify what method and class called a given method. Useful for logging and debugging race conditions.


//Add this private instance method to the class you want to trace from
-(void)trace
{
  //Go back 2 frames to account for calling this helper method
  //If not using a helper method use 1
  NSArray* stack = [NSThread callStacksSymbols];
  if (stack.count > 2)
    NSLog(@"Caller: %@", [stack objectAtIndex:2]);
}

//Add this line to the method you want to trace from
[self trace];

In the output window you will see something like:


Caller: 2 MyApp 0x0004e8ae -[IINClassroomInit buildMenu] + 86

You can also parse this string to extract more data about the stack frame:

2 = Thread id
My App = Your app name
0x0004e8ae = Memory address of caller
-[IINClassroomInit buildMenu] = Class and method name of caller
+86 = Number of bytes from the entry point of the caller that your method was called

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

Martin is a New York area software developer and MBA with 10+ years of experience on the Microsoft stack. Over the past few years he has also expanded into iOS development using native Objective-C. he architects and develops full-stack web applications, iOS apps, database systems, and backend services.

Comments

comments