Objective-c:极其奇怪且烦人的字符串问题

发布于 2024-10-13 05:30:17 字数 2231 浏览 3 评论 0原文

我一直在开发一个表格视图程序,并且有一个函数可以处理来自用户首选项和核心数据的多个数据。程序解析这些东西并返回一个 url 地址。代码如下:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 NSString *serverAdress = [prefs stringForKey:@"serverAdress"];
 serverAdress = [serverAdress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"dd.MM.yyyy"];

 NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
 [dateFormatter2 setDateFormat:@"dd.MM.yyyy HH:mm"];

 NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

 NSString *dateTimeString = [NSString stringWithFormat:@"%@ %@",dateString,[dashboardParameters attribute1]];

 NSTimeInterval dayInterval = [[dashboardParameters attribute2] intValue]*60*60*(-1);
 NSDate *date2 = [[dateFormatter2 dateFromString:dateTimeString] addTimeInterval:dayInterval];
 NSString *urlString = 
 [NSMutableString stringWithFormat:@"%@/webservices/service1.asmx/getHourlySales2?tarih2=%@&tarih1=%@&salesType=%@",
  serverAdress,
  dateTimeString,
  [dateFormatter2 stringFromDate:date2],
  [dashboardParameters itemOrder]
  ];
 urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
 return urlString;

一切正常,我在检索数据方面没有问题。但是当我执行代码时,我得到以下日志输出:

//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi/webservices/service1.asmx/getHourlySales2 ?tarih2=21.01.2011%2022:00&tarih1=21.01.2011%2011:00&salesType=Hepsi

必须是

//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi

但奇怪的是程序添加了

/webservices/service1.asmx/getHourlySales2?tarih2=21.01.2011%2022:00&tarih1=21.01.2011%2011:00&salesType=Hepsi

正常处理后自行处理。

删除 NSString *serverAdress = [prefs stringForKey:@"serverAdress"] 并手动输入 serveradress 时,代码正常运行

另外一件重要的事情是,当我从代码中

。请帮助,程序中的所有内容都工作正常,但我遇到了这个问题。

谢谢你的帮助。

i've been working on a tableview program and i have a function that processes several data from user preferences, and core-data. the program parses these things and returns a url adress. heres the code:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 NSString *serverAdress = [prefs stringForKey:@"serverAdress"];
 serverAdress = [serverAdress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"dd.MM.yyyy"];

 NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
 [dateFormatter2 setDateFormat:@"dd.MM.yyyy HH:mm"];

 NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

 NSString *dateTimeString = [NSString stringWithFormat:@"%@ %@",dateString,[dashboardParameters attribute1]];

 NSTimeInterval dayInterval = [[dashboardParameters attribute2] intValue]*60*60*(-1);
 NSDate *date2 = [[dateFormatter2 dateFromString:dateTimeString] addTimeInterval:dayInterval];
 NSString *urlString = 
 [NSMutableString stringWithFormat:@"%@/webservices/service1.asmx/getHourlySales2?tarih2=%@&tarih1=%@&salesType=%@",
  serverAdress,
  dateTimeString,
  [dateFormatter2 stringFromDate:date2],
  [dashboardParameters itemOrder]
  ];
 urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
 return urlString;

Everything works great, i have no problem on retrieving data. But when i execute the code, i get this log output:

//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi/webservices/service1.asmx/getHourlySales2?tarih2=21.01.2011%2022:00&tarih1=21.01.2011%2011:00&salesType=Hepsi

it has to be

//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi

But strangely the program adds

/webservices/service1.asmx/getHourlySales2?tarih2=21.01.2011%2022:00&tarih1=21.01.2011%2011:00&salesType=Hepsi

by itself after the normal processing.

Also another important thing, the code runs normally when i remove

NSString *serverAdress = [prefs stringForKey:@"serverAdress"] from the code and enter serveradress manually.

Plase help , everything in the program works fine but im stuck with this problem.

Thanks for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2024-10-20 05:30:17

那么,在将 serverAdress 添加到 urlString 之前, 中会包含哪些内容。看起来好像 serverAdress 包含 '//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi'

So what's in serverAdress before you add it to urlString. Looks as if serverAdress contains '//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文