Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-16-2011, 09:01 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 136
Allah is on a distinguished road
Default Escaping Strings and Casting

I'm trying to escape a string, so naturally I turned to
Code:
NSString *escapedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
However this did not work, so I googled and I found this and it tells me to use this:
Code:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL,
    (CFStringRef)unencodedString,
    NULL,
    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
    kCFStringEncodingUTF8 );
But, unbelievably i got this error:
Code:
Automatic Reference Counting Issue: Cast to 'NSString *' of a non-Objective-C to an Objective-C pointer is disallowed with Automatic Reference Counting
I can't seem to find out how to cast things properly, despite many threads and such saying this kind of thing is what you do.

Anybody know of a way around this dilemma?
Allah is offline  
Old 07-16-2011, 10:59 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 136
Allah is on a distinguished road
Default

I've thought of a way around (making my own class and using replaceCharactersInRange) but I don't think this is very elegant, and I'd still prefer to use this method :/
Allah is offline  
Old 07-16-2011, 01:50 PM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

For start, why did the first approach not work for you?

NSString is toll free bridged with CFStringRef, but how are you using automatic reference counting, are you working with beta SDK or Xcode?
baja_yu is offline  
Old 07-16-2011, 02:24 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 136
Allah is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
For start, why did the first approach not work for you?

NSString is toll free bridged with CFStringRef, but how are you using automatic reference counting, are you working with beta SDK or Xcode?
It simply didn't work. This poster had the same problem and here it says that it's a known thing.


And it's the beta SDK. Don't give me any of that NDA rubbish please.
Allah is offline  
Old 07-16-2011, 02:26 PM   #5 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

What I got from that is that displaying it with NSLog has issues not the method. I've used it plenty of times and it worked fine. Try printing the result in a text file and see if it's ok.
baja_yu is offline  
Old 07-16-2011, 02:41 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 136
Allah is on a distinguished road
Default

I can't get stuff to write to a text file, but I know that it isn't escaped because if it was, then my program would work, and not crash.
Allah is offline  
Old 07-16-2011, 02:50 PM   #7 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Why does it crash? What's the output in the console. Why can't it write to a file?! Are you checking the error object after any of these operations? Rarely does something "just not work", there's plenty of things to check and try to find out what is failing and why.
baja_yu is offline  
Old 07-16-2011, 02:53 PM   #8 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 136
Allah is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Why does it crash? What's the output in the console. Why can't it write to a file?! Are you checking the error object after any of these operations? Rarely does something "just not work", there's plenty of things to check and try to find out what is failing and why.
Forget writing to a file for a second. It crashes because when the connectionDidReceiveData (because I'm using NSURLConnection) delegate method is called, there is no data to write (because the string isn't escaped). I've looked at whats in the NSLog, escaped all the characters myself, entered it into safari, and it works, so it obviously isn't being escaped.
Allah is offline  
Old 07-16-2011, 03:01 PM   #9 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Is stringByAddingPercentEscapesUsingEncoding returning an object or is it nil? If it returns an object, what are the contents? Print it out to a file (use the writeToFile:atomically:encoding:error: method of NSString and watch the error object to see if and why it fails to write) or NSLog (if it works) and see what exactly it gives you. If it's nil, what's in the 'string' object? Is that by any chance nil? If not, what are its contents? What is the exact reason for the crash (post the crash log)?

Well, if you're not willing to debug your own code thoroughly I'm not sure what more to say.
baja_yu is offline  
Old 07-16-2011, 03:53 PM   #10 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 136
Allah is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Is stringByAddingPercentEscapesUsingEncoding returning an object or is it nil? If it returns an object, what are the contents? Print it out to a file (use the writeToFile:atomically:encoding:error: method of NSString and watch the error object to see if and why it fails to write) or NSLog (if it works) and see what exactly it gives you. If it's nil, what's in the 'string' object? Is that by any chance nil? If not, what are its contents? What is the exact reason for the crash (post the crash log)?

Well, if you're not willing to debug your own code thoroughly I'm not sure what more to say.
Yes, stringByAddingPercentEscapesUsingEncoding is returning an object, however it hasn't changed in the text file.

The reason for the crash is EXC_BAD_ACCESS, but I stepped through the code (a few hours ago), and found out that the error is when I do this:
Code:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	[responseData appendData:data];
}
I used the unescaped string in Safari (because it doesn't escape), and the JSON reply had data[] in it, so I think the error is coming around because:

1) The request is invalid, because it's not escaped.
2) Because of this, it's returning no data.
3) When it tries to write no data, it crashes.
Allah is offline  
Old 07-16-2011, 04:13 PM   #11 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Is data in that method nil? If so, don't append it. Are you sure your reference to responseData is ok and not deallocated? Can you post the string before and after calling escape?
baja_yu is offline  
Old 07-16-2011, 09:47 PM   #12 (permalink)
Super Moderator
 
Join Date: Jan 2011
Posts: 434
Rhade is on a distinguished road
Default

Quote:
Originally Posted by Allah View Post
And it's the beta SDK. Don't give me any of that NDA rubbish please.
I'll give you plenty of that rubbish.
Rhade is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 399
17 members and 382 guests
blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0