cocoascript-class-babel-safe
This is a (hopefully temporary) fork of cocoascript-class
that can be safely transpiled to ES5.
Lets you create real ObjC classes in cocoascript so you can implement delegates, subclass builtin types, etc
Installation
In your plugin, assuming you're already using an ES6 build toolchain and either npm
or yarn
npm install --save cocoascript-class
or yarn add cocoascript-class
Usage
Here is an example class created with this:
const MyClass = // String values create ivar _private: 'initial' // This is a method on the class. { ; }; // MyClass is now a real ObjC class as far as cocoascript is concerned:const obj = MyClass; // You can use setters for the ivarsobj_private = "efgh"; // And call methodsobj test;obj;
Advanced
Calling super
The SuperCall
function will let you send a message to your superclass, equivalent to [super myMethod]
. However, you need to tell it the types of the arguments to your function.
;
each argument or return type is just an object with {type: encodedString}
, where encodedString is the Objective-C type encoding of that type, for example:
@encode(char*) = "*"
@encode(id) = "@" // any object can use this encoding
@encode(Class) = "#"
@encode(void*) = "^v"
@encode(CGRect) = "{CGRect={CGPoint=dd}{CGSize=dd}}"
@encode(SEL) = ":"
Here, we call [super description]
in our class' description method:
; const HasDescriptionClass = { // You should cache the result of SuperCall function, don't look it up each time if typeof MyClass_superDesc == 'undefined' const sel = ; MyClass_superDesc = ; const superDesc = MyClass_superDesc; return NSString; }; // MyClass is now a real ObjC class as far as cocoascript is concerned:const obj = HasDescriptionClass;; // calls description to become a string