[英]setValue:forKey: with a NSString
I'm currently working on a small and specific iPhone app to connect to a server and download some JSON data. 我目前正在开发一个小型且特定的iPhone应用程序,以连接到服务器并下载一些JSON数据。 My problem is about a property list I use to save some data (login, password and domain of the server). 我的问题是关于我用来保存一些数据(服务器的登录名,密码和域)的属性列表。 I've easily created the plist in Xcode but when I try to edit it with the texts entered by the user, I'm having a problem and I don't know how to fix it ... 我已经很容易在Xcode中创建了plist,但是当我尝试使用用户输入的文本对其进行编辑时,出现了问题,并且我不知道如何解决...
Here is the textFieldDidEndEditing: method
, in which I try to write to the plist, with what the user have written in a domainTextField : 这是textFieldDidEndEditing: method
,在该textFieldDidEndEditing: method
中,我尝试使用用户在domainTextField中编写的内容写入plist:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filepath = [NSString stringWithFormat:@"%@/userInfo.plist", [paths objectAtIndex:0]];
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] initWithContentsOfFile:filepath];
if (self.domainTextField) {
NSString *domain = [NSString stringWithFormat:@"%@", self.domainTextField.text];
[userInfo setValue:domain forKey:@"domain"];
}
if ([userInfo writeToFile:filepath atomically:TRUE]) {
NSLog(@"write succeeded");
} else {
NSLog(@"write failed");
}
}
I'm always receiving a "write failed" error here and after playing a bit with the debugger, I found that the problem is with the setValue:forKey: method
. 我总是在这里收到“写入失败”错误,并且在与调试器一起玩了一段时间之后,我发现问题出在setValue:forKey: method
。 In fact, I expect this method to write that in the plist : domain = "whateverthedomainis"
because domain
is a NSString but it writes instead : domain = whateverthedomainis
. 实际上,我希望此方法将其写在plist中: domain = "whateverthedomainis"
因为domain
是一个NSString,但是它改为: domain = whateverthedomainis
。 And as the writeToFile:atomically: method makes sure that all the objects are property list objects, it returns NO. 正如writeToFile:atomically:方法确保所有对象都是属性列表对象一样,它返回NO。 I tried to put backslashes, but that didn't help. 我试图加反斜杠,但这没有帮助。
What can I do to resolve this error? 如何解决此错误?
如果字符串中有任何参数,将使用StringWithFormat,这样您就可以直接分配一个字符串值,就像这样
NSString *domain = self.domainTextField.text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.