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。

4

1 回答 1

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
于 2012-07-16T04:00:58.127 回答