gpt4 book ai didi

objective-c - 字符串中的 URL 给出 null

转载 作者:行者123 更新时间:2023-12-03 17:31:46 25 4
gpt4 key购买 nike

我有一个给出路径的字符串,另一个将参数附加到它的字符串。当我将它们放入字符串并显示时,我得到了正确的格式。如果我尝试将整个字符串放入 NSURL 中,它会显示 NULL。获取它的格式是什么?

  NSString *booking=urlForBooking.bookHall;
NSLog(@" book %@",booking); // this prints --- http://10.2.0.76:8080/ConferenceHall/BookingHallServlet

NSString *bookingString=[booking stringByAppendingString:[NSString stringWithFormat:@"? employeeId=%@&conferenceHallId=%@&bookingId=%d&purpouse=%@&fromDate=%@&toDate=%@&comments=%@&submit=1",empId,_hallId,_bookingId,_purpose,fromDateStr,toDateStr,_comments]];
NSLog(@"book str %@",bookingString); //this prints --- ?employeeId=3306&conferenceHallId=112&bookingId=0&purpouse=S&fromDate=25/Feb/2013 13:29&toDate=25/Feb/2013 15:29&comments=C&submit=1

NSURL *bookingURL=[NSURL URLWithString:bookingString];
NSLog(@"BOOK %@",bookingURL); //here I'm not getting the url(combined string), it gives null.

最佳答案

这是因为您正在构建的 URL 包含在 URL 中无效的字符,例如空格和斜杠。

您应该转义这些字符:

NSString *bookingPath =[bookingString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *bookingURL=[NSURL URLWithString:bookingPath];

您可能需要替换日期中的斜杠,因为它们可能未正确编码。

NSString *bookingString=[NSString stringWithFormat:@"%@?employeeId=%@&conferenceHallId=%@&bookingId=%d&purpouse=%@&fromDate=%@&toDate=%@&comments=%@&submit=1",
booking,
empId,
_hallId,
_bookingId,
[_purpose stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[fromDateStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[toDateStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[_comments stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURL *bookingURL=[NSURL URLWithString:bookingString];
NSLog(@"BOOK %@",bookingURL);

关于objective-c - 字符串中的 URL 给出 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com