无法附加到空字符串

时间:2014-10-23 20:17:51

标签: ios xcode string

在我的项目中,我需要接受来自文本框的用户输入,然后格式化该输入,然后将格式化的输入附加到另一个字符串,这是我稍后提交的URL。

我的文本框是inputZip,inputCity,inputState,输入将保存在名为location的字符串中。当输入位于邮政编码字段中时,它会将文本框的内容保存到位置并正常工作。当输入是城市/州时,我需要将两个文本框中的输入格式化为"州/城市",但是我无法获得格式化并保存该信息的位置,它只是保持空白。到目前为止,这是我的代码,带有注释。

@interface ViewController ()
- (IBAction)btnSubmit:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *inputZip;
@property (weak, nonatomic) IBOutlet UITextField *inputCity;
@property (weak, nonatomic) IBOutlet UITextField *inputState;

@property NSString *location;
@property NSMutableString *queryURL;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.location = [[NSString alloc]init];
    self.queryURL = [[NSMutableString alloc]initWithString:@"http://api.wunderground.com/api/api-key/forecast/q/"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnSubmit:(id)sender {
    // IF inputZip is not empty
        // assign location
    // ELSE inputCity + inputState = ST/City
        // assign location
    // call getWeather(location)

    // clear previous location
    self.location = nil;

    if (self.inputZip != nil){
        self.location = self.inputZip.text;
        //self.location = [NSString stringWithFormat:@"%@",self.inputZip.text];
        // both of these lines work fine
    }else{
        //self.location = [NSString stringWithFormat:@"%@/%@", self.inputState, self.inputCity];
        //self.location = [NSString stringWithFormat:@"%@/%@", self.inputState.text, self.inputCity.text];
        [self.location stringByAppendingString:self.inputState.text];
        [self.location stringByAppendingString:@"/"];
        [self.location stringByAppendingString:self.inputCity.text];
        // none of these approaches work, location comes up empty each time
    }

    // print variables to check them
    NSLog(@"zip = %@", self.inputZip.text);
    NSLog(@"city = %@", self.inputCity.text);
    NSLog(@"state = %@", self.inputState.text);
    NSLog(@"loc = %@", self.location);
    NSLog(@"--------------------");
}

-(void) getWeather:(NSString*)location{
    // build query using location
    // submit query
    // update results page
}
@end

我想知道它是否不起作用,因为拉链字段不是零,如果它认为它是一个空字符串,但如果是这样的话它不应该是偶数看看城市和州的田野。我被卡住了。

为了好玩,这是我的输出:

2014-10-23 15:12:45.738 WeatherApp[33763:1934020] zip = 12345
2014-10-23 15:12:45.738 WeatherApp[33763:1934020] city = 
2014-10-23 15:12:45.739 WeatherApp[33763:1934020] state = 
2014-10-23 15:12:45.739 WeatherApp[33763:1934020] loc = 12345
2014-10-23 15:12:45.739 WeatherApp[33763:1934020] --------------------
2014-10-23 15:12:50.495 WeatherApp[33763:1934020] zip = 
2014-10-23 15:12:50.495 WeatherApp[33763:1934020] city = CHICAGO
2014-10-23 15:12:50.495 WeatherApp[33763:1934020] state = IL
2014-10-23 15:12:50.495 WeatherApp[33763:1934020] loc = 
2014-10-23 15:12:50.496 WeatherApp[33763:1934020] --------------------

2 个答案:

答案 0 :(得分:1)

您的代码中有两个问题,第一个是您向nil发送邮件,第二个是您丢弃修改后的字符串。

nil

发送消息

在if语句之前,您将属性设置为nil

// clear previous location
self.location = nil;

您可能知道,向nil发送消息不会做任何事情。所以,当你致电

时,几行

[self.location stringByAppendingString:self.inputState.text];

你实际上是在打电话

[nil stringByAppendingString:self.inputState.text];

什么也没做。如果要清除之前的值,则注释表示您可以将该属性设置为空字符串。

// clear previous location
self.location = @"";

丢弃修改后的字符串

如果仔细观察文档,您会发现stringByAppendingString:没有修改字符串(毕竟你使用的是 immutable 字符串),而是返回一个新字符串:

  

- stringByAppendingString:

     

返回通过将给定字符串附加到接收器而生成的新字符串。

该代码应该是:

self.location = [self.location stringByAppendingString:self.inputState.text];
self.location = [self.location stringByAppendingString:@"/"];
self.location = [self.location stringByAppendingString:self.inputCity.text];

或者您应该使用 mutable 字符串:

NSMutableString *mutableString = [self.location mutableCopy]; // a mutable copy
[mutableString appendString:self.inputState.text];
[mutableString appendString:@"/"];
[mutableString appendString:self.inputCity.text];
self.location = [mutableString copy]; // make an immutable copy again

答案 1 :(得分:1)

  

- stringByAppendingString:

     

返回通过将给定字符串附加到接收器而生成的新字符串。

然后您丢弃新字符串...需要将其分配回self.location

self.location = [self.location stringByAppendingString:self.inputState.text];