从文本字段使用空格使应用程序崩溃
问题描述:
在我的应用程序中,我使用从UITextField传递的字符串。它可以工作,但是当用户输入带有空格(即2个或更多字)的文本时,应用程序会在我使用它的地方崩溃。从文本字段使用空格使应用程序崩溃
.h
NSString *nameReturned
IBOutlet UITextField *nameField;
.m
nameReturned = nameField.text;
在应用程序崩溃是有一点:
NSLog(@"name returned %@",nameReturned); //here the NSLOg returns the string with the blanks spaces, I mean 2 or more word correctly
NSString *name = [[NSString alloc] initWithFormat:@"%@", nameReturned]; //there the app crash if blank spaces are present.
我用这个字符串来获得我使用的URL请求的URL。
答
请发表更多信息。
它为我
工作正常.H
@interface MyClass : UIViewController {
NSString *nameReturned;
IBOutlet UITextField *nameField;
}
@property (nonatomic, retain) IBOutlet UITextField *nameField;
-(IBAction)clickMe;
@end
.M
@implementation MyClass
@synthesize nameField;
-(IBAction)clickMe{
nameReturned = [[nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain];
NSLog(@"name returned %@",nameReturned); //here the NSLOg returns the string with the blanks spaces, I mean 2 or more word correctly
NSString *name = [[NSString alloc] initWithFormat:@"%@", nameReturned]; //there the app crash if blank spaces are present.
NSURL *uRL = [NSURL fileURLWithPath:name];
//release the nameField in dealloc and nameReturned your after usage
}
@end
很难想象如果真的nameReturned是一个字符串,但会崩溃,而不管,你不应该使用用户输入的格式作为格式,并且如果你只是在做我,你不需要任何格式s复制一个字符串。 – 2012-07-15 22:58:59
你可以在你设置'nameRetrieved'的地方发布你的代码吗? – Imirak 2012-07-15 23:03:17
我只是编辑答案...这些问题安装了2个或更多字的onluy ... – doxsi 2012-07-16 08:50:52