你也可以rip'em'apart和字符串他們回來一起,like it looks this guys is doing....
function stringToNSString(str) {
return ['@"',str,'"'].join('');
}
function numberToNSNumber(num) {
return ['[NSNumber numberWithLong:',num,']'].join('');
}
function booleanToNSNumber(bool) {
return ['[NSNumber numberWithBool:',(bool ? 'YES' : 'NO'),']'].join('');
}
function nullToNSNull(){
return '[NSNull null]';
}
function arrayToNSArray(array){
var lines = _(array).map(function(value){
var convertedValue;
if (_(value).isString()) {
convertedValue = stringToNSString(value);
} else if (_(value).isNumber()) {
convertedValue = numberToNSNumber(value);
} else if (_(value).isBoolean()) {
convertedValue = booleanToNSNumber(value);
} else if (_(value).isNull()) {
convertedValue = nullToNSNull();
} else if (_(value).isArray()) {
convertedValue = arrayToNSArray(value);
} else if (typeof value === 'object') {
convertedValue = objectToNSDictionary(value);
}
return convertedValue;
}).map(function(value){
return value + ', ';
});
lines.unshift('[NSArray arrayWithObjects:');
lines.push('nil]');
return lines.join('');
}
function objectToNSDictionary(obj){
var lines = _(obj).map(function(value,key){
var convertedValue;
if (_(value).isString()) {
convertedValue = stringToNSString(value);
} else if (_(value).isNumber()) {
convertedValue = numberToNSNumber(value);
} else if (_(value).isBoolean()) {
convertedValue = booleanToNSNumber(value);
} else if (_(value).isNull()) {
convertedValue = nullToNSNull();
} else if (_(value).isArray()) {
convertedValue = arrayToNSArray(value);
} else if (typeof value === 'object') {
convertedValue = objectToNSDictionary(value);
}
return [convertedValue,stringToNSString(key)];
}).map(function(valueKey){
valueKey.push('');
return valueKey.join(',');
});
lines.unshift('[NSDictionary dictionaryWithObjectsAndKeys:');
lines.push('nil]');
return lines.join('\n');
}
完美,謝謝!那麼,還有一件事,JavaScrip對象究竟是什麼? – 2011-04-20 08:16:52