Skip to content

Re-write of Injection for Xcode in (mostly) Swift

License

Notifications You must be signed in to change notification settings

Caolongs/InjectionIII

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftEval - Yes, it's eval() for Swift

Icon

SwiftEval is a single Swift source you can add to your iOS simulator or macOS projects to implement an eval function inside classes that inherit from NSObject. There is a generic form which has the following signature:

extension NSObject {
	public func eval<T>(_ expression: String, _ type: T.Type) -> T {

This takes a Swift expression as a String and returns an entity of the type specified. There is also a shorthand function for expressions of type String which accepts the contents of the String literal as it's argument:

	public func eval(_ expression: String) -> String {
	    return eval("\"" + expression + "\"", String.self)
	}

The code works by adding an extension to your class source containing the expression. It then compiles and loads this new version of the class "swizzling" this extension onto the original class. The expression can refer to instance members in the class containing the eval class and global variables & functions in other class sources.

The command to rebuild the class containing the eval is parsed out of the logs of the last build of your application and the resulting object file linked into a dynamic library for loading. In the simulator, it was just not possible to codesign a dylib so you have to be running a small server "'signer", included in this project to do this alas.

But Why?

I Guess it could be useful in a DSL or something? You never know.

About

Re-write of Injection for Xcode in (mostly) Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 48.6%
  • Objective-C 21.6%
  • Objective-C++ 15.1%
  • C 8.1%
  • Java 3.3%
  • Rich Text Format 2.0%
  • Shell 1.3%