2012-09-14 65 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"