网址添加到字符串报错

打印这个网页上的内容
http://123.56.189.59/englishStudy/queryMoviesbyName?info={"movieName":"的"}

Paste_Image.png

上图的报错是因为字符串的@"" 和网址中的""起了冲突.解决办法就是用转意符\修饰"

Paste_Image.png

还要注意网址中的汉字,全部代码如下


    NSString *urlString = @"http://123.56.189.59/englishStudy/queryMoviesbyName?info={\"movieName\":\"的\"}";
    
    // UTF8转码
    NSString *string = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:string]];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData 
* _Nullable data, NSURLResponse 
* _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
        NSLog(@"%@" ,dict);
    }];

你可能感兴趣的:(网址添加到字符串报错)