Created
April 8, 2011 13:20
-
-
Save monjudoh/909813 to your computer and use it in GitHub Desktop.
JavaScriptのobject・arrayからObjective-CのNSDictionary,NSArrayのコード生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function booleanToNSNumber(bool) {
return ['[NSNumber numberWithLong:',(bool ? 'YES' : 'NO'),']'].join('');
}
を
function booleanToNSNumber(bool) {
return ['[NSNumber numberWithBool:',(bool ? 'YES' : 'NO'),']'].join('');
}
にしてくれ