2012-07-15 30 views
0

在我的应用程序中,我使用从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。

+1

很难想象如果真的nameReturned是一个字符串,但会崩溃,而不管,你不应该使用用户输入的格式作为格式,并且如果你只是在做我,你不需要任何格式s复制一个字符串。 – 2012-07-15 22:58:59

+0

你可以在你设置'nameRetrieved'的地方发布你的代码吗? – Imirak 2012-07-15 23:03:17

+0

我只是编辑答案...这些问题安装了2个或更多字的onluy ... – doxsi 2012-07-16 08:50:52

回答

1

请发表更多信息。

它为我

工作正常.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 
+0

Tx用于回答。我正在试验的问题是将大约2(或更多)个单词用于空格分隔。该应用程序不会为一个单词而崩溃,而是以2为单位,是的。 – doxsi 2012-07-16 08:42:26

+0

我编辑我的答案.... – doxsi 2012-07-16 08:51:17

+0

我编辑了我的答案.. – 2012-07-17 06:36:12