Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法附加到空字符串_Ios_Xcode_String - Fatal编程技术网

Ios 无法附加到空字符串

Ios 无法附加到空字符串,ios,xcode,string,Ios,Xcode,String,在我的项目中,我需要接受来自文本框的用户输入,然后格式化该输入,然后将格式化的输入附加到另一个字符串中,该字符串是我稍后提交的URL 我的文本框是inputZip、inputCity、inputState,输入将保存在名为location的字符串中。当输入位于zip code字段时,它会将文本框的内容保存到相应位置,并且工作正常。当输入是city/state时,我需要将两个文本框中的输入格式化为“state/city”,但我无法获取格式化和保存该信息的位置,它只是保持空白。这是我到目前为止的代码

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

我的文本框是inputZip、inputCity、inputState,输入将保存在名为location的字符串中。当输入位于zip code字段时,它会将文本框的内容保存到相应位置,并且工作正常。当输入是city/state时,我需要将两个文本框中的输入格式化为“state/city”,但我无法获取格式化和保存该信息的位置,它只是保持空白。这是我到目前为止的代码,带有注释

@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,它是否将其视为空字符串,但如果是这种情况,它甚至不应该查看city和state字段。我卡住了

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

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:
不会修改字符串(毕竟您使用的是不可变字符串),而是返回一个新字符串:

返回通过向接收方追加给定字符串而生成的新字符串

该代码应该是:

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
-stringByAppendingString:

返回通过向接收方追加给定字符串而生成的新字符串

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

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

这两个选项都有意义,但当我尝试它们时,位置仍然是空的。我错过了您也将这些消息发送到
nil
(请参阅更新的答案)我修复了nil问题,但我仍然遇到相同的位置为空的问题。这两种建议都会发生,有和没有可变字符串。是不是
self.inputZip
真的
nil
?或者只是一个空字符串?类似于
if([self.inputZip count]>0)
的内容可能更接近您要查找的内容。如果这不能解决您的问题:添加一些断点,逐步查看代码以查看执行的代码路径,同时打印对象描述。如果你以前没有学过,这是一个学习无价技能的好机会。