Question

I'm attempting to generate some javascript from objective C. I have a simple objective-c object: only NSStrings, NSArrays, NSDictionarys, and NSNumbers - and I'd like to insert it as an object literal into javascript. What's the best way to do this?

So far I've managed to serialize the object and get a JSON string, but JSON isn't exactly equivalent to javascript object literals. I could insert it as a JSON.parse('%@') call, but that seems inefficient:

(controller.createWindow).apply((controller), JSON.parse('[\"LoggedIn\",3,1,[0,0,480,320],false,false]'))
Était-ce utile?

La solution

Here's the full code:

//turn an NSObject into an NSString
+ (NSString *)jsonDumps:(id)obj {
    NSError *error;
    NSData *utf8stringified = [NSJSONSerialization dataWithJSONObject:obj options:0 error:&error];
    if (utf8stringified == nil) {
        [NSException raise:@"Failed to jsonify arguments" format:@"Failed to jsonify arguments %@: %@",
         obj, error];
    }

    NSString *stringified = [[[NSString alloc] initWithData:utf8stringified encoding:NSUTF8StringEncoding]
                             autorelease];
    return stringified;
}

//turn a JSON string into a JS literal
+(NSString *)jsonToJSLit:(NSString *)jsonString {
    NSMutableString *s = [NSMutableString stringWithString:jsonString];
    [s replaceOccurrencesOfString:@"\u2028" withString:@"\\u2028" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\u2029" withString:@"\\u2029" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    return [NSString stringWithString:s];
}

//turn an object into a JS string literal
+(NSString *)objToJSLiteral:(id)obj {
    return [self jsonToJSLit:[self jsonDumps:obj]];
}

I thought there was more of a difference between JSON and JS literals, but apparently it's just those two unicode characters!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top