Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sending a Javascript Array in Cocoa



That's right! Thanks a lot darin! :)

I changed my NSArray into an NSMutableArray, the "count" into lengthObject and it's perfect!

- (void)setArrayOfSteps:(id)object
{
id lengthObject = [object valueForKey:@"length"];
unsigned length = [lengthObject isKindOfClass:[NSNumber class]] ? [lengthObject unsignedIntValue] : 0;
arrayOfSteps = [[NSMutableArray alloc] initWithCapacity:length];
unsigned i;
for (i = 0; i < length; ++i) {
id element = [object webScriptValueAtIndex:i];
if (element)
[arrayOfSteps addObject:element];
}
NSLog(@"arrayOfSteps filled with %d elements !", [arrayOfSteps count]);
}


Mathieu Thouvenin

On Aug 29, 2007, at 18:14 , Darin Adler wrote:

If you look at the documentation in the WebScriptObject.h header file, you'll see that an NSArray does turn into a JavaScript array object, but a JavaScript array object does not turn into an NSArray. There are many reasons for this, one being that there are many different kinds of objects in JavaScript that act like arrays, and luckily it's easy to cope with. You can modify your code to be something like this:

- (void)setArrayOfSteps:(id)object
{
id lengthObject = [object valueForKey:@"length"];
unsigned length = [lengthObject isKindOfClass:[NSNumber class]] ? [count unsignedIntValue] : 0;
arrayOfSteps = [[NSArray alloc] initWithCapacity:length];
unsigned i;
for (i = 0; i < length; ++i) {
id element = [object webScriptValueAtIndex:i];
if (element)
[arrayOfSteps addObject:element];
}
NSLog(@"arrayOfSteps filled with %u elements !", [arrayOfSteps count]);
}


Hope that helps.

    -- Darin




_______________________________________________ Do not post admin requests to the list. They will be ignored. Webkitsdk-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webkitsdk-dev/email@hidden

This email sent to email@hidden
References: 
 >Sending a Javascript Array in Cocoa (From: Mathieu Thouvenin <email@hidden>)
 >Re: Sending a Javascript Array in Cocoa (From: Darin Adler <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.