Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Latest commit

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

cocoascript-class

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:

constMyClass=newObjCClass({// String values create ivar_private: 'initial',// This is a method on the class.test(){log("test: "+this._private);},});// MyClass is now a real ObjC class as far as cocoascript is concerned:constobj=MyClass.new();// You can use setters for the ivarsobj._private="efgh";// And call methods[objtest];obj.test();

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.

SuperCall(sel,argumentTypes,returnType);

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:

importObjCClass,{SuperCall}from'cocoascript-class';constHasDescriptionClass=newObjCClass({description(){// You should cache the result of SuperCall function, don't look it up each timeif(typeofMyClass._superDesc=='undefined'){constsel=NSStringFromSelector('description');MyClass._superDesc=SuperCall(sel,[],{type:"@"});}constsuperDesc=MyClass._superDesc.call(this);returnNSString.stringWithString(`${superDesc} { _private=${this._private} }`);}});// MyClass is now a real ObjC class as far as cocoascript is concerned:constobj=HasDescriptionClass.new();log(obj);// calls description to become a string

About

Lets you create real ObjC classes in cocoascript so you can implement delegates, subclass builtin types, etc

Topics

Resources

Stars

23 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages