Strange BAD_EXC_ACCESS when declaring a string - iphone

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

Related

objective-c concatenate NSString

I have problems to concatenate NSString.
Each time I pushed a button I want that something ("aux") is added to my string ("myString"). so:
NSString *aux = [NSString stringWithFormat: #"%d", buttonIndex];
myString=[NSString stringWithFormat:#"%#/%#",posTargetaText,aux];
aux = nil;
The first time i pushed the button it works good but the second it doesn't work.
Some help please?
So you can certainly use stringWithFormat, but why don't you use stringByAppendingString instead, since that's exactly what you want to do?
NSString *newString = [firstString stringByAppendingString:secondString];
You really don't need to use a mutable string unless you have a compelling reason to.
Not sure what exactly you want to do. But as per your code aux will have new buttonIndex value each time and You will have always new mystring when ever you tap button.
If you want to append string always in myString that you need to do like this.
myString=[NSString stringWithFormat:#"%#%#/%#",myString,posTargetaText,aux];
You suppose to add previous value of myString as well in new myString string ?
Not sure this is what you want or something different. Please explain in detail if this is not.
If you wanna concatenate two strings use NSMutablestring and method appendstring instead of NSString.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html
You need to use NSMutableString.

iPhone: Reading a string from a file is returning the wrong results

What I'm doing:
I am reading some data off a file several times while my app runs. I use the following code to do so:
NSString *dataPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"data.txt"];
NSString *data = [NSString stringWithContentsOfFile:dataPath encoding:NSStringEncodingConversionExternalRepresentation error:NULL];
NSArray *components = [data componentsSeparatedByString:#"|||||"];
The first time this is called, it works as expected - I get an array of length 5, each section containing a relevant string.
What goes wrong:
This file is never modified. Yet, when I call the same procedure a second time (or third, fourth etc) I don't get the same result. Instead, an array of length 1 is returned, containing only part of the necessary data.
Why? I can't see any reason for this to go wrong... any help is much appreciated!
Since the file is in you AppBundle this means that you can't modify this file at all.
Are you sure that, where ever this code is called, the autorelease object are retained correctly?
If you call this block of code every time you want this data, it might be an idea to save the results of the first time and use that every time. This will speed things up a bit.
Turns out that the code works fine. The problem was elsewhere in my code, where I was (accidentally) accessing protected directories. As a result, iOS blocked my app from accessing any files at all :o

Issue posting variable with iPhone SDK

I am encountering an issue while posting a variable with Xcode:
While running this code the app crashes while posting the variable to a webservice:
NSArray *array = [stringFromFile componentsSeparatedByString: #","];
NSString *time = [array objectAtIndex:1];
UTCorLocal = time;
NSLog(#"%#", UTCorLocal);
UTCorLocal variable is declared earlier in the code. The NSLog outputs the correct string, but when I try to use it further along in the code it crashes.
When I give the variable a static value like this:
UTCorLocal = #"UTC";
It all runs like it should do!
Could anybody please help, it's driving me crazy!
Thanks a lot,
Ron
It's probably released somewhere along the way, I would guess, without knowing the rest of your code. Try copying or retaining 'time' and see what happens.

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 ...

Objective-C NSString: strange characters when logging

Hey all, I'm a total noob when it comes to Objective-C / iPhone development.
I'm trying to pull in text from a SQLite DB. I have a while loop that looks like this:
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
And within that loop, this prints to the log just fine:
NSLog(#"Text: %s",sqlite3_column_text(selectstmt, 1));
This does not work:
Category *categoryObj = [[Category alloc] initWithPrimaryKey:primaryKey];
categoryObj.categoryName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSLog(#"cat name: %s",categoryObj.categoryName);
When I run the above and look at the logs I see:
cat name: ‡}00å
I tried to write the field out to a label, thinking it might be something specific to the NSLog but nothing shows up there. Clearly I'm missing something fundamental but I'm at a loss for what it is.
Log your string with %# instead of %s and you'll be fine. NSStrings aren't pointers to characters, they're full-fledged objects, so you need to use the "object" placeholder in the log format string.
This has the added advantage of doing the right thing with non-ASCII strings and all of the other important things that NSString gives you.
Note that if you had just logged the result from SQLite directly instead of creating an NSString with it, then your %s would have been correct.
Remember: %s is for C strings, %# is for Objective-C objects.