1

我编写了一些代码来获取我想要在 ARC 中使用的格式的字符串,我得到了 EXC_BAD_ACCESS(code=2 address=0x100)。

这里的代码:

NSString *inCook = [NSString stringWithFormat:@"GETAFREE_AUTH_HASH=%@; GETAFREE_USER_ID=%@", FRLAuthHash, FRLUserID];
NSLog(@"%@",inCook);

NSString *cookie1 = [[NSString alloc]init]; 
cookie1 = [NSString stringWithFormat:@"__utma=138759908.342110698.1339513728.1347619146.1347628547.51; __utmz=138759908.1339513728.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __atuvc=0%7C33%2C0%7C34%2C0%7C35%2C16%7C36%2C50%7C37; GETAFREE_REFERRAL_URL=https%3A%2F%2Fwww.freelancer.com%2Fusers%2Fonlogin.php; _chartbeat2=skcgjvrzgugx7kr0.1339513733066.1347628548930.00000000000001; WRUID=0; session2=6d909115a69f37ff641d798045d6f252; __qca=P0-624434954-1344332147605; FL_DASHBOARD_VIEWPAGE_4164248=sellerview; FL_DASHBOARD_TABINDEX_4164248=6; GETAFREE_LANGUAGE=en; __utmc=138759908; __utmb=138759908.1.10.1347628547; SpryMedia_DataTables_project_table_=%7B%22iCreate%22%3A1347628548100%2C%22iStart%22%3A0%2C%22iEnd%22%3A0%2C%22iLength%22%3A10%2C%22sFilter%22%3A%22%22%2C%22sFilterEsc%22%3Atrue%2C%22aaSorting%22%3A%5B%20%5B7%2C%22desc%22%5D%5D%2C%22aaSearchCols%22%3A%5B%20%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%5D%2C%22abVisCols%22%3A%5B%20false%2Ctrue%2Cfalse%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Cfalse%2Cfalse%2Cfalse%5D%7D"];

NSString *cookie2 = [NSString stringWithFormat:@"%@; %@",cookie1,inCook];

NSLog(@"%@",cookie2);

我在这条线上得到了 EXC_BAD_ACCESS:

    cookie1 = [NSString stringWithFormat:@"__utma=138759908.342110698.1339513728.1347619146.1347628547.51; __utmz=138759908.1339513728.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __atuvc=0%7C33%2C0%7C34%2C0%7C35%2C16%7C36%2C50%7C37; GETAFREE_REFERRAL_URL=https%3A%2F%2Fwww.freelancer.com%2Fusers%2Fonlogin.php; _chartbeat2=skcgjvrzgugx7kr0.1339513733066.1347628548930.00000000000001; WRUID=0; session2=6d909115a69f37ff641d798045d6f252; __qca=P0-624434954-1344332147605; FL_DASHBOARD_VIEWPAGE_4164248=sellerview; FL_DASHBOARD_TABINDEX_4164248=6; GETAFREE_LANGUAGE=en; __utmc=138759908; __utmb=138759908.1.10.1347628547; SpryMedia_DataTables_project_table_=%7B%22iCreate%22%3A1347628548100%2C%22iStart%22%3A0%2C%22iEnd%22%3A0%2C%22iLength%22%3A10%2C%22sFilter%22%3A%22%22%2C%22sFilterEsc%22%3Atrue%2C%22aaSorting%22%3A%5B%20%5B7%2C%22desc%22%5D%5D%2C%22aaSearchCols%22%3A%5B%20%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%5D%2C%22abVisCols%22%3A%5B%20false%2Ctrue%2Cfalse%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Cfalse%2Cfalse%2Cfalse%5D%7D"];

里面的值只是从我从网站上捕获的内容中复制和粘贴。

谁能告诉我做错了什么?

非常感谢

4

3 回答 3

3

那个超长的 gobbledy-gook 字符串中有一大堆未转义的%字符。如果其中任何一个转换为格式字符串模式,它将尝试从stringWithFormat:调用中获取一个不存在的参数并构造一个格式化字符串。

不要使用stringWithFormat:. 鉴于它是一个静态字符串,只需使用一个裸@"....gobbledy-gook.....";.

于 2012-09-14T16:07:11.670 回答
1

在线上

cookie1 = [NSString stringWithFormat:@"__utma=138759908.342110698.1339513728.1347619146.1347628547.51; __utmz=138759908.1339513728.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __atuvc=0%7C33%2C0%7C34%2C0%7C35%2C16%7C36%2C50%7C37; GETAFREE_REFERRAL_URL=https%3A%2F%2Fwww.freelancer.com%2Fusers%2Fonlogin.php; _chartbeat2=skcgjvrzgugx7kr0.1339513733066.1347628548930.00000000000001; WRUID=0; session2=6d909115a69f37ff641d798045d6f252; __qca=P0-624434954-1344332147605; FL_DASHBOARD_VIEWPAGE_4164248=sellerview; FL_DASHBOARD_TABINDEX_4164248=6; GETAFREE_LANGUAGE=en; __utmc=138759908; __utmb=138759908.1.10.1347628547; SpryMedia_DataTables_project_table_=%7B%22iCreate%22%3A1347628548100%2C%22iStart%22%3A0%2C%22iEnd%22%3A0%2C%22iLength%22%3A10%2C%22sFilter%22%3A%22%22%2C%22sFilterEsc%22%3Atrue%2C%22aaSorting%22%3A%5B%20%5B7%2C%22desc%22%5D%5D%2C%22aaSearchCols%22%3A%5B%20%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%5D%2C%22abVisCols%22%3A%5B%20false%2Ctrue%2Cfalse%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Cfalse%2Cfalse%2Cfalse%5D%7D"];

您要求格式化字符串,而 '%' 是格式说明符。换句话说,-stringWithFormat需要 double- %(like %%) 在结果字符串中生成单个百分比。否则,它希望您使用上面链接中的说明符之一。

您的意思是简单地分配字符串,例如?

cookie1 = @"__utma=138759908.342110698.1339513728.1347619146.1347628547.51; __utmz=138759908.1339513728.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __atuvc=0%7C33%2C0%7C34%2C0%7C35%2C16%7C36%2C50%7C37; GETAFREE_REFERRAL_URL=https%3A%2F%2Fwww.freelancer.com%2Fusers%2Fonlogin.php; _chartbeat2=skcgjvrzgugx7kr0.1339513733066.1347628548930.00000000000001; WRUID=0; session2=6d909115a69f37ff641d798045d6f252; __qca=P0-624434954-1344332147605; FL_DASHBOARD_VIEWPAGE_4164248=sellerview; FL_DASHBOARD_TABINDEX_4164248=6; GETAFREE_LANGUAGE=en; __utmc=138759908; __utmb=138759908.1.10.1347628547; SpryMedia_DataTables_project_table_=%7B%22iCreate%22%3A1347628548100%2C%22iStart%22%3A0%2C%22iEnd%22%3A0%2C%22iLength%22%3A10%2C%22sFilter%22%3A%22%22%2C%22sFilterEsc%22%3Atrue%2C%22aaSorting%22%3A%5B%20%5B7%2C%22desc%22%5D%5D%2C%22aaSearchCols%22%3A%5B%20%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%2C%5B%22%22%2Ctrue%5D%5D%2C%22abVisCols%22%3A%5B%20false%2Ctrue%2Cfalse%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Cfalse%2Cfalse%2Cfalse%5D%7D";
于 2012-09-14T16:15:13.227 回答
0

您的字符串中的%符号cookie1正在抛出解析器。如果您想要一个实际的百分号,请使用%%而不是%.

或者:由于您没有在该字符串中添加任何其他内容,因此您也可以使用该stringWithString方法而不是stringWithFormat. 或者干脆@"your cookie"

于 2012-09-14T16:08:30.130 回答