Hot Code Swapping in Xcode?

前端 未结 4 989
野性不改
野性不改 2020-12-30 05:28

I am doing iOS game development using the cocos2d framework, and I tremendously envy the ability for Eclipse Java programmers to hot swap their code while they are debugging

相关标签:
4条回答
  • 2020-12-30 05:31

    You can hot swap a variable value in Xcode by:

    expression <variable> = <value>;.

    By having a break point in the place where you wanna change the value and doing the command in Xcode console.

    Example:

    // Messages From Console
    (lldb) expression graphFlag = @"X";    // Update variable value
    (__NSCFConstantString *) $0 = 0x36f95718 @"X"  // Xcode prints the updated value
    (lldb) expression graphFlag; // Printing value through expression command
    (__NSCFConstantString *) $1 = 0x36f95718 @"X" // Hot Swapped variable value
    
    0 讨论(0)
  • 2020-12-30 05:35

    Not possible with the current tools.

    Keep in mind that iOS applications are signed -- if you change a single byte you'd have resign the whole thing. One could imagine making it work using runtime's support for dynamically adding and removing methods. But that'd surely require adding some extra stuff to support it on the device, and that's something that malware could easily take advantage of. So it's probably not a feature you'll be likely to see anytime soon.

    By the way, Xcode versions 1.x-3.x did have a "Fix and Continue" feature. You could edit as you were debugging, use the Fix and Continue command, and continue running the updated code. I believe it was removed at some point, perhaps due to some combination of: requiring that your project be configured to use "zero link" and perhaps some other things; being less than completely reliable; probably not supporting iOS; the switch to llvm; other changes in Xcode 4. Maybe it'll come back someday -- if you want it back, file a bug -- but again, I think supporting it on iOS would be a challenge.

    0 讨论(0)
  • 2020-12-30 05:35

    If you're just talking about changing variable values then you can achieve that surreptitiously via lldb (or, presumably) gdb. Supposing you had:

    - (void)uselessMethod
    {
        NSString *localString = @"I'm some local text";
    
        NSLog(@"%@", localString);
    }
    

    And put a breakpoint on the NSLog, at that point you could ask lldb to evaluate a reassignment of localString as a means of performing it. E.g.

    po localString = @"Hat"
    

    If you then allow program execution to continue, you should find that the reassignment has stuck. Similarly you can call any method or perform any other sort of assignment.

    I've just tested this against Xcode 4.3.2.

    0 讨论(0)
  • 2020-12-30 05:40

    There is a great plugin which allow changing code in live, working APP. It is called InjectionPlugin.

    As FAQ says:

    How does it work? The Objective-C runtime allows you to load a new version of a class into an application using a bundle even if there is already an implementation linked into the application. Swizzling is used as the bundle is loaded to switch the existing class to use the methods of the new implementation. This can be done repeatedly and works for OSX and iOS applications and on iOS devices.

    I made some small video which shows how to install and use this plugin http://nomtek.com/developers/how-to-install-and-use-injection-plugin-for-xcode/

    Hope it helps!

    0 讨论(0)
提交回复
热议问题