NSURL Connection Warning "Unused variable" - iphone

I am getting data from an NSURL connection like below. The code works, but I get a compiler warning that says "Unused variable strResult."
I don't actually need to use what is returned. I just need the URL to be executed.
How can I go about modifying this to get rid of the warning?
NSString *strURL = [NSString stringWithFormat:#"http://www.site.com/file.php?id=%#", id];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL
encoding:NSUTF8StringEncoding];
Thank you!

You have initialized and assigned data in strResult variable but you haven't used that variable.
So use that variable or just NSLog#("%#",strResult)
i suggest if the variable is not useful then remove that variable it occupies memory

You are not using the strResult anywhere after declaring ie the Warning .
Just log it you can see the warning goes off.
NSString *strResult = [[NSString alloc] initWithData:dataURL
encoding:NSUTF8StringEncoding];
NSLog(#"%#", strResult);

use NSURLConnection and call it's static method:
[NSURLConnction sendSynchronousRequest:.....]
and if you wanna send Asynchronous request use:
[NSURLConnection sendAsynchronusRequest:....]

The compiler just informs you that you created a variable and you are not using it (yet) .. so the best solution is to use it, as a return value of your method. It would help to see the complete implementation of the relevant method to help you better.
Hope this clarifies the warning.

You get that warning, because nowhere in your code, you actually use strResult.
Use it, and the warning is gone :) Or, remove the statement if you don't actually need the repsonse.

Related

Strange BAD_EXC_ACCESS when declaring a string

Okay, all I am doing is setting an NSString to a value with this code:
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%#/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
This is a string that I then turning into a URL for querying the TMDB database. This line of code gives me a BAD_EXC_ACCESS and it is blowing my mind because using this sort of NSString construction is something I have done thousands of times without a problem.
The one other thing to note is that this line is being executed right after another query call is made. The weird thing is that call makes the stringURL the same way, yet it works fine.
Any help would be appreciated...
You need to use %i to log an NSInteger, not %#
You need to use the following
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%d/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
Because searchID has NSInteger type and you are using "%#"
If it's an NSInteger you need to use %ld or you will got a warning, you can also use %d and explicitly cast to int via (int)searchID

Obj-c, incorrect checksum for freed object - object was probably modified after being freed

I'm getting this error
malloc: * * * error for object 0x8a591d4: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
No memory available to program now: unsafe to call malloc
No memory available to program now: unsafe to call malloc
It comes from this line
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Info"
ofType:#"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:plistPath];
NSDictionary *loadedPlist =
[NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:0 format:NULL errorDescription:NULL]; <<< this line
The value of plistData is set to Summary Unavailable
I'm not sure where to start ?
EDIT - added more code
I'm going to add another answer: it's also possible that you have ALREADY hosed the heap, and you're failing on propertyListFromData: just because it does a lot of allocations and happens to hit the bad spot. So edit your scheme in Xcode, and make sure you have all of the Memory Management items checked (in the Diagnostics tab of the Run task).
Have you set a breakpoint in malloc_error_break? This at least gives you a backtrace and you might see something of note. If nothing else, you could try po plistData from the GDB console.
BTW, while it may not help, I'd think dictionaryWithContentsOfFile: would be simpler.
How is Info.plist being stored? Is it a standard plist/xml file? Would the following solve the problem you are trying to solve, and possibly prevent this issue from happening?
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Info"
ofType:#"plist"];
NSDictionary *loadedPlist = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
Or if you want an auto-released object...
NSDictionary *loadedPlist = [NSDictionary dictionaryWithContentsOfFile:plistPath];

NSURLConnection help

Also I know that anything that I create using alloc I have to release it, but when I try to release 'request' object using [request release]; it throws the following error:
Program received signal: “EXC_BAD_ACCESS”.
kill
error while killing target (killing anyway): warning: error on line 2179 of "/SourceCache/gdb/gdb-1510/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)
You are creating the request using requestWithURL:cachePolicy:timeoutInterval:. Since that method name doesn't begin with "alloc" or "new" or contain "copy", according to the Memory Management Rules you don't own it and so should not be releasing it (unless you call retain on it explicitly, of course).
As for efficiency, the code seems fine. Note though that if the user can trigger a second load before the first completes that you will have problems; the solution to that is to either prevent such a thing or to save the NSURLConnection object created in load into an ivar and then have the pseudo-delegate methods check that against the passed connection before doing anything else. It's also a good idea to set the ivar to nil when you release the object it formerly contained, as then you cannot accidentally use the released object. And I note the variable name for your "authentication failed" alert is alertsuccess, that's misleading ;)
Try this in your load function:
NSMutableURLRequest *request;
request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:pageUrl]];
NSURLResponse *response;
NSError *error = [[NSError alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];`
NSString *responseString = [NSString stringWithCString:[data bytes] length:[data length]];
`

Represent XML content as NSData - iPhone

I have an external XML file which I want to be represented in an NSData object
I am doing this;
NSError *error;
NSString* contents = [NSString stringWithContentsOfUrl:[NSURL URLWithString:#"http://www.apple.com/"]
encoding:NSUTF8StringEncoding
error:&error];
NSData* xmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
But for some reasons, I am getting an error (does not respond)
Please help me.
Don't have time to test this out but I think you may want to try looking into NSData's dataWithContentsOfURL: or dataWithContentsOfURL:options:error: and get it directly as data.
Also, unless you just threw http://www.apple.com/ in as a placeholder, I don't believe the source of that page is valid XML. The following feed is valid xml: https://stackoverflow.com/feeds You could try that. with what you have now first and see if it works.
Hope this helps.
Updated:
Without my knowing your project, you may get some benefit from using TouchXML - https://github.com/mrevilme/TouchXML which handles XML very well including what you are trying to do:
CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithContentsOfURL:(NSURL *)inURL encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError];

UIWebView - adding an object to the address

I have a UIWebView that I'd like to load a web page and also append an object to the end of the address.
this is what I have:
NSString *urlAddress = #"http://www.wikipedia.org/%#",recipeName;
the name of a particular recipe would be appended to the end of the URL
I'm getting the "statically allocated instance of Objective-C class NSString" error. Is my syntax incorrect or is it something else?
I've been looking at this for quite a while now and thought I'd ask the community here.
thanks in advance.
NSString *urlAddress = [NSString stringWithFormat:#"http://www.wikipedia.org/%#",recipeName];
Try this code it will work..
hAPPY cODING...
Try this:
NSString *urlAddress = [NSString stringWithFormat: #"http://www.wikipedia.org/%#",recipeName];
You cannot create string with format directly, try [NSString stringWithFormat].
Well not fast enough I guess ...