ios - 无法附加到空字符串

标签 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

我想知道它是否不起作用,因为 zip 字段不是 nil,如果它认为它是一个空字符串,但如果是这种情况,它甚至不应该查看城市和州字段。我卡住了。

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

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] --------------------

最佳答案

您的代码有两个问题,第一个是您将消息发送到 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: 不会修改字符串(毕竟您使用的是 不可变 字符串)而是返回一个新的字符串字符串:

- stringByAppendingString:

Returns a new string made by appending a given string to the receiver.

该代码应该是:

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

或者你应该使用一个可变的字符串来代替:

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

关于ios - 无法附加到空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536656/

相关文章:

ios - 使用 Core Location 的 Swift 后台获取位置更新

ios - 在 Swift 中将对象从一个类移动到另一个类

ios - 如何使用 UIAutomation 处理慢速网络连接

swift - 是否可以在旧的 Xcode 上编译最新的 Swift 版本?

javascript - AngularJS 将字符串转换为引用 $scope 中对象的对象/数组

ios - 如何在 iOS 4 中从 UIViews 加载 nib?

ios - 从 xcode 更新应用程序而不是全新安装

objective-c - Xcode 无法解析 zlib 符号

c - C 字符串程序的输出

string - 如何使用 GWT 将字节数组转换为字符串,并将字符串转换为字节数组?