Skip to content

Instantly share code, notes, and snippets.

@mactive
Last active July 24, 2016 13:34
Show Gist options
  • Save mactive/fc2c88ecbc31cf039d2b4f8fbdb061a3 to your computer and use it in GitHub Desktop.
Save mactive/fc2c88ecbc31cf039d2b4f8fbdb061a3 to your computer and use it in GitHub Desktop.
JS中调用OC的方法,JSExport JSContext
var loadPeopleFromJSON = function(jsonString) {
var data = JSON.parse(jsonString);
var people = [];
for (i = 0; i < data.length; i++) {
var person = Person.createWithFirstNameLastName(data[i].first, data[i].last);
person.birthYear = data[i].year;
people.push(person);
}
return people;
}
[
{
"first": "Grace",
"last": "Hopper",
"year": 1906
},
{
"first": "Ada",
"last": "Lovelace",
"year": 1815
},
{
"first": "Margaret",
"last": "Hamilton",
"year": 1936
}
]
// in Person.h -----------------
@class Person;
@protocol PersonJSExports <JSExport>
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property NSInteger ageToday;
- (NSString *)getFullName;
// create and return a new Person instance with `firstName` and `lastName`
+ (instancetype)createWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
@end
@interface Person : NSObject <PersonJSExports>
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property NSInteger ageToday;
@end
// in Person.m -----------------
@implementation Person
- (NSString *)getFullName {
return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
+ (instancetype) createWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
Person *person = [[Person alloc] init];
person.firstName = firstName;
person.lastName = lastName;
return person;
}
@end
//
// PersonJSExports.m
// ReactiveCocoaPractice
//
// Created by mengqian on 23/7/2016.
// Copyright © 2016 thinktube. All rights reserved.
//
#import "PersonJSExports.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "Person.h"
@interface PersonJSExports()
@property(nonatomic, strong)JSContext * context;
@end
@implementation PersonJSExports
@synthesize context;
-(id)init
{
if ((self = [super init])){
// set self
[self initContext];
}
return self;
}
-(void)initContext
{
context = [[JSContext alloc] init];
// [context evaluateScript:@"var num = 5 + 5"];
// [context evaluateScript:@"var names = ['Grace', 'Ada', 'Margaret']"];
// [context evaluateScript:@"var triple = function(value) { return value * 3 }"];
//
// JSValue *tripleNum2 = [context evaluateScript:@"triple(num)"];
// NSLog(@"tripleNum2: %d",[tripleNum2 toInt32]);
// 给JS提供Object 和方法 其实就是model Person.createWithFirstName:lastName:;
context[@"Person"] = [Person class];
// load PeopleJson
// 加载测试数据
NSString *peopleJSON = [NSString stringWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: @"people" ofType: @"json"]
encoding:NSUTF8StringEncoding
error:nil];
// load People.js
// 加载people解析js文件, 这个js文件中使用了 Person.createWithFirstNameLastName方法
NSString *peopleJSString = [NSString stringWithContentsOfFile:
[[NSBundle mainBundle] pathForResource: @"people" ofType: @"js"]
encoding:NSUTF8StringEncoding
error:nil];
[context evaluateScript:peopleJSString];
JSValue *load = context[@"loadPeopleFromJSON"];
// call with JSON and convert to an NSArray
JSValue *loadResult = [load callWithArguments:@[peopleJSON]];
// 得到一个包含三个Person class 的数\
NSArray *people = [loadResult toArray];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment