GithubHelp home page GithubHelp logo

isabella232 / cocoascript-class Goto Github PK

View Code? Open in Web Editor NEW

This project forked from atlassian/cocoascript-class

0.0 0.0 0.0 20 KB

A fork of cocoascript-class that can be safely transpiled to ES5

License: MIT License

JavaScript 100.00%

cocoascript-class's Introduction

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 = new ObjCClass({
  // 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:
const obj = MyClass.new();

// You can use setters for the ivars
obj._private = "efgh";

// And call methods
[obj test];
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:

import ObjCClass, {SuperCall} from 'cocoascript-class';

const HasDescriptionClass = new ObjCClass({  
  description() {
    // You should cache the result of SuperCall function, don't look it up each time
    if (typeof MyClass._superDesc == 'undefined') {
      const sel = NSStringFromSelector('description');
      MyClass._superDesc = SuperCall(sel, [], {type:"@"});
    }
    const superDesc = MyClass._superDesc.call(this);
    return NSString.stringWithString(`${superDesc} { _private=${this._private} }`);
  }
});

// MyClass is now a real ObjC class as far as cocoascript is concerned:
const obj = HasDescriptionClass.new();
log(obj); // calls description to become a string

cocoascript-class's People

Contributors

darknoon avatar kannonboy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.