Parsing text file using Obj-C in iOS
I am constructing a tuning fork app. The fork should allow up to 12 preset pitches.
Moreover, I wish to allow the user to choose a theme. Each theme will load a set of presets (not necessary to use all of them).
My configuration file would look something like this*:
(* of course I am happy to modify the format in order to simplify the necessary code, the consideration is that the customer will be building their own file by hand, so I want to avoid unnecessary overhead / obfuscation)
theme: "A3"
comment: "An octave below concert pitch (ie A4 440Hz)"
presets: {
A3 220Hz=220.0
}
// http://en.wikipedia.org/wiki/Guitar_tuning
theme: "Guitar Standard Tuning"
comment:"EADGBE using 12-TET tuning"
presets: {
E2=82.41
A2=110.00
D3=146.83
G3=196.00
B3=246.94
E4=329.63
}
theme: "Bass Guitar Standard Tuning"
comment: "EADG using 12-TET tuning"
presets: {
E1=41.204
A2=55.000
D3=73.416
G3=97.999
}
...which need to be extracted into some structure like this:
@class Preset
{
NSString* label;
double freq;
}
@class Theme
{
NSString* label;
NSMutableArray* presets;
}
NSMutableArray* themes;
What are my options? How can I achieve this tidily? Can anyone point me towards some relevant code?
(NOTE: this is NOT a duplicate of another question that has practically an identical ti开发者_开发问答tle, so please don't flag it just because the titles match)
i find a xml property list easiest in many cases.
you can edit it as xml or with an xml editor, which is not so difficult for average people to edit.
parsing it will be much easier for you. as well, xcode will validate the xml. xcode can also convert the xml to binary when copied to the bundle, so the time to parse/read/size will all be efficient.
then you simply open it like a NSDictionary and read valueForKey:
to construct an object.
How about parsing json? the code you posted look like json.
精彩评论