除了一个字符串之外,我预先填写了信息,用户将输入该字符串。我目前只能用这段代码读取一个密钥:
#define defaultValue @"Someone"
#define myKey @"Event"
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *val = [defaults stringForKey:myKey];
if (val == nil) val = defaultValue;
[myTextField setStringValue:val];
我用这段代码写了:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *val;
// Get the new value from the textfield
val = [myTextField stringValue];
if ([val isEqualToString:defaultValue]) {
[defaults removeObjectForKey:myKey];
} else {
[defaults setObject:val forKey:myKey];
}
我真正需要的是将数组写入这样的.plist,并使第5个字符串能够成为用户输入字符串。我怎么能这样做?:
<plist version="1.0">
<dict>
<key>Event/key>
<dict>
<key>Staff Meeting</key>
<string>Friday Feb 3rd, 2012 </string>
<key>Contact</key>
<string>events@whitehouse.gov</string>
<key>URL</key>
<string>http://www.whitehouse.gov</string>
<key>Status</key>
<string>ATTENDING</string>
<key>Name</key>
<string>Mr. President</string> <------- USER TEXT INPUT
<key>Signature</key>
<data>
20001
</data>
</dict>
</dict>
</plist>
答案 0 :(得分:0)
这是一个创建所需结构的示例。
-(void)saveData:(NSString*)someParam
{
NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:1];
NSDictionary *innerDict;
NSString *staffMeetingValue;
NSString *contactValue;
NSString *nameValue;
staffMeetingValue = @"Friday Feb 3rd, 2012";
contactValue = @"events@whitehouse.gov";
nameValue = someParam;
innerDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: staffMeetingValue, contactValue, nameValue, nil]
forKeys:[NSArray arrayWithObjects:@"Staff Meeting", @"Contact", @"Name"]];
[rootObj setObject:innerDict forKey:@"Event"];
id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj
format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
}
有关其他信息,请参阅Creating Property Lists Programmatically
希望它有所帮助。
P.S。您必须为其他标记添加键和对象。为了简单起见,我跳过它们。
修改强>
这是一个如何做到这一点的简单示例。 staffMeetingValue
和contactValue
值是硬编码的,但您可以将其作为nameValue
的参数传递。
-(void)saveDataWithName:(NSString*)nameValue staffMeting:(NSString*)staffMetingValue contact:(NSString*)contactValue {
// previous stuff here
}