2012-09-14 66 views
1

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

这里的代码:

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"]; 

值内部只是复制和从我从网站上获取粘贴。

任何人都可以告诉我做错了什么?

非常感谢

+1

下面的答案是正确的,但另外你不需要行'NSString * cookie1 ...'和'cookie1 = [NSString ...''你可以用'NSString * cookie1 = @替换它们' “__utma = 138759908.3421106 ....”;' –

回答

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"]; 

,和 '%' 是一个format specifier。 换句话说,-stringWithFormat需要双击-%(如%%)才能在结果字符串中生成一个百分比。否则,它期望您使用上述链接中的一个说明符。

您是不是只是简单地指定字符串而已,比如?

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"; 
+0

非常感谢。我改变了很多次,这让我感到困惑。 – Kalvin

3

这gobbledy性国的尤伯杯长的字符串中有转义字符%的整体转换。如果其中任何一个转换为格式字符串模式,它将尝试从stringWithFormat:调用中获取一个不存在的参数并构造一个格式化的字符串。

请勿使用stringWithFormat:。鉴于它是一个静态字符串,只需使用一个空的@"....gobbledy-gook.....";

0

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

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