Hot Code Swapping in Xcode?

a 夏天 提交于 2019-11-29 06:47:43

问题


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 their application (i.e. change out variable values within a method and have the program live update as if you had a REPL without having to recompile).

This seems like something that would be absolutely tremendously helpful when it came to iOS development, where my development environment is (obviously) Xcode and the language I am programming in is Objective C. I have googled around but havent been able to find anything - so I thought I would ask the community.

Does anyone know if there a way to Hot Swap code in Xcode while programming in Objective C?

Thanks in advance


回答1:


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.




回答2:


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!




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/10196155/hot-code-swapping-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!