将UITextField值转换为字符串

时间:2014-05-04 22:45:33

标签: ios nsstring nsurl

关键是要有人将其ClientID和API输入两个UITextField,我会将它们传递给URL。好吧,我需要将它们转换为字符串。我有,但应用程序崩溃运行。最好的方法是什么?

我的代码如下:

@interface LogInViewController : UIViewController

    @property (strong, nonatomic) IBOutlet UITextField *clientID;
    @property (strong, nonatomic) IBOutlet UITextField *clientAPI;

@end

@implementation LogInViewController {
    NSString *clientIDToString;
    NSString *clientAPIToString;
}

- (IBAction)signIn:(id)sender {
    clientIDToString = _clientID.text;
    clientAPIToString = _clientAPI.text;

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.digitalocean.com/droplets/?client_id=%p&api_key=%p", clientIDToString, clientAPIToString]];

}

2 个答案:

答案 0 :(得分:2)

首先不要使用%p,但在字符串中使用%@,所以:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.digitalocean.com/droplets/?client_id=%@&api_key=%@", clientIDToString, clientAPIToString]];

这就是崩溃的原因。

第二件事:您的@property应该是weak而不是strong,因为您使用的是IBOutlet

答案 1 :(得分:1)

此:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.digitalocean.com/droplets/?client_id=%p&api_key=%p", clientIDToString, clientAPIToString]];

应该是:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.digitalocean.com/droplets/?client_id=%@&api_key=%@", clientIDToString, clientAPIToString]];